Skip to main content
Participating Frequently
March 16, 2015
Question

As3 script issue

  • March 16, 2015
  • 13 replies
  • 927 views

Hey all, I am currently trying to create a type of falling object game. I want the user to click a keyboard button and an object will fall down from the sky and if the player does not move will crush their character. I have coded a ball falling from the sky but I need help switching it to a key press button, specifically the letter "T". So upon T keypress the object will fall straight down.

This topic is closed to new replies. Start a new post to keep the conversation going.

13 replies

Ned Murphy
Legend
March 16, 2015

What code do you already have for the object dropping?

Participating Frequently
March 16, 2015

This is what I first tried but it appears when I run the game. I am looking for the object to only appear after keypress "T" and the object will fall and crush the character. Thank you for replying

ball_mc.x = 250;

ball_mc.y = 0; /* position starts */

var speed:Number = 10; /* falling speed */

ball_mc.addEventListener(Event.ENTER_FRAME, moveDown);

function moveDown(e:Event):void

{

e.target.y += speed;

if(e.target.y >= 350)

{

circle_mc.removeEventListener(Event.ENTER_FRAME, moveDown);

}

}

Ned Murphy
Legend
March 17, 2015

What you can do is add the event listener with the key release and remove the key listener until the drop finishes...

var speed:Number = 10; /* falling speed */

stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);

function keyReleased(e:KeyboardEvent):void {
       if (e.keyCode == 84){
            stage.removeEventListener(KeyboardEvent.KEY_UP, keyReleased);
            ball_mc.x = 250;
            ball_mc.y = 0; /* position starts */
            ball_mc.addEventListener(Event.ENTER_FRAME, moveDown);
      }
}

function moveDown(e:Event):void {
      e.target.y += speed;
      if(e.target.y >= 350){
            ball_mc.removeEventListener(Event.ENTER_FRAME, moveDown);
            stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
      }
}

rezun8
Inspiring
March 16, 2015