【发布时间】:2021-12-03 10:10:48
【问题描述】:
你能告诉我我在哪里犯了代码错误吗?为什么我的敌人不跟随玩家?
我不确定我是否定义了正确的 followPlayer() 。
'SceneB' 是第二个场景,会是这个问题吗? Console.log 很清晰。我正在使用 EasyStar.js 库。游戏正在运行,一切都在运行,但敌人被冻结了。
谢谢
class SceneB extends Phaser.Scene {
constructor(){
super('SceneB')
}
//..
create(){
finder = new EasyStar.js()
let grid = []
let tile
for(let y = 0; y < map.height; y++) {
let col = []
for(let x = 0; x < map.width; x++) {
tile = map.getTileAt( x, y )
col.push( tile.index)
}
grid.push(col)
}
finder.setGrid(grid)
let tileset = map.tilesets[0]
let properties = tileset.tileProperties
let acceptableTiles = []
for (let i = tileset.firsgid - 1;i < tiles.total;i++) {
if(properties[i].collides == false ) {
acceptableTiles.push ( i+1)
}
}
finder.setAcceptableTiles( acceptableTiles)
}
update(){}
followPlayer(player,evil) {
let toX = this.player.x
let toY = this.player.y
let fromX = this.evil.x
let fromY = this.evil.y
finder.findPath(fromX, fromY, toX, toY, function(path) {
console.log(path)
this.moveCharacter(path)
})
finder.calculate()
}
moveCharacter(path){
let mytimeline = this.scene.tweens.createTimeLine()
for(let i = 0;i < path.length - 1;i++) {
let ex = path[i + 1].x
let ey = path[e + 1].y
mytimeline.add({
targets: this.player,
x:{value: ex * map.tileWidth, duration: 200},
y:{value: ey * map.tileHeight, duration: 200}
})
}
mytimeline.play()
}
【问题讨论】:
标签: javascript phaser-framework