简体   繁体   English

敌人不跟随玩家。 移相器3

[英]Enemy don not follow Player. Phaser3

Can you please tell me where I made mistake in code ?你能告诉我我在代码中哪里出错了吗? why my enemy do not follow player?为什么我的敌人不跟随玩家? I am not sure if I defined correct followPlayer() .我不确定我是否定义了正确的 followPlayer() 。 'SceneB' is second Scene, can be this problem ? 'SceneB' 是第二个场景,可能是这个问题吗? Console.log is clear . Console.log 很清楚。 I am using EasyStar.js library.我正在使用 EasyStar.js 库。 Game is working , everything is running but enemy is frozen .游戏正在运行,一切都在运行,但敌人被冻结了。
Thank you谢谢


 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 is never getting called, so the logic will never actually trigger. followPlayer永远不会被调用,所以逻辑永远不会真正触发。

You'll want to call this somewhere in your code, depending upon how you've implemented your player logic.您需要在代码中的某处调用它,具体取决于您实现player逻辑的方式。

You may also want to check inside finder.findPath that path !== null , and do something if it is, as that could also cause an issue.您可能还想在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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM