【问题标题】:Phaser Wont Detect CollisionPhaser 检测不到碰撞
【发布时间】:2017-08-10 09:23:06
【问题描述】:

我正在使用 phaser.js 编写一个游戏,你必须避免移动宇宙飞船。鼠标被一个不明飞行物取代。我需要帮助来弄清楚为什么移相器不会检测到 UFO 和宇宙飞船群之间的碰撞。由于我尝试了很多潜在的解决方案,请原谅混乱。 代码:

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);
    }
};

【问题讨论】:

    标签: javascript phaser-framework


    【解决方案1】:

    你应该移动这条线

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

    到更新函数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多