Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
this should give you the basis to work from
http://www.freeactionscript.com/2010/09/8-way-keyboard-player-movement/
Copy link to clipboard
Copied
What code do you already have for the object dropping?
Copy link to clipboard
Copied
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);
}
}
Copy link to clipboard
Copied
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);
}
}
Copy link to clipboard
Copied
Would this not over complicate a simple way to code a keypress event listener, unless you know an easier way to code this
Copy link to clipboard
Copied
What is overcomplicated about it?
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Look into incorporating the hitTestObject() method.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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