I tried almost everything to make my character stop when it detects a wall. It only works for UP and RIGHT or DOWN and LEFT but not all 4 directions, so only one y movement and one x movement. So I decided to make 4 functions, one for each direction. But then it only works when the left key is pressed and hits a wall.
My question to you is; What do I have to do to make the collision detection stop the character from moving in all 4 directions? Thanks in advance
var leftArrow:Boolean;
var upArrow:Boolean;
var rightArrow:Boolean;
var downArrow:Boolean;
var speed:int = 10;
var hitting:Boolean;
var ismoving:Boolean;
var wallsRect:Rectangle = bounds.getBounds(this);
var charRect:Rectangle = char.getBounds(this);
var boundsBmpData = new BitmapData(wallsRect.width, wallsRect.height, true, 0);
var charBmpData = new BitmapData(charRect.width, charRect.height, true, 0);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
stage.addEventListener(Event.ENTER_FRAME, detectHit2);
stage.addEventListener(Event.ENTER_FRAME, walkingLEFT);
stage.addEventListener(Event.ENTER_FRAME, walkingUP);
stage.addEventListener(Event.ENTER_FRAME, walkingDOWN);
stage.addEventListener(Event.ENTER_FRAME, walkingRIGHT);
boundsBmpData.draw(bounds);
charBmpData.draw(char);
function keyPressed(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.LEFT)
{
leftArrow = true;
}
if (event.keyCode == Keyboard.UP)
{
upArrow = true;
}
if (event.keyCode == Keyboard.RIGHT)
{
rightArrow = true;
}
if (event.keyCode == Keyboard.DOWN)
{
downArrow = true;
}
}
function keyReleased(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.LEFT)
{
leftArrow = false;
}
if (event.keyCode == Keyboard.UP)
{
upArrow = false;
}
if (event.keyCode == Keyboard.RIGHT)
{
rightArrow = false;
}
if (event.keyCode == Keyboard.DOWN)
{
downArrow = false;
}
}
function walkingLEFT(event:Event):void
{
if (leftArrow && ! hitting)
{
char.x -= speed;
}else
{
hitting = false;
ismoving = false;
}
}
function walkingRIGHT(event:Event):void
{
if (rightArrow && ! hitting)
{
char.x += speed;
}else
{
hitting = false;
ismoving = false;
}
}
function walkingUP(event:Event):void
{
if (upArrow && ! hitting)
{
char.y -= speed;
}
else
{
hitting = false;
ismoving = false;
}
}
function walkingDOWN(event:Event):void
{
if (downArrow && ! hitting)
{
char.y += speed;
}
else
{
hitting = false;
ismoving = false;
}
}
function detectHit2(e:Event):void
{
if(boundsBmpData.hitTest(new Point(bounds.x, bounds.y),
255,
charBmpData,
new Point(char.x, char.y),
255))
{
hitting = true;
ismoving = false;
}
else
{
hitting = false;
bounds.filters = [];
}
}