Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Maze borders

New Here ,
Sep 17, 2015 Sep 17, 2015

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.

TOPICS
ActionScript
278
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 18, 2015 Sep 18, 2015
LATEST

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);

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines