gpt4 book ai didi

javascript - Typescript - 移相器碰撞重叠功能不起作用

转载 作者:行者123 更新时间:2023-12-03 04:56:23 25 4
gpt4 key购买 nike

所以我只是想在下落的物体和玩家 Sprite 之间进行简单的碰撞。没什么复杂的。

我正在尝试使用 game.physicals.arcade.overlap() 函数。

这是我的代码:

Player 类 ->

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 中的几个移相器函数上遇到了同样的问题。我能够使用内联 lambda 函数来工作。

this.game.physicals.arcade.overlap(this.player, this.rocks, (p, r) => {console.log("这有效吗?!"); }, null, this);

关于javascript - Typescript - 移相器碰撞重叠功能不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42416874/

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