How do enemies follow you?
  • Where in tutorial.js do the purple enemies, (for demo 3), follow you.  The red enemies go straight, but I can't find out how the purple ones follow...

    Thanks!

  • selimselim
    Accepted Answer
    Each time a enemy position is update the following method is called on its associated object:
    this.update = function(playerNode){
    this.updateX(playerNode);
    this.updateY(playerNode);
    };


    There you can see that this method in turn update the X and Y coordinate of the enemy. For the purple enemies the following code gets executed for the Y coordinate update:
    Brainy.prototype.updateY = function(playerNode){
    if((this.node[0].gameQuery.posy+this.alignmentOffset) > $(playerNode)[0].gameQuery.posy){
    var newpos = parseInt(this.node.css("top"))-this.speedy;
    this.node.css("top",""+newpos+"px");
    } else if((this.node[0].gameQuery.posy+this.alignmentOffset) < $(playerNode)[0].gameQuery.posy){
    var newpos = parseInt(this.node.css("top"))+this.speedy;
    this.node.css("top",""+newpos+"px");
    }
    }


    There you can see why the enemy Y position align with the player position.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!