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

As3 script issue

New Here ,
Mar 16, 2015 Mar 16, 2015

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.

TOPICS
ActionScript
847
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
Enthusiast ,
Mar 16, 2015 Mar 16, 2015
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
LEGEND ,
Mar 16, 2015 Mar 16, 2015

What code do you already have for the object dropping?

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
New Here ,
Mar 16, 2015 Mar 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);

}

}

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
LEGEND ,
Mar 16, 2015 Mar 16, 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);
      }
}

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
New Here ,
Mar 17, 2015 Mar 17, 2015

Would this not over complicate a simple way to code a keypress event listener, unless you know an easier way to code this

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
LEGEND ,
Mar 17, 2015 Mar 17, 2015

What is overcomplicated about it? 

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
New Here ,
Mar 17, 2015 Mar 17, 2015

oh sorry it was the an error that I sorted it out , it works fine now I just have to figure out how to stop at a certain point to work with the animation I have. So that the character is not crushed until the object hits him

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
LEGEND ,
Mar 17, 2015 Mar 17, 2015

Look into incorporating the hitTestObject() method.

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
New Here ,
Mar 17, 2015 Mar 17, 2015

Will look into it now thanks, I have the other character working using the arrow keys. Is it possible to make it look more real rather than the character gliding

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
LEGEND ,
Mar 17, 2015 Mar 17, 2015

I would think it is possible, most things are, though I have no idea what you mean.  Think about what you want and what you would have to do to make it happen.

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
New Here ,
Mar 18, 2015 Mar 18, 2015

Yes I sorted it out , I ran into a issue yesterday with the game menu. I created a new frame and added the following code:

stop();

startbutton.addEventListener(MouseEvent.CLICK,startF);

function startF(e:Event):void{

gotoAndPlay(2);

}

When the mouse is clicked the game screen appears but the button stays covering the stage, do you know how to remove this upon clicking the mouse

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
Guest
Mar 18, 2015 Mar 18, 2015

The code for the ball, which piece of this code is used to assign the keyboard letter T or is it any key pressed triggers the action?

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
New Here ,
Mar 24, 2015 Mar 24, 2015
LATEST

Sadly the code I used does not give the outcome I wanted, so the bug does not get crushed when the ball falls down. I dunno what happened and I am a complete novice to sorting this out

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