rexrainbow / phaser3-rex-notes

Notes of phaser3 engine
MIT License
1.21k stars 263 forks source link

Cannot read properties of undefined (reading 'moveToward') #383

Closed songyukino1 closed 1 year ago

songyukino1 commented 1 year ago

I am writing a chess in TypeScript. According to the documentation example, I wrote the following code, but it is giving me an error.

import { MoveTo } from 'phaser3-rex-plugins/plugins/board-components';
import Level from "../scenes/Level";
import MyBoard from "./Board";
import Box from "./Box";

export default class Player extends Phaser.GameObjects.Sprite
{
    constructor(scene: Level, board: MyBoard)
    {
        super(scene, 0, 0, "tilesheet", 5)
        scene.add.existing(this)
        this.moveTo = scene.rexBoard.add.moveTo(this, {
            occupiedTest: true
        })
            .on('occupy', (box: Box, player: Player) => {
                const direction = board.directionBetween(player, box);
                box.moveTo.moveToward(direction);
            })
    }
    moveTo: MoveTo
    wander() {
        if (this.moveTo.isRunning) {
            return;
        }
        this.moveTo
            .once('complete', () => {
                this.wander();
            })
            .moveToRandomNeighbor();
    }

}
console.log(box)

i found that the method does exist

songyukino1 commented 1 year ago

Alright, it seems like I've figured it out. I need to add an "occupiedTest" property to the box.