|
Title: strange displaylist removal bug Post by: JMickle on February 25, 2012, 01:29:07 PM In my android app I have a displaylist containing all the "Actors" on the screen, and when you want to remove one, this is called:
Code: public function remove(a:Actor):void where trouble is the position of the Actor in the displaylist.{ var THIS_MEANS_TROUBLE:int = list.indexOf(a); for (var i:int = THIS_MEANS_TROUBLE; i < list.length-1; i++) list[THIS_MEANS_TROUBLE] = list[THIS_MEANS_TROUBLE + 1]; list.pop(); } When you collide with a balloon, it is supposed to remove that balloon, but it is removing one of the other elements (always the same ones, though). The other elements don't even have any collision code. Here is the collision for the Balloon. Code: if (collide(x, y + 1, "balloon")) { speed.y = jump; Director.cast.remove(collide(x, y + 1, "balloon")); } and the collide() function: Code: public function collide(cx:int, cy:int, t:String):Actor { var dick:int = Director.cast.list.length; var mybox:Rectangle = hitbox.clone(); mybox.x += x; mybox.y += y; var colbox:Rectangle; for (var i:int = 0; i < dick; i++) { if (Director.cast.list[i].type==t) { colbox = Director.cast.list[i].hitbox.clone(); colbox.x += Director.cast.list[i].x; colbox.y += Director.cast.list[i].y; if (mybox.intersects(colbox)) { return Director.cast.list[i]; } } } return null; } what am i doing wrong????????? Title: Re: strange displaylist removal bug Post by: SFBTom on February 26, 2012, 03:36:32 AM Code: public function remove(a:Actor):void { var THIS_MEANS_TROUBLE:int = list.indexOf(a); for (var i:int = THIS_MEANS_TROUBLE; i < list.length-1; i++) list[THIS_MEANS_TROUBLE] = list[THIS_MEANS_TROUBLE + 1]; list.pop(); } Perhaps here you meant to use i? Code: public function remove(a:Actor):void { var THIS_MEANS_TROUBLE:int = list.indexOf(a); for (var i:int = THIS_MEANS_TROUBLE; i < list.length-1; i++) list[i] = list[i+ 1]; list.pop(); } Title: Re: strange displaylist removal bug Post by: Rob Lach on February 26, 2012, 09:02:00 AM In addition to what SFBTom mentioned, what are your intentions with list.pop() here:
Code: public function remove(a:Actor):void { var THIS_MEANS_TROUBLE:int = list.indexOf(a); for (var i:int = THIS_MEANS_TROUBLE; i < list.length-1; i++) list[THIS_MEANS_TROUBLE] = list[THIS_MEANS_TROUBLE + 1]; list.pop(); } since what it's doing is removing the last element in the array. I haven't touched AS3 in years but I'd do something like this: Code: public function remove(a:Actor):void { list.splice(list.indexOf(a),1); } Title: Re: strange displaylist removal bug Post by: JMickle on February 26, 2012, 11:38:07 AM Quote from: SFBTom Perhaps here you meant to use i? ugh do I feel stupid. I did a workaround by setting the removed id as null instead. also yeah I didn't notice the splice function before. thanks guys! |