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
Related
Will make this brief, I have a game map with units on it and had finalized a fully interactive minimap where the units on the minimap have event listeners for rollover/rollout (displays a small popup unit data summary) and click (selects the "real" unit on the main game map and scrolls the viewpoint to that location). All done, tested, working.
I then implement an interactive scrollable unit list with more status summary data and dozens of objects with rollover/rollout/click listeners. All tested and working fine.
Then I go back and look at my minimap, and the listeners on the mini-ships aren't working anymore. Things tried:
Debug code to make sure listeners still being added
Debug to watch the one place where I remove those listeners to make sure that ain't happening unexpectedly
Debug to watch all the places I refresh that dialog to make sure every iteration adds the listeners back
Can't see that there is any transparent object on top intercepting
Checked mini-ship parents to make sure I didn't turn off mouseChildren or something like that somewhere
No added stage-level listener, in fact I killed all of them temporarily to test this
What happens when I debug with a breakpoint on the mini-ship listener handler is nada. It's no longer receiving mouse events. So either something I haven't thought of has stopped them from listening or something I don't know of is intercepting.
So what is the strategy here? How can I find the break in the chain?
Well knowing what the actual problem was certainly gives us the advantage of hindsight... that being said, you could have detected the error by adding a trace call inside your function that adds the listener and another one inside your function that removes it. Then you would have seen that it isn't getting re-added. Or you could set break points there.
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.
I am developing an kiosk-like application (a game) which needs to be locked in full screen all the time. I am using as3/flash/AIR for it. Things started well at first, and for the most part all works fine.. but there is a mystery brewing somewhere which I haven't been able to figure out... That's where your help would be greatly appreciated!
The way I handled this problem is by adding at the very beginning of the app:
stage.addEventListener(KeyboardEvent.KEY_DOWN, playerOnKeyDown);
Then, on my playerOnKeyDown function:
function playerOnKeyDown(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.ESCAPE)
{
event.preventDefault();
//More code here opening out menus, etc, etc.)
}
}
So, all of this worked just fine, but of course I needed to also bring along:
stage.focus = stage;
into the party, otherwise, when removing objects - as in removeChild() - the event firing wouldn't behave as I wanted, because flash changed the focus elsewhere in the display list.
I have been careful to add the focus to the stage every time a remove a "child", and it works great everywhere, except for one time in the entire run, right after I remove an object from an externally loaded swf.
I still add the lines as it should be expected to work:
removeChild(childFromLoadedSWF);
stage.focus = stage;
except that when I hit the any key, the event won't trigger my function, and if I hit the ESC key, it takes me out of full screen (its default behavior), once again, circumventing completely my listener function playerOnKeyDown.
The strange thing is that right before doing this, the line:
stage.hasEventListener(KeyboardEvent.KEY_DOWN))
traces true!
The focus is on the stage, the listener is on, and yet when pressing the ESC key the default behavior is ignoring my function completely....
What could be causing this?
THANK YOU!!
Looks like I just needed to remove an ENTER_FRAME listener I had. That was wreaking havoc with the stage.focus. Now everything works great!
stage.removeEventListener(Event.ENTER_FRAME, onEnterFrame)
For anyone looking into using AS3 to develop desktop games for mac or PC (as in Steam), this is great.
the game Machinarium must have done something like this, because the ESC key never causes this effect, event though it was made in flash.
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 ?!?
I'm working on a game that uses four simultaneous key presses. It all works fine, except that when the keys are rapidly pressed, my framerate slows down significantly (if I hammer even just one of the keys, I can halve the framerate).
I initially just assumed that there was too much going on in the method that the key press triggers, but if I take the code out of the method completely, the slowdown still occurs.
Has anyone run into this before? The keypress is one where you hold the key down, so it's repeatedly firing a method call every frame, but this is pretty standard for many uses and I've never encountered this before.
EDIT: clarification.
Structurally, there's a KEY_DOWN and KEY_UP listener attached to the stage:
stage.addEventListener(KeyboardEvent.KEY_DOWN, menuKeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, menuKeyUp);
which then calls a method with a single switch statement in, that contains five lines like this:
case ONE: pressing1 = true; break;
There's an ENTER_FRAME event that checks to see if any of the boolean flags are true, and handles character movement. This calculation happens regardless of key presses (i.e. if you let go of the keys, gravity still has an effect). This holds 60fps easily.
The issue is literally at the exact moment the key is pressed or released, there's an almost imperceptible frame drop. Repeatedly pressing the keys causes the framedrop to get worse and worse. Holding a key down doesn't kill the framerate, apart from the single split second frame drop when the key is pressed down. After that the game carries on as normal. Letting go of the key causes another tiny hitch and then the framerate goes back to normal.
EDIT 2 - I added a framerate checker so I could see exactly what was happening to the framerate. Interestingly, I can't make it go any lower than EXACTLY 30fps even when I press keys very rapidly. Is there some sort of restriction in play here with Flash Player?
Turns out that the code was correct all along. Playing 60FPS content in the debug player or the standalone player causes any events (mouse and keyboard) to hitch the framerate. Viewing the same content in a browser, or exported to AIR, stops the issue entirely. The content is now running perfectly at 60fps without any slowdown.
Hope this helps someone, I was tearing my hair out!
Do you have multiple different listeners, or one listener that appropriately routes the keypress? I would suspect the former. The solution is to switch to the latter.
What I typically do is have one object that's responsible for listening to key presses and translating keyboard events into other, more meaningful events.
For example:
protected function handleKeyboardEvent(e:KeyboardEvent):void {
if (e.ctrlKey) {
switch (e.keyCode) {
case Keyboard.A:
eventBus.dispatchEvent(new Event(ViewEventKind.SELECT_ALL));
return;
case Keyboard.Y:
eventBus.dispatchEvent(new Event(ModelEventKind.REDO));
return;
case Keyboard.Z:
eventBus.dispatchEvent(new Event(ModelEventKind.UNDO));
return;
}
}
}