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)
    },

});

I'm using Cocos Creator, basically I have an array of empty objects, and I want my enemy to patrol back and forth towards those objects. The problem is the enemy would complete a full round ( move towards all objects and back) and then gives me error when it goes back to the first object, strangely enough, sometimes it will complete more than 1 round before giving the error:

Uncaught TypeError: Cannot read property 'position' of undefined

It might be because the position comparison is not accurate enough, but I don't know how else to do it.

Update: I finally fixed it, the problem was here:

this._forward = !this._forward;

I changed it to this:

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

That's very very strange, but it's now working fine :/

Abdou023
  • 1,654
  • 2
  • 24
  • 45

2 Answers2

0

This seems to be a JavaScript problem. See JavaScript "cannot read property "bar" of undefined or Detecting an undefined object property.

According to the one answer this happens when:

  • The object has the property and its value is not undefined.

  • The object has the property and its value is undefined.

  • The object does not have the property.

(I don't know JS so I can't tell you what exactly is causing this in your code).

Laurel
  • 5,965
  • 14
  • 31
  • 57
0

It's either this line:

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

or this line:

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

Your this._currentMove is out of array size bounds, or this.flags[] array has undefined on that position. Add additional logging to get the exact scenario

  • I don't know what else should I log out, the weird thing is that it sometimes work and sometimes doesn't, which made me question the position comparison. Have you tried the code yourself? or do you have a different approach to achieving this ? – Abdou023 Mar 24 '16 at 20:23
  • If you're stuck that badly perhaps you can upload your game somewhere and I'll debug it for you :) Just throw the link in, man – Роман Гуйван Mar 25 '16 at 10:39