Copy link to clipboard
Copied
I've created maze in AS3. You're supossed to move your avatar with arrow keys witout crossing borders. Borders are created as their own movie clip.
I'd like to use a code, which will calculate/define avatar's possition after pressing arrow key. If possition will be on border, code should prevent avatar from moving there. (some kind of Collision handler or Hit test).
This is pretty much the code I'm having so far (avatar is named ''oseba''):
var keys:Array = [];
function keyDownHandler(e:KeyboardEvent):void{
keys[e.keyCode] = true;
}
function keyUpHandler(e:KeyboardEvent):void{
keys[e.keyCode] = false;
}
function move (e:Event):void{
if (keys[Keyboard.RIGHT]) {
oseba.x += 5;
}
if (keys[Keyboard.LEFT]) {
oseba.x -= 5;
}
if (keys[Keyboard.UP]) {
oseba.y -= 5;
}
if (keys[Keyboard.DOWN]) {
oseba.y += 5;
}
}
function handleCollision(e:Event ):void{
if(oseba.hitTestObject(back)){
gotoAndPlay(5);
}
This is code I used in a car track game in AS2 and i'm looking for something simmilar in AS3 (the whole game is in as3, so I need new code).
//check for collisions
if (_root.terrain.hitTest(this["lpx"+who], this["lpy"+who], true)){
_root["car"+who]._rotation += 5;
this["speed"+who] *= 0.85;
}
In advance thank you for any suggestions.
Copy link to clipboard
Copied
i'm not sure that handleCollision function will work like you want, but if it does, use:
function move (e:Event):void{
if (keys[Keyboard.RIGHT]) {
oseba.x += 5;
}
if (keys[Keyboard.LEFT]) {
oseba.x -= 5;
}
if (keys[Keyboard.UP]) {
oseba.y -= 5;
}
if (keys[Keyboard.DOWN]) {
oseba.y += 5;
}
handleCollision(null);
}
Find more inspiration, events, and resources on the new Adobe Community
Explore Now