kittykatattack / ga

The world's tiniest, cutest and funnest game engine
451 stars 85 forks source link

Two functions for gameobject plus some suggestions. #31

Closed qsrahman closed 9 years ago

qsrahman commented 9 years ago

Adding two functions the the gameobject will make it easy to remove it from parent and add an object to a group:

o.kill = function() {
    if(this.parent) {
        this.parent.removeChild(this);
    }
   return this;
};
o.addTo = function(group) {
    group.addChild(this);
    return this;
};

Also each function of gameobject, group etc. should return this for chaining.

kittykatattack commented 9 years ago

Currently the addChild method automatically removes a sprite from its existing parent when it adds it to the new parent.

    o.addChild = function(sprite) {

      //Remove the sprite from its current parent, if it has one, and
      //the parent isn't already this object
      if (sprite.parent) {
        sprite.parent.removeChild(sprite);
      }

      //Make this object the sprite's parent and
      //add it to this object's `children` array.
      sprite.parent = o;
      o.children.push(sprite);
    };