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;
}
As shown in the Actionscript Docs,
hitTestObject()takes aDisplayObjectas a parameter, not any kind of list.If you want, you can implement this kind of functionality yourself.
And then you can use that…
or