【发布时间】:2017-08-09 10:24:36
【问题描述】:
所以我只是想在坠落的物体和玩家精灵之间进行简单的碰撞。它没有什么复杂的。
我正在尝试使用 game.physics.arcade.overlap() 函数。
这是我的代码:
播放器类 ->
export class Player{
game: Phaser.Game;
player: Phaser.Sprite;
constructor(game:Phaser.Game){
this.game = game;
this.player = this.game.add.sprite(400, 520, "Player");
this.game.physics.arcade.enable(this.player);
}
create(){
}
update(){
if (this.game.input.keyboard.isDown(Phaser.Keyboard.LEFT))
{
if(this.player.x >= -10){
this.player.x -= 7;
}
}
else if (this.game.input.keyboard.isDown(Phaser.Keyboard.RIGHT))
{
if(this.player.x <= 830){
this.player.x += 7;
}
}
}
}
落物类 ->
export class Rock {
game:Phaser.Game;
rocks: Phaser.Group;
constructor(game:Phaser.Game){
this.game = game;
}
create(){
this.rocks = this.game.add.physicsGroup(Phaser.Physics.ARCADE);
this.game.physics.arcade.enable(this.rocks);
var x = 10;
for(var i = 0; i < 9; i++){
var rock = this.rocks.create(x, this.game.rnd.between(-30,-5), "Rock");
rock.body.velocity.y = this.game.rnd.between(240,300);
x += 105;
}
}
update(player){
this.rocks.forEach(this.checkPos, this);
}
checkPos(rock) {
if (rock.y > 800)
{
rock.y = -100;
}
}
}
我使用重叠功能的主游戏文件 ->
create(){
this.difficulties = [];
this.difficulties.push(new Difficulty(0, 5));
this.difficulties.push(new Difficulty(1, 7));
this.difficulties.push(new Difficulty(2, 9));
this.currentDifficulty = this.difficulties[0];
this.shouldChangeDifficulty = true;
this.levelOne = new LevelOne(this.game);
this.levelOne.create(this.currentDifficulty);
this.currentLevel = this.levelOne;
this.player = new Player(this.game);
this.player.create();
this.rocks = new Rock(this.game);
this.rocks.create();
}
update(){
this.player.update();
this.rocks.update(this.player);
this.game.physics.arcade.overlap(this.player, this.rocks, this.collisionHandler, null, this);
}
collisionHandler (player, rock) {
console.log("Does it work ?!");
}
【问题讨论】:
-
我不知道使用 TypeScript 开发 Phaser 但我想问你一个问题:你的控制台有什么错误吗?你正确激活物理了吗?:
this.game.physics.startSystem(Phaser.Physics.ARCADE);
标签: javascript typescript collision phaser-framework