Hi, I am quite new to hitTestObject…
So im making a game for an assignment, its in top down view and is not tile based. to stop players from walking where they are not allowed i’ve added about 5 or more walls to the stage as movie clips on each different frame. I would like to use hitTestObject to make ‘hero’ stop when he interacts with them.
Question: how can i do this? Do I need to make an array for the walls? what do i put in it to make the man keep walking but not be able to pass?
A side question: Can i put elements in an array without them being present on the stage? I would like to put all ‘walls’ inside one array so that I can keep using the same one but I am afraid it wont work… (I have about 200 walls all up)
Here is my code: (the ‘hero’ has 3 different states, one for walking right and left and one for walking up and down and one for standing still)
import flash.events.Event;
stop();
var town1Walls:Array=new Array(wall1,wall2,wall3,wall4,wall5,wall7)
//creates keyboard movement
stage.addEventListener(KeyboardEvent.KEY_DOWN , moveHero);
function moveHero( evt:KeyboardEvent ) : void
{
if ( evt:keyCode == Keyboard.UP )
{
hero.y -= 10;
hero.scaleY=1;
hero.gotoAndStop(‘walking2’);
for(var u = 0; u<town1Walls.length; u++)
{
if(hero.hitTestObject(town1Walls[u]))
{
hero.y+=10;
}
}
}
}
if ( evt.keyCode == Keyboard.DOWN )
{
hero.y += 10;
hero.scaleY=1;
hero.gotoAndStop('walking2');
for(var d = 0; d<town1Walls.length; d++)
{
if(hero.hitTestObject(town1Walls[d]))
{
hero.y-=10;
}
}
}
else if ( evt.keyCode == Keyboard.LEFT )
{
hero.x -= 10;
hero.scaleX=1;
hero.gotoAndStop('walking');
//checks when left key is pressed whether hero is at screen's edge
if (currentLabel == "town2" && hero.x < 0)
{
gotoAndStop("town1");
hero.x = 800;
}
hero.x -= 10;
if (currentLabel == "town3" && hero.x < 0)
{
gotoAndStop("town2");
hero.x = 800;
}
}
else if ( evt.keyCode == Keyboard.RIGHT )
{
hero.x += 10;
hero.scaleX=-1;
hero.gotoAndStop('walking');
if (currentLabel == "town1" && hero.x > 800)
{
gotoAndStop("town2");
hero.x = 0;
}
if (currentLabel == "town2" && hero.x > 800)
{
gotoAndStop("town3");
hero.x = 0;
}
}
else
{
hero.gotoAndStop('still');
}
I am getting errors saying ‘undefined property, keyCode…’
Any help is appreciated… Thanks!