多个球的碰撞
原文地址:http://www.phaser.io/examples/v2/arcade-physics/multiball。
效果
源代码
window.onload = function () { var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser_container', preload: preload, create: create, update: update }); function preload() { game.load.image('atari', 'images/phaser/atari130xe.png'); game.load.spritesheet('bullets', 'images/phaser/balls.png', 17, 17); } var atari; var balls; var cursors; function create() { game.physics.startSystem(Phaser.Physics.ARCADE); game.stage.backgroundColor = '#2d2d2d'; balls = game.add.group(); balls.createMultiple(250, 'bullets', 0, false); atari = game.add.sprite(300, 450, 'atari'); game.physics.arcade.gravity.y = 400; // 开启世界中所有对象的物理效果(参数true表示子对象也开启物理效果) game.physics.arcade.enable(game.world, true); atari.body.allowGravity = 0; atari.body.immovable = true; cursors = game.input.keyboard.createCursorKeys(); game.time.events.loop(150, fire, this); game.add.text(16, 16, '按Left / Right键移动', { font: '18px Arial', fill: '#ffffff' }); } function fire() { var ball = balls.getFirstExists(false); if (ball) { ball.frame = game.rnd.integerInRange(0, 6); ball.exists = true; ball.reset(game.world.randomX, 0); ball.body.bounce.y = 0.8; } } function reflect(a, ball) { if (ball.y > (atari.y + 5)) { return true; } else { ball.body.velocity.x = atari.body.velocity.x; ball.body.velocity.y *= -(ball.body.bounce.y); return false; } } function update() { game.physics.arcade.collide(atari, balls, null, reflect, this); atari.body.velocity.x = 0; if (cursors.left.isDown) { atari.body.velocity.x = -200; } else if (cursors.right.isDown) { atari.body.velocity.x = 200; } balls.forEachAlive(checkBounds, this); } function checkBounds(ball) { if (ball.y > 600) { ball.kill(); } } }
发布时间:2016/10/7 下午9:31:26 阅读次数:4792