keyboard input to movieClip actionScript 3.0 - actionscript-3

I have two alternatives to move a space ship movieclip.
First one is adding eventListener(keybordEvent) check which button is pressed and act according to it.
The second one is again,adding an eventListener(keyboardEvent) and using 4 boolean varaibles,leftArrow,rightArrow,upArrow and downArrow. keep track of pushed buttons and use eventListener(Event.ENTER_FRAME) and move movieClip in everyframe using these bool variables)
Which one is better or at least does any of them have an advantages ?

I suggest the second option because you want to handle movement in the game loop and not in the key event.

I would use the Boolean method as the other method (depending how you implement it) could react to the key repeat that happens when you press a key down - it triggers once, pauses, then continues to fire.
EDIT
..and, I agree, also for the best practices reason Nambew mentions!

Related

actionscript 3: calling function for second time wont work

I'm new to actionscript 3, I have sequences of frames and two buttons to control which sequence to play, it first works properly, but have problem when a sequence is being played for the second time. I have used gotoAndPlay function for my navigation. can anyone help me?
From your description I have a hunch about what may be happening...
Firstly I would ask you if the buttons are present at all frames along the timeline? If they are not (ie, sometimes the timeline shows a frame where the buttons are not present before returning to them ) you should realise that when they come back into view again, they are not the same buttons as the ones from before. That means that the event listeners you attached the first time are not going to respond to clicks on these new buttons.
This happens because flash always totally recreates timeline objects when they come into view again. Flash can sometimes cope with a "jump" over a "gap" when a symbol is the same, but this is extremely unreliable and should be avoided for this reason.
You can avoid this problem by keeping the ui on the stage at all times, and revealing and hiding the buttons when you need them. Even better, create an instance of your ui in code and add it to the stage when you need it. This way you know there is only one instance, and you are in control of it.

Pressing space does weird things

I've made a simple game in flex. You control falling blocks and your goal is to eliminate viruses. It's almost a copy of the 90s game dr Mario. I've made it so you control blocks with the arrow keys and you spin the block with space. Everything works fine as it should when playing. However when i switch to another program and the application is out of focus and i get back to the game, whenever i press space the game restarts. It's like it calls a function that reinitializes the game and resets all the variables to the start values.
The game is made with several NavigationContent components that acts like scenes. The game doesn't go back to the start screen when i press space, it just resets the game. Wich is really weird.
Are there any default method that is called that causes this behavior? Anyone have a clue?
EDIT: The issue arises - as it seems - exclusively when i tie a function to the space key (keyCode 32). I solved the issue by rebinding the key to "CTRL". But still it would be great to know what's up with the SPACE key. The game works fine with space if i use Internet Explorer. Other browsers doesn't work with the space key. It's the same issue with all of them.
EDIT: This is how the event listener looks:
this.stage.addEventListener(KeyboardEvent.KEY_DOWN, moveBlocksKeyboardEvent);
Even if i comment out all the code in the moveBlocksKeyboardEvent method the game still restarts. It's exclusively when hitting the SPACE-key. If i hold down the key the blocks spin. It's when i release the space button the game restarts. As if it's some reinitialization method tied to the KEY_UP event or something.
This type of behaviour is often tied to a null or undefined value, causing a nonsense-code jump which then results in a reset.
Make sure that that event handler for key down is attached to a valid object; if you are using "stage" then make sure it exists. When you move out of focus, the event handler may be left associated with a null object; when you reenter, it doesn't exist anymore, and therefore you get the reset behaviour.
This thread may help provide more detail:
Adding a key listener in Action Script 3

How To Use "Direct Input" Instead of Keyboard Events In ActionScript 3?

As you may know in "real" games you need to check if a key is pressed at this time. Right now I'm using events and I remember if last event was a key-up or key-down to know if a button is held down.
BUT I'm experiencing some lags (not network) in the game when several buttons are held down. New key pushes are recognized a little bit too late. From DirectInput in DirectX and from LWJGL I know how nice and smooth input for games can work.
a) So I ask: Is there a way to check keys directly in ActionScript 3 ? I don't think any other extra package would be useful since it would just do what I do right now.
b) If no. Why wouldn't Adobe add direct input feature to Flash since it is used for games to a high percentage ?!?

Can two buttons with the same class instance act differently in Flash?

Say I have this specific button class in Flash called cont_button and it's supposed to be used to break out of a loop, but I want to use the class more than once. Is there a way to give every instance of this class some kind of parameter so that it knows which frame it nees to go to?
Example:
I have an instance of cont_button on frame 200 and there's a loop between 200 and 210. This cont_button executes a gotoAndPlay(211). But later on I have another instance of the button on frame 315 and a loop between 315 and 325. Is there a way to make it so each instance knows which frame it specifically needs to go to via the use of a variable? Or am I going to have to make an actionscript file for each individual one?
Pretty new to ActionScript so I appreciate the help and if there are good coding references to AS3 you guys recommend, I'll gladly look those over.
Sure, this is possible. One way you can do this is make the frame numbers class variables and when the button is clicked, they reference whatever value is stored in them, rather then hardcoded numbers. To get a better idea, can you post the relevant parts of your button class?
As you say, you need to pass a parameter to each instance of the button. There are lots of different ways you could do this, but I'd be tempted to just do it via the instance names.
You could name each button loopBreakTo211, loopBreakTo326 and so on, then in your button's class have:
var breakFrame:Number = Number(name.replace("loopBreakTo", ""));
(parent as MovieClip).gotoAndPlay(breakFrame);
Admittedly that's not a very robust way of doing it (for example, it will break if a button is named incorrectly and breakFrame ends up as NaN, so you might want to add a check for that), but it keeps the parameter together with the instance instead of in the timeline somewhere.

AS3 Detecting Multiple Keyboard Keys Pressed

Is there is a way for me to detect if say 'a' and 'b' are pressed together using AS3.
If they are I can then make a movieclip visible.
Seems simple but I can't seem to find this out.
Just use Senocular KeyObject class to detect if a key is down or not. Listen for the KeyboardEvent.KEY_DOWN event on the stage, and see if both your keys are down.
That class has a small example usage in the header comments.