在没有物理的画布移相器中检测到碰撞

时间:2019-05-22 10:23:17

标签: javascript html phaser-framework phaser

我必须做一个迷你电子游戏来练习。我在相位器,JavaScript和Java中都有一个代码。

画布是在Phaser中绘制的。 当我的飞船碰到画布极限时,我需要在世界边界或其他物体上碰撞,因为我的飞船没有显示在屏幕上。

我的老师被禁止从事诸如街机游戏,忍者o P2之类的物理学工作。

解决方案是否在javascript o phaser中都没有关系。只有我需要在画布的边界设置限制。

我有这个功能可以在相位器中画出世界

game = new Phaser.Game(1024, 600, Phaser.AUTO, 'gameDiv'

在预加载中,我的精灵已经遍布世界:

game.global.myPlayer.image = game.add.sprite(0, 0, 'spacewar', game.global.myPlayer.shipType);

在创建功能中,我具有键盘控制:

this.wKey = game.input.keyboard.addKey(Phaser.Keyboard.UP);
        this.sKey = game.input.keyboard.addKey(Phaser.Keyboard.DOWN);
        this.aKey = game.input.keyboard.addKey(Phaser.Keyboard.LEFT);
        this.dKey = game.input.keyboard.addKey(Phaser.Keyboard.RIGHT);
        this.spaceKey = game.input.keyboard.addKey(Phaser.Keyboard.CONTROL);
        this.shiftKey = game.input.keyboard.addKey(Phaser.Keyboard.SHIFT);

在更新功能中,运动:

if (this.wKey.isDown)
                msg.movement.thrust = true;
            if (this.sKey.isDown)
                msg.movement.brake = true;
            if (this.aKey.isDown)
                msg.movement.rotLeft = true;
            if (this.dKey.isDown)
                msg.movement.rotRight = true;
            if (this.spaceKey.isDown) {
                msg.bullet = this.fireBullet()
            }
            if (this.shiftKey.isDown) {
                msg.push = true;
            }

1 个答案:

答案 0 :(得分:1)

不确定为学校项目寻求解决方案将如何帮助您学习任何东西。

但是无论如何,每帧(每秒60次)都会调用update()函数,因此在该函数内部,您可以执行以下操作来防止玩家移出游戏区域:

// cannot move outside game area, left and right
if (game.global.myPlayer.image.x < 0) {
    game.global.myPlayer.image.x = 0;
}
if (game.global.myPlayer.image.x > game.world.width) {
    game.global.myPlayer.image.x = game.world.width;
}

// cannot move outside game area, top and bottom
if (game.global.myPlayer.image.y < 0) {
    game.global.myPlayer.image.y = 0;
}
if (game.global.myPlayer.image.y > game.world.height) {
    game.global.myPlayer.image.y = game.world.height;
}