在线测试地址:http://www.ifiero.com/uploads/phaserjs3/jumper/
空格键:轻按:跳低 ,长按:跳高
键盘:--> 向右 , <-- 向左
请确保已打开电脑的音乐开关
var config = {type: Phaser.AUTO,width: 650,height: 450, parent: "ifierocom", physics: { default: 'arcade', arcade: { gravity: { y: 350 }, debug: false } }, scene: { preload: preload, create: create, update: update } }; var player; var stars; var bombs; var platforms; var cursors; var score = 0; var gameOver = false; var scoreText; // var jumpTimer = 0; var game = new Phaser.Game(config); var isSmall = true; // small jump music var isSuper = true; // super jump music function init() { this.jumpTimer = 0; this.isCanJump = false; this.isCanLeft = false; this.isCanRight = false; this.isCanStand = true; this.velocityL = 0; this.velocityR = 0; } function preload() { this.load.image('sky', 'assets/sky.png'); this.load.image('ground', 'assets/platform.png'); this.load.image('star', 'assets/star.png'); this.load.image('bomb', 'assets/bomb.png'); // this.load.spritesheet('dude', 'assets/dude.png', { // frameWidth: 32, // frameHeight: 48 // }); this.load.spritesheet('mario', 'assets/mario_mario.png', { frameWidth: 16, frameHeight: 16, margin: 1, spacing: 1 }); //music: small this.load.audio('jumpSmall', 'assets/audio/JumpSmall.mp3'); //music: super this.load.audio('jumpSuper', 'assets/audio/JumpSuper.mp3'); //music: super this.load.audio('coin', 'assets/audio/Coin.mp3'); } function create() { this.spaceBar = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.SPACE); // A simple background for our game this.add.image(400, 300, 'sky'); // The platforms group contains the ground and the 2 ledges we can jump on platforms = this.physics.add.staticGroup(); // Here we create the ground. // Scale it to fit the width of the game (the original sprite is 400x32 in size) platforms.create(400, 568, 'ground').setScale(2).refreshBody(); // Now let's create some ledges platforms.create(600, 400, 'ground'); platforms.create(50, 250, 'ground'); platforms.create(750, 220, 'ground'); // The player and its settings // player = this.physics.add.sprite(100, 450, 'dude'); player = this.physics.add.sprite(100, 450, 'mario', 0); player.setScale(2.5); // Player physics properties. Give the little guy a slight bounce. player.setBounce(0.2); player.setCollideWorldBounds(true); // Our player animations, turning, walking left and walking right. this.anims.create({ key: 'run', frames: this.anims.generateFrameNumbers('mario', { start: 1, end: 3 }), frameRate: 10, repeat: -1 }); this.anims.create({ key: 'shift', frames: [{ key: 'mario', frame: 4 }], frameRate: 20 }); this.anims.create({ key: 'stand', frames: [{ key: 'mario', frame: 0 }], frameRate: 20 }); this.anims.create({ key: 'jump', frames: [{ key: 'mario', frame: 5 }], frameRate: 20 }); // Input Events cursors = this.input.keyboard.createCursorKeys(); // Some stars to collect, 12 in total, evenly spaced 70 pixels apart along the x axis stars = this.physics.add.group({ key: 'star', repeat: 11, setXY: { x: 12, y: 0, stepX: 70 } }); stars.children.iterate(function (child) { // Give each star a slightly different bounce child.setBounceY(Phaser.Math.FloatBetween(0.4, 0.8)); }); bombs = this.physics.add.group(); // The score scoreText = this.add.text(16, 16, 'score: 0', { fontSize: '32px', fill: '#000' }); scoreText.setScrollFactor(0); // Collide the player and the stars with the platforms this.physics.add.collider(player, platforms, function () { // console.log('hit ground'); // console.groupEnd(); this.isCanJump = false; }, null, this); this.physics.add.collider(stars, platforms); this.physics.add.collider(bombs, platforms); // Checks to see if the player overlaps with any of the stars, if he does call the collectStar function this.physics.add.overlap(player, stars, collectStar, null, this); this.physics.add.collider(player, bombs, hitBomb, null, this); this.cameras.main.setBounds(0, 0, 800, 600);