Keyboard event in as3 executed repeatedly - actionscript-3

I'm new in AS3 language. I'd like to ask does KeyboardEvent in as3 used to executed as long as key is pressed?
It gives me problem when performing a double jump for my character.
Here's some of my code:
var dbJump:Boolean=true;
var onGround:Boolean=false;
var Yvel:Number=0;
var keyUp:Boolean=false;
this.addEventListener(Event.ENTER_FRAME,Eframe);
stage.addEventListener(KeyboardEvent.KEY_DOWN,
kDown)
stage.addEventListener(KeyboardEvent.KEY_UP,
kUp)
function kDown(e:KeyboardEvent):void{
var key:uint=e.keyCode;
if(key==32){
keyUp=true;
}
}
function kUp(e:KeyboardEvent):void{
var key:uint=e.keyCode;
if(key==32){
keyUp=false;
}
}
function Eframe(e:Event):void{
Yvel++;
myChar.y+=Yvel;
while(ground.hitTestPoint(myChar.x,myChar.y,true)){
myChar.y--;
Yvel=0;
}
if(ground.hitTestPoint(myChar.x,myChar.y+3,true)){
onGround=true;
//if touching ground then return the dbJump to true;
dbJump=true;
else{
onGround=false;
if(!keyUp){
//if not on ground an not pressed space, dbJump
// still true
dbJump=true;
}
}
if(keyUp && dbJump && onGround){
Yvel=-12;
}
// if space pressed and not on the ground
else if(keyUp && dbJump && !onGround){
Yvel=-12;
//then say doublejump is completed
dbJump=false;
trace(dbJump);
}
}
so the dbJump variable immediately becomes false in the first space key press, not the second time space pressed. I put the trace so i can see how much is key down event repeated.
So the point is the dbJump immediately became false as soon as my character off the ground. i didn't release the key yet. I tried to press and release the space keyboard quickly,and it works(so i'm really sure it is because the repeated statement of 'dbJump=false' .
How to fix that?
Also, i mean if the dbJump property is false it means cant jump again/doublejump have been completed.

Related

Allow a button press by true or false variable AS3

I have been making an advent calendar in which when you press the button the door opens. I have created a variable to control which date you can open them on called 'AllowOpen' if it is the right date. I have also created a function to go to a frame when clicked.
var AllowOpen: Boolean = false;
if (Day == 1) {
AllowOpen = true;
} else {
AllowOpen = false;
}
button1.addEventListener(MouseEvent.MOUSE_DOWN, click);
function click(event:MouseEvent):void
{
gotoAndStop(2)
}
I can't work out how I would tell the the program to only open the door if allow open is true. I have tried the 'if' statement but it doesn't seem to work. Thanks
OK, you could put the 'if' loop inside the function as already suggested thusly:
function click(e:Event):void{
if (AllowOpen) {
gotoAndStop(2);
}
}
BUT, spidey senses tingling. I would opt for adding an ENTER_FRAME listener so that as the day changes you automatically change boolean values for each day/door to open:
var my_boolean_var_i:boolean = false;
addEventListener(Event.ENTER_FRAME, checkDay);
function checkDay(e:Event):void{
if(Day == day_i){
my_boolean_var_i = true;
}
}
"i" would be another variable that you would declare to store an integer from 1 to 28 depending on the date and then a set of variables 'day + i'. Then you can have a listener/function for each door named sequentially with i to keep everything organized:
door_i.addEventListener(Event.MOUSE_CLICK, click_i);
function click_i(e:Event):void{
if(my_boolean_var_i == true){
gotoAndStop(2);
}
}
This is the way I would go about settling the task. It's a lot of repetitive code so there is bound to be some more elegant way to do this from a more advanced user. Also, if you want to animate the door look up GreenSock TweenLite. VERY useful and friendly for graphics and whatnot.

Using QWER keys to jump to a specific scene in AS3

TLDR: I need code in action script 3 which will let users press keys like QWER to jump to a specific scene.
So what I'm doing is creating and interactive comic within flash, and to be blunt I didn't know any AS3 code before this and still pretty much know none.
So I'm going to need some help on this one and I think it is worth mentioning that I am using sound in this project.
what I need to know is how to use letter keys (eg. QWER) to act as shorts cuts to jump to specific scenes. What I have so far which is working is this and another version which uses a mouse click instead.
stop(); ( 1 )
stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_KeyboardDownHandler);
function fl_KeyboardDownHandler(event:KeyboardEvent):void {
gotoAndPlay(currentFrame+1);
}
and of course all this does is advance the frame, which I do need for some sections of dialogue but that's the basis I've been trying to get it to work.
I do know that q = 81, w = 87, e = 69 and r = 82.
Besides that I've got nothing and I need some help real bad.
You need to check which key has been pressed, you can do it like this:
function fl_KeyboardDownHandler(event:KeyboardEvent):void {
if(event.keyCode == Keyboard.Q){
//do what you need to do when Q was pressed
}else if(event.keyCode == Keyboard.W){
//same for W
}
...etc
}
The KeyboardEvent instance contains data about the event itself, part of it being keyCode, which gives the code of the pressed key. Using this property, you can detect which key the user pressed, and react accordingly.
As you understood, you can use gotoAndPlay() and gotoAndStop() to move around your animation.
It gives us the following code :
stop();
stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_KeyboardDownHandler);
function fl_KeyboardDownHandler(event:KeyboardEvent):void {
if(event.keyCode == Keyboard.Q) {
gotoAndPlay(1); // Back to the start
} else if (event.keyCode == Keyboard.W) {
gotoAndPlay(currentFrame-1); // Back one frame
} else if (event.keyCode == Keyboard.E) {
gotoAndPlay(currentFrame-1); // Forward one frame
} else if (event.keyCode == Keyboard.R) {
gotoAndPlay(100); // Go to 100th frame
}
}
Note that I am using the Keyboard class to get the keyCode associated to a specific key.

as3 how to perfom different actions if button is clicked or not

I am a complete n00b.
I'm working on a game. Basically, I have to perform no action if a button is pressed on a certain keyframe, but if it's not clicked, I need the scene to proceed until a certain point and then go to another frame.
In my stupid head I thought: if I can create an empty global variable I could input "something" when the button is clicked on my frame and check on a latter frame "if globalvar == "something": nothing happens, else: gotoAndPlay(where I need)"
Explanatory image:
Apparently that's not how AS3 works. So what can I do?
import flash.events.MouseEvent;
var pressed = "no";
function work(event:MouseEvent):void
{
pedal.visible = false;
cursor_mc.visible = false;
pressed = "yes"
}
pedal.addEventListener(MouseEvent.CLICK, work);
This is what I've got when the action needs to be performed.
Then I would put (I've never written an if statement in as3)
if (pressed == "no") {
gotoAndPlay(some other frame);
}
This would be put on a complete different frame AND LAYER from the previous so that the part in between is still played.
You got to add a new event, and ENTER_FRAME event to which will fire a function on every frame. There you could check whether the button is pressed. For example:
stage.addEventListener(Event.ENTER_FRAME, updateFunction);
function updateFunction(e:Event){
if(pressed == "no"){
if(currentFrame == frame number when you want to check){
gotoAndPlay(some other frame);
}
}
}

AS3: Multiple options in one key and preventing multiple key presses

I have a "pause function" in my AS3 code, the problem is that i can't figure the logic necessary to give the letter "P" the capacity to pause and unpause and at the same time limit the number of presses to one at a time. My code so far (working ,yes, but without the "one press at a time" limit).
public function PauseDown(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.P)
{
pause = true;
trace ("apreté pausa");
pausa();
}
}
public function pausa():void
{
trace ("pausa");
if (pause == true && paused == false)
{
paused = true;
backgroundLvL1.removeEventListener(Event.ENTER_FRAME,update);
}
else if(pause == true && paused == true)
{
paused = false;
backgroundLvL1.addEventListener(Event.ENTER_FRAME,update);
}
}
Your 'PauseDown' function should probably be called something else, because it's going to be called whenever ANY key is pressed; Something like 'keyPressed' is probably more descriptive.
If you want to have only ONE key acknowledged as the most recently pressed key, I would set a variable (eg _currentKeyCode) to the event.keyCode in the 'keyPressed' function. Then, in your game loop function you can check the value of _currentKeyCode and have the game respond accordingly.
In the 'keyPressed' function you could also still include your test to see if 'P' has been pressed.
Note that you should also have a 'keyReleased' function, triggered on a KeyBoardEvent.KEY_UP event. This could check if _currentKeyCode == the event.keyCode and, if so, set _currentKeyCode to -1. Then, in your game loop, -1 would signify no key presses.
One last, slightly irrelevant, thing: In the 'pausa' function you don't need to check if 'pause == true' because the pausa function is only called when pause == true.

FLASH actionscript 3.0 problems

I want to ask about this problem. I have a movieclip (instance name : char). Inside it I have 2 frames. The first frame contains a movieclip (it does nothing I forgot why I even bother to make it into movieclip). This first frame has frame label "still".
The second frame also contains a movieclip, inside it contains 12 frame. This second first frame has frame label "run"
This is my code
char.gotoAndStop(char.still);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keysDown);
stage.addEventListener(KeyboardEvent.KEY_UP,keysUp);
function keysDown(e:KeyboardEvent):void{
if(e.keyCode == Keyboard.RIGHT)
{
char.gotoAndStop("run");
this. char.scaleX = 1;
}
}
function keysUp(e:KeyboardEvent):void{
if(e.keyCode == Keyboard.RIGHT)
{
char.gotoAndStop("still");
}
}
The problem is, when I press RIGHT ARROW button, It moves, but the movieclip (with frame name "run") can't loop or even play complete from frame 1-12, it only plays from frame 1-9 then stop (not go to frame 10 or even loop)
is there something wrong with my code?
Have a look at how often the KEY_DOWN-Event is actually fired. For example by just tracing.
function keysDown(e:KeyboardEvent):void{
if(e.keyCode == Keyboard.RIGHT)
{
trace("pressed");
char.gotoAndStop("run");
this. char.scaleX = 1;
}
}
You will realize that the event is thrown WHILE the Key is pressed, not only once. You actually call the gotoAndStop("run") repeatedly, which makes your animation mc restart all the time.