So I’ve never dealt with collision detection since I mostly have just made simple web-designs, apps, and things that just don’t make use of physics or anything like that.
But today I decided to fiddle with it a little and was pretty excited when I started out just using a square to move around with keys and so I made some walls to figure out some collision stuff.
And basically, I can’t move out of the wall once I collide. I know I could make it so when I collide the sprite backs off a few pixels, but it causes that jittery effect. So I was wondering is it possible to check for a collision before the block is animated?
I’ve searched online for a while, everything seems to be scripted for as2 and just isn’t what I’m looking for.
Below is my sloppy testCode on the stage just to get my bearings before I implement anything in classes. The two sprites in use are sloppily instanced named: person, wall.
import flash.events.Event;
import flash.events.KeyboardEvent;
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyedDown);
stage.addEventListener(KeyboardEvent.KEY_UP, keyedUp);
this.addEventListener(Event.ENTER_FRAME, frameHandler);
var keyIsDown:Boolean = false;
var keyUP:Boolean = false;
var keyDOWN:Boolean = false;
var keyLEFT:Boolean = false;
var keyRIGHT:Boolean = false;
var speed:Number = 5;
function frameHandler(e:Event):void
{
if (keyIsDown)
{
if (keyLEFT)
{
if (! this.person.hitTestObject(this.wall))
{
this.person.x -= speed;
}
}
if (keyRIGHT)
{
if (! this.person.hitTestObject(this.wall))
{
this.person.x += speed;
}
}
}
}
function keyedDown(e:KeyboardEvent):void
{
keyIsDown = true;
switch (e.keyCode)
{
case 65 :
keyLEFT = true;
break;
case 68 :
keyRIGHT = true;
break;
default :
break;
}
}
function keyedUp(e:KeyboardEvent):void
{
keyIsDown = false;
keyUP = false;
keyDOWN = false;
keyLEFT = false;
keyRIGHT = false;
}
Also to add onto my question.
What if I wish to check collisions on ‘barrier’ boolean propertied tiles? Like if I made impassable terrain, how does one check that for collisions on a wide scale?