gpt4 book ai didi

javascript - Phaser 无法检测碰撞

转载 作者:行者123 更新时间:2023-12-01 03:14:50 26 4
gpt4 key购买 nike

我正在用 Phaser.js 编写一个游戏,你必须避免移动宇宙飞船。鼠标被替换为不明飞行物。我需要帮助来弄清楚为什么移相器无法检测到不明飞行物和宇宙飞船群之间的碰撞。请原谅我的困惑,因为我已经尝试了很多可能的解决方案。代码:

var spaceShip;
var score = 0;
var text;
var gameOver = false;
var veggies;
var cursor;
var gameplayState = {
create: function () {
game.add.sprite(0,0,'Stars');
veggies = game.add.group();
veggies.enableBody = true;
veggies.physicsBodyType = Phaser.Physics.ARCADE;
function doSpaceship() {

if (ships == true){
if (ships == false){
kill();
}
if (Math.random() > 0.5){
spaceShip = veggies.create(0, 300, 'Ship');
spaceShip.angle = 90;
game.physics.arcade.collide(veggies, cursor, collisionHandler);
spaceShip.body.velocity.x=1000;
fx.play();
var times = game.time.events.add(Phaser.Timer.SECOND * Math.random() * 3, doSpaceship, this);
var timess = game.time.events.add(Phaser.Timer.SECOND * 1, kill, this);
} else {
spaceShip = veggies.create(Math.random() * 640, 480, 'Ship');
game.physics.arcade.collide(veggies, cursor, collisionHandler);

spaceShip.body.velocity.y=-1000;
gx.play();
var times = game.time.events.add(Phaser.Timer.SECOND * Math.random() * 3, doSpaceship, this);
var timess = game.time.events.add(Phaser.Timer.SECOND * 1, kill, this);
}
}
function kill(){
if(gameOver == false){
score++;
}
spaceShip.destroy();
}
}
cursor = game.add.sprite(0,0,'Ufo');
var ships = true;
text = game.add.text(0,0,"Score: " + score);
text.font = 'Saira Extra Condensed';
doSpaceship();
function collisionHandler(){
gameOver = true;
}
},
update: function(){
cursor.x = game.input.mousePointer.x;

cursor.y = game.input.mousePointer.y;
text.setText("Score: " + score);
}
};

最佳答案

你应该移动这条线

game.physics.arcade.collide(veggies, cursor, collisionHandler);

到更新功能。

关于javascript - Phaser 无法检测碰撞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45602676/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com