TypeError:this.physics在自定义函数中未定义

时间:2019-07-08 15:01:27

标签: javascript phaser-framework game-development

我创建的所有函数都给出此错误。

一段时间前,我开始玩泡泡射击游戏。 当我启动它时,我使用了移相器2。 我决定继续开发并希望完成此游戏。 看到相位器3已完全发布,我认为这是迁移和学习差异的好时机。

我尝试了几种解决方案。 1.一个非常丑陋的解决方案,我在调用函数时已将其包含在所有参数中。我确信这不是这样做的方法。 2.我已将所有自定义功能添加到config中的场景列表中。

var config = {
    type: Phaser.AUTO,
    width: 1050,
    height: 900,
    physics: {
        default: 'arcade',
        arcade: {
            gravity: { y: 0 },
            debug: true
        }
    },
    scene: {
        preload: preload,
        create: create,
        update: update,
        render: render,
        stopBall: stopBall,
        spawnBalls: spawnBalls,
        getBallType: getBallType,
        getNewBall: getNewBall,
        getGridPos: getGridPos,
        clearConnectedBalls: clearConnectedBalls
    }
};

var game = new Phaser.Game(config);

function preload() {
    this.load.image('arrow', 'assets/img/arrow.png');
    this.load.image('pokeball', 'assets/img/pokeballs/pokeball.png');
    this.load.image('greatball', 'assets/img/pokeballs/greatball.png');
    this.load.image('masterball', 'assets/img/pokeballs/masterball.png');
    this.load.image('premierball', 'assets/img/pokeballs/premierball.png');
    this.load.image('safariball', 'assets/img/pokeballs/safariball.png');
    this.load.image('ultraball', 'assets/img/pokeballs/ultraball.png');

}

function create() {
    // Fuction called after 'preload' to setup the game 
    ballGrid = this.add.group();
    ballGrid.enableBody = true;
    ballsToShoot = [];
    levelGridArray = {...}
    }
    //active mouse
    this.input.mouse.capture = true;

    getNewBall(this);

 }

function update() {
    // Function called 60 times per second
    this.physics.world.collide(currentBall, ballGrid,function(){
        stopBall(this, currentBall)
    })
    //aiming arrow

    //shooting pokeball

}


function stopBall(test, currentBall) {
    currentBall.body.velocity.x = 0
    currentBall.body.velocity.y = 0
    ballGrid.add(currentBall)
    getGridPos(test, currentBall.x, currentBall.y);

    currentBall.body.immovable = true;

    getNewBall(this)
}

function getNewBall() {

    if (typeof currentBall !== 'undefined') {
        currentBall.destroy()
        currentBall = secondBall;
        currentBall.x = 8 * 60;
        currentBall.y = 14 * 60;
        secondBall.destroy()
    } else {
        currentBall = this.physics.add.sprite(8 * 60, 14 * 60, getBallType())
        currentBall.disableBody(false, false);
    }
    secondBall = this.physics.add.sprite(1 * 60, 14 * 60, getBallType())
}

stopBall函数具有参数this。 我删除了getNewBall()的参数,以显示错误显示其面孔的位置。

http://cihantasdelen.com/bubble2/ 在这里您将找到游戏。

我希望有人可以向我解释为什么会出现此错误。

0 个答案:

没有答案