[英]Enemy don not follow Player. Phaser3
你能告訴我我在代碼中哪里出錯了嗎? 為什么我的敵人不跟隨玩家? 我不確定我是否定義了正確的 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()
}
followPlayer
永遠不會被調用,所以邏輯永遠不會真正觸發。
您需要在代碼中的某處調用它,具體取決於您實現player
邏輯的方式。
您可能還想在finder.findPath
中檢查path !== null
,如果是,則執行某些操作,因為這也可能導致問題。
finder.findPath(fromX, fromY, toX, toY, function(path) {
console.log(path);
if (path !== null) {
this.moveCharacter(path);
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.