overlaps: AxSprite, AxGroup and AxCloud

Discuss suggestions to improve the Axel library.

overlaps: AxSprite, AxGroup and AxCloud

Postby isidoro » Tue Oct 09, 2012 12:11 am

When overlapping an Entity against a Group or Cloud, for loops continue even if overlaps have found one Group's Entity really overlaps. Maybe you can directly return true, avoiding the loop continues:

AxSprite:
Code: Select all
override public function overlaps(other:AxRect):Boolean {
   if (!exists) {
      return false;
   }
   
   if (other is AxGroup) {
      var objects:Vector.<AxEntity> = (other as AxGroup).members;
      for each (var o:AxEntity in objects) {
         if (o.exists && overlaps(o)) {
            return true;
         }
      }
   } else if (other is AxCloud) {
      var sprites:Vector.<AxSprite> = (other as AxCloud).members;
      for each (var s:AxSprite in sprites) {
         if (s.exists && overlaps(s)) {
            return true;
         }
      }
   } else if (other != null) {
      return super.overlaps(other);
   }
   return false;
}

//Instead of:
override public function overlaps(other:AxRect):Boolean {
   if (!exists) {
      return false;
   }
   
   var overlapFound:Boolean = false;
   if (other is AxGroup) {
      var objects:Vector.<AxEntity> = (other as AxGroup).members;
      for each (var o:AxEntity in objects) {
         if (o.exists && overlaps(o)) {
            overlapFound = true;
         }
      }
   } else if (other is AxCloud) {
      var sprites:Vector.<AxSprite> = (other as AxCloud).members;
      for each (var s:AxSprite in sprites) {
         if (s.exists && overlaps(s)) {
            overlapFound = true;
         }
      }
   } else if (other != null) {
      overlapFound = super.overlaps(other);
   }
   return overlapFound;
}


AxGroup:
Code: Select all
override public function overlaps(other:AxRect):Boolean {
   if (!exists) {
      return false;
   }
   
   for (var i:uint = 0; i < members.length; i++) {
      if ((members[i] as AxEntity).exists && (members[i] as AxEntity).overlaps(other)) {
         return true;
      }
   }
   return false;
}

//Instead of:
override public function overlaps(other:AxRect):Boolean {
   if (!exists) {
      return false;
   }
   
   var overlapFound:Boolean = false;
   for (var i:uint = 0; i < members.length; i++) {
      if ((members[i] as AxEntity).exists && (members[i] as AxEntity).overlaps(other)) {
         overlapFound = true;
      }
   }
   return overlapFound;
}


AxCloud:
Code: Select all
override public function overlaps(other:AxRect):Boolean {
   if (!exists) {
      return false;
   }
   
   for (var i:uint = 0; i < members.length; i++) {
      if ((members[i] as AxSprite).exists && (members[i] as AxSprite).overlaps(other)) {
         return true;
      }
   }
   return false;
}

//Instead of:
override public function overlaps(other:AxRect):Boolean {
   if (!exists) {
      return false;
   }
   
   var overlapFound:Boolean = false;
   for (var i:uint = 0; i < members.length; i++) {
      if ((members[i] as AxSprite).exists && (members[i] as AxSprite).overlaps(other)) {
         overlapFound = true;
      }
   }
   return overlapFound;
}
isidoro
Private
 
Posts: 7
Joined: Fri Jun 15, 2012 10:17 am

Return to Suggestions

Who is online

Users browsing this forum: No registered users and 1 guest

cron