2016-03-21 7 views
0
cc.Class({ 
    extends: cc.Component, 

    properties: { 

     flags: { 
      default: [], 
      type: [cc.Node], 
     }, 
     speed: 2, 
     _currentMove: 0, 
     _forward: true, 
    }, 

    // use this for initialization 
    onLoad: function() { 

    }, 

    start: function() { 
     this.node.position = this.flags[0].position; 

    }, 
    // called every frame, uncomment this function to activate update callback 
    update: function (dt) { 
     this.movement(); 
    }, 

     movement: function() { 
// comparePos is a custome helper method, check bottom of script 
      if (this.comparePos(this.node.position, this.flags[this._currentMove].position) 
      && this._forward) { 
       this._currentMove++; 
       this.moveActions(); 

     } 
     else if (this.comparePos(this.node.position, this.flags[this._currentMove].position) 
     && !this._forward) { 
      this._currentMove--; 
      this.moveActions(); 
     } 

     if (this._currentMove >= this.flags.length - 1) { 
      this._currentMove = this.flags.length - 1; 
      this._forward = !this._forward; 
     } 
     else if (this._currentMove <= 0) { 
      this._currentMove = 0; 
      this._forward = !this._forward; 
     } 
    }, 

    moveActions: function() { 
     var move = cc.moveTo(this.speed, this.flags[this._currentMove].position); 
     this.node.runAction(move); 
    }, 

    comparePos: function (a, b) { 
     return Math.round(a.x) == Math.round(b.x) && 
     Math.round(a.y) == Math.round(b.y) 
    }, 

}); 

私はココスクリエイターを使用していますが、基本的に空のオブジェクトの配列を持っています。敵にこれらのオブジェクトに向かって往復させてもらいたいのです。問題は完全なラウンド(すべてのオブジェクトに向かって移動して戻ってくる)を完了し、それが最初のオブジェクトに戻ったときに私にエラーを与え、奇妙なことに、エラーを出す前に1ラウンド以上を完了することがある場合です。敵の巡視システム

Uncaught TypeError: Cannot read property 'position' of undefined 

位置の比較が十分正確ではない可能性がありますが、それ以外の方法はわかりません。

アップデート:私は最終的にそれを固定 、問題はここにあった

this._forward = !this._forward; 

が、私はこれにそれを変更:非常に非常に奇妙だが、それが今で正常に動作します

this._forward = true; // and false down below 

:/

答えて

0

これはJavaScriptの問題のようです。 hereまたはhereを参照してください。 -Theオブジェクトプロパティを持ち、その値は未定義ではありません

:ときこの問題が発生した1つの答えによると

- オブジェクトにはプロパティがあり、その値は未定義です。

- オブジェクトにはプロパティがありません。

(私はJSを知らないので、あなたのコードでこれが何を引き起こしているのかは分かりません)

  if (this.comparePos(this.node.position, this.flags[this._currentMove].position) 

またはこの行:

else if (this.comparePos(this.node.position, this.flags[this._currentMove].position) 

あなたthis._currentMove配列サイズの範囲外である、またはthis.flagsは[]配列は、その位置に未定義た

0

それは、このラインのどちらかです。追加のログを追加して正確なシナリオを取得する

+0

ログアウトする必要があるかどうかわからない、時にはうまくいかないことがあり、時にはうまくいかないため、私は位置比較に疑問を感じました。自分でコードを試しましたか?またはこれを達成するための別のアプローチがありますか? – Abdou023

+0

もしあなたがそれをひどく詰め込んでいるとすれば、あなたはどこかであなたのゲームをアップロードすることができ、私はあなたのためにそれをデバッグします:)ただリンクを投げます –

関連する問題