So I am working on a top-down style game (think Zelda), and I need my character to come to a stop when he hits a wall. Here is what I have for character movement
// I'll use comments to save space
// Here the necessary statements are imported
char.addEventListener(Event.ENTER_FRAME, charWalk);
stage.addEventListener(KeyboardEvent.KEY_DOWN, kDown);
stage.addEventListener(KeyboardEvent.KEY_UP, kUp);
char.gotoAndStop('idledown');
var charSpeed:int = 5;
// this array holds references to all the keys
var keys:Array = [];
function kDown(e:KeyboardEvent):void
{
keys[e.keyCode] = true;
}
// Actual code for movement
function charWalk(e:Event):void
{
if (keys[Keyboard.RIGHT])
{
char.x += charSpeed;
char.gotoAndStop('walkright');
}
// Repeated for the different directions
}
// Return to idle animations
function kUp(e:KeyboardEvent):void
{
keys[e.keyCode] = false;
if(e.keyCode == Keyboard.LEFT)
{
char.gotoAndStop('idleleft');
}
// Again, this is repeated for all directions
}
I have been scouring the internet for some help with this. But everything either assumes I just want to return an integer on collision, or that the character dies or something.
I had code that set the charSpeed to 0 on collision, but then he was stuck to the wall, because from that point on he was never, not touching it!
How can I stop him from moving through a wall, but still let him move!?
GAH!