Phaser JS中区域范围内的游戏对象

时间:2019-11-28 02:09:01

标签: javascript phaser-framework

有人知道如何检查相对于Phaser js中的另一个游戏对象而言,某个区域中是否存在游戏对象或组中的对象部分吗?我专门尝试从Mario游戏中编程“ Red Koopa Troopa”,当它们到达悬崖边缘时会转身。有任何想法吗?这是敌人的代码,如果有帮助的话:

class Koopa extends Phaser.GameObjects.Sprite{
    constructor(scene, x, y) {
        super(scene, x, y, "Koopa");
        scene.add.existing(this);
        this.play("koopaWalk");
        scene.physics.world.enableBody(this);
        this.theScene = scene;
        this.status = "normal";
        this.playerOverlap;
        this.capOverlap;
        this.orgX = x;
        this.orgY = y;
        this.shellTimer = 0;
    }

    initObj() {
        this.body.setBounceX(1);
        this.body.setVelocityX(75);
        this.playerOverlap = this.theScene.physics.add.overlap(this.theScene.player, this, this.koopaState, null, this);
    }

    koopaState() {
        if(this.status == "shell"){
            this.status = "slide";
            this.anims.play("shellAnim");
            if (this.x < this.theScene.player.x) {
                this.body.setVelocityX(-300);
            } else {
                this.body.setVelocityX(300);
            }
        } else if (this.status == "slide"){
            if (this.theScene.player.y + 20 < this.y && !this.theScene.player.body.onFloor() && this.theScene.player.body.velocity.y > 0) {
                if (this.theScene.player.specialMove != "groundPound"){
                    this.theScene.player.setVelocityY(-400);
                }
                this.status = "shell";
                this.body.setVelocityX(0);
                this.anims.play("shell");
            } else {
                if (this.body.velocity.x > 0) {
                    if(this.x < this.theScene.player.x){
                        this.killPlayer();
                    }
                }
                if (this.body.velocity.x < 0) {
                    if (this.x > this.theScene.player.x){
                        this.killPlayer();
                    }
                }
            }
        } else {
            if (this.theScene.player.y + 20 < this.y && !this.theScene.player.body.onFloor()) {
                    if (this.theScene.player.specialMove != "groundPound") {
                        this.theScene.player.setVelocityY(-400);
                    }
                    this.body.setVelocityX(0);
                    this.anims.play("shell");
                    this.status = "shell";
                } else {
                    this.killPlayer();
                }
        }
    }

    killPlayer() {
        this.theScene.player.disableBody(true, true);
        this.theScene.cap.disableBody(true, true);
    }

    //NPC Loop
    preUpdate (time, delta) {
        super.preUpdate(time, delta);
            //Must be executed last to terminate the object


            if (this.anims.getCurrentKey() == "goombaKill" && !this.anims.isPlaying) {
                this.destroy();
            }
            if (this.body.velocity.x < 0){
                this.flipX = false;
            } else {
                this.flipX = true;
            }
            if (this.status == "shell") {
                this.shellTimer++;
                if (this.shellTimer > 200) {
                    this.status = "normal";
                    this.shellTimer = 0;
                    this.anims.play("koopaWalk");
                    if (this.flipX) {
                        this.body.setVelocityX(-75);
                    } else {
                        this.body.setVelocityX(75);
                    }
                }
            } else {
                this.shellTimer = 0;
            }

    }
    respawn() {
        this.x = this.orgX;
        this.y = this.orgY;
        this.body.setVelocityX(75);
        this.body.setEnable(true);
        this.setVisible(true);
    }
}

0 个答案:

没有答案