我在处理小行星和子弹之间的碰撞时遇到了困难。 碰撞永远不会发生!!!
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>My Game</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/phaser/2.6.2/phaser.min.js"></script>
<style type="tex`enter code here`t/css">
body {
margin: 0;
}
</style>
</head>
<body background-color = "#6666ff">
<script type="text/javascript">
var game = new Phaser.Game(1090, 525, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.image('neb', 'nebu.jpg');
game.load.image('ship', 'thrust_ship5.png');
game.load.spritesheet('bullet', 'rgblaser.png', 4, 4);
game.load.image('asteroid', 'asteroid1.png');
game.load.audio('boden', 'war1.mp3');
game.load.audio('fire', 'fire1.wav');
}
var sprite;
var weapon;
var cursors;
var fireButton;
var fx;
var asteroid;
function create() {
back = game.add.image(-400, -400, 'neb');
weapon = game.add.weapon(40, 'bullet');
asteroid = game.add.image(800,200,'asteroid');
music = game.add.audio('boden');
music.loop = true;
fx = game.add.audio('fire',0.1);
back.smoothed = false;
music.play();
weapon.setBulletFrames(0, 80, true);
weapon.bulletKillType = Phaser.Weapon.KILL_WORLD_BOUNDS;
weapon.bulletSpeed = 400;
// Speed-up the rate of fire, allowing them to shoot 1 bullet every 50ms
weapon.fireRate = 50;
世界各地的子弹包围着对方
weapon.bulletWorldWrap = true;
sprite = this.add.sprite(400, 300, 'ship');
game.physics.enable(sprite, Phaser.Physics.ARCADE);
game.physics.enable(asteroid, Phaser.Physics.ARCADE);
game.physics.enable(weapon, Phaser.Physics.ARCADE);
sprite.physicsBodyType = Phaser.Physics.ARCADE;
sprite.anchor.set(0.5);
game.physics.arcade.enable(sprite);
sprite.body.drag.set(70);
sprite.body.maxVelocity.set(200);
告诉武器跟踪“玩家”#39;雪碧 没有偏离位置 但真正的&#39;参数告诉武器跟踪精灵旋转
weapon.trackSprite(sprite, 0, 0, true);
cursors = this.input.keyboard.createCursorKeys();
fireButton = this.input.keyboard.addKey(Phaser.KeyCode.SPACEBAR);
}
function update() {
game.physics.arcade.collide(asteroid,weapon, collision_handler, null, this);
if (checkOverlap(asteroid, sprite))
{
console.log("Hi, overlap occured!");
asteroid.kill();
}
if (checkOverlap1(weapon, asteroid))
{
console.log("Hi, overlap occured in iffffffffffffffff!");
asteroid.kill();
//weapon.kill();
}
//game.physics.arcade.collide(weapon, asteroid);
//game.physics.arcade.overlap(weapon, asteroid, collisionHandler, null, this);
game.physics.arcade.overlap(sprite, asteroid, hitEnemy, null, this);
if (cursors.up.isDown)
{
game.physics.arcade.accelerationFromRotation(sprite.rotation, 300, sprite.body.acceleration);
}
else if (cursors.down.isDown)
{
game.physics.arcade.accelerationFromRotation(sprite.rotation, -300, sprite.body.acceleration);
}
else
{
sprite.body.acceleration.set(0);
}
if (cursors.left.isDown)
{
sprite.body.angularVelocity = -300;
}
else if (cursors.right.isDown)
{
sprite.body.angularVelocity = 300;
}
else
{
sprite.body.angularVelocity = 0;
}
if (fireButton.isDown)
{
weapon.fire();
fx.play();
}
game.world.wrap(sprite, 16);
}
function render() {
// weapon.debug();
}
function hitEnemy (one, two) {
one.kill();
two.kill();
console.log("Hit");
}
function checkOverlap(spriteA, spriteB) {
var boundsA = spriteA.getBounds();
var boundsB = spriteB.getBounds();
return Phaser.Rectangle.intersects(boundsA, boundsB);
}
function checkOverlap1(spriteA, spriteB) {
var boundsA = spriteA.bounds;
var xcor = spriteA.x;
var ycor = spriteB.y;
var boundsB = spriteB.getBounds();
//console.log(xcor + "" + ycor);
//console.log("Inside Collision 1");
//console.log(Phaser.Rectangle.contains(spriteB,xcor, ycor));
return Phaser.Rectangle.contains(spriteB,xcor, ycor);
}
function collision_handler (object1, object2) {
//object1.kill();
object2.kill();
}
</script>
</body>
</html>