Reputation: 31
I am very new to coding and i wanted to know if i could put a list of objects in an array for collision.
instead of writing... if player.hitTestObject(wall1) then wall 2 then wall 3?
can i put them all in one array or something else so i can just say if player.hitTestObject(everywall)
Thanks. my code looks like this and i have around 30 walls.
I would be very grateful if someone posted an example.
function keydown(event:KeyboardEvent) :void {
switch(event.keyCode){
case Keyboard.LEFT :
hero.x -= 10;
if(hero.hitTestObject(w1) || hero.hitTestObject(w2) || hero.hitTestObject(w3) || hero.hitTestObject(w4)){
hero.x +=10;}
break;
case Keyboard.RIGHT:
hero.x +=10;
if(hero.hitTestObject(w1) || hero.hitTestObject(w2) || hero.hitTestObject(w3) || hero.hitTestObject(w4)){
hero.x -=10;}
break;
case Keyboard.UP:
hero.y -=10;
if(hero.hitTestObject(w1) || hero.hitTestObject(w2) || hero.hitTestObject(w3) || hero.hitTestObject(w4)){
hero.y +=10;}
break;
case Keyboard.DOWN:
hero.y += 10;
if(hero.hitTestObject(w1) || hero.hitTestObject(w2) || hero.hitTestObject(w3) || hero.hitTestObject(w4)){
hero.y -=10;}
break;
default :
break;
}
Upvotes: 0
Views: 181
Reputation: 10325
As shown in the Actionscript Docs, hitTestObject()
takes a DisplayObject
as a parameter, not any kind of list.
If you want, you can implement this kind of functionality yourself.
function myHitTest(obj:DisplayObject, arr:Array):Boolean {
for (var i:int = 0; i < arr.length; ++i) {
//for (var item:DisplayObject in arr) {
var item:DisplayObject = arr[i]
if (obj.hitTestObject(item)) {
return true;
}
}
return false;
}
And then you can use that...
if(myHitTest(hero,[w1,w2,w3]))
...
or
var everywall:Array = [w1,w2,w3];
...
if(myHitTest(hero,everywall))
...
Upvotes: 4