Skip to content

Commit

Permalink
[#59] various function updates and fixes
Browse files Browse the repository at this point in the history
* moved `getContainer` under `me.game`
* optimzed the `hasChild` function
* fixed the `moveTo*` set of functions
  • Loading branch information
obiot committed Aug 3, 2013
1 parent d2ac4b6 commit 0e4698c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
13 changes: 13 additions & 0 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -1365,6 +1365,19 @@ window.me = window.me || {};
}
};


/**
* Returns the entity container of the specified Child in the game world
* @name getEntityContainer
* @memberOf me.game
* @function
* @param {me.ObjectEntity} child
* @return {me.EntityContainer}
*/
api.getEntityContainer = function(child) {
return child.ancestor;
};


/**
* remove an object from the world
Expand Down
29 changes: 8 additions & 21 deletions src/entity/entitycontainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,19 +185,7 @@
* @return {Boolean}
*/
hasChild : function(child) {
return (this.children.indexOf( child ) !== -1);
},

/**
* Returns the container of the specified Child
* @name getContainer
* @memberOf me.EntityContainer
* @function
* @param {me.ObjectEntity} child
* @return {me.EntityContainer}
*/
getContainer : function(child) {
return child.ancestor;
return (this == child.ancestor);
},

/**
Expand Down Expand Up @@ -245,17 +233,16 @@
* @param {me.ObjectEntity} child
*/
removeChild : function(child) {
var index = this.children.indexOf( child );

if ( index !== -1 ) {

if (this.hasChild(child)) {

child.ancestor = undefined;

if (typeof (child.destroy) == 'function') {
child.destroy();
}

this.children.splice( index, 1 );
this.children.splice( this.getChildIndex(child), 1 );

me.entityPool.freeInstance(child);

Expand All @@ -272,7 +259,7 @@
* @param {me.ObjectEntity} child
*/
moveUp : function(child) {
var childIndex = getChildIndex(child);
var childIndex = this.getChildIndex(child);
if (childIndex -1 >= 0) {
// note : we use an inverted loop
this.swapChildren(child, this.getChildAt(childIndex-1));
Expand All @@ -287,7 +274,7 @@
* @param {me.ObjectEntity} child
*/
moveDown : function(child) {
var childIndex = getChildIndex(child);
var childIndex = this.getChildIndex(child);
if (childIndex+1 < this.children.length) {
// note : we use an inverted loop
this.swapChildren(child, this.getChildAt(childIndex+1));
Expand All @@ -302,7 +289,7 @@
* @param {me.ObjectEntity} child
*/
moveToTop : function(child) {
var childIndex = getChildIndex(child);
var childIndex = this.getChildIndex(child);
if (childIndex > 0) {
// note : we use an inverted loop
this.splice(0, 0, this.splice(childIndex, 1)[0]);
Expand All @@ -319,7 +306,7 @@
* @param {me.ObjectEntity} child
*/
moveToBottom : function(child) {
var childIndex = getChildIndex(child);
var childIndex = this.getChildIndex(child);
if (childIndex < (this.children.length -1)) {
// note : we use an inverted loop
this.splice((this.children.length -1), 0, this.splice(childIndex, 1)[0]);
Expand Down

0 comments on commit 0e4698c

Please sign in to comment.