actionscript 3: calling function for second time wont work - actionscript-3

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.

Related

Cocos2D-X nodes loading in wrong positions have RemoveAllChildren()

I'm only using one scene object for my entire 2d sidescrolling platformer with two layers: There is a hudLayer for the controller, and the gameLayer for everything in the game.
When I control my character to walk into a door (sprite with a physicsBody that has a callback that takes me to the next level), the next level loads by these instructions:
remove all event listeners
stop all actions for both layers (the gameLayer typically has been doing a Follow action that follows the player, and is limited to the size of the backgroundSpriteNode)
remove all children from the gameLayer
set gameLayer position to Vec2(0,0)
load level 2 (a complete copy of what level 1 should look like... load the background sprite to gameLayer, playerSprite, add event listeners)
add event listeners back to the hudLayer
The only problem is that for whatever reason, the level only partially loads the way it should... For instance, coins appear in the spots they should, but platforms don't seem to. Neither that or the player, or other characters. Not sure really why these objects are specifically not loading in the correct positions, but maybe someone who has experienced this sort of problem before can help?
Most nodes (such as platforms) are appearing below the bottom of the screen when they should be fully shown at the bottom of the screen. Characters are appearing down there too when they should be appearing above the platforms.
P.S. After testing a bit more, I've come to realize that SOMETIMES some of the platforms are loading in the correct positions, but sometimes they aren't. Not sure why. A fix for this may be to load things one at a time with a delay between them. But I'd rather not have to put delays and just figure out what the deal is.
Figured it out: for whatever reason, I needed to add a delay in there. I think for whatever reason, by calling removeAllChildren(), something got messed up with the positions. It was my suspicion since SOME of the time, parts of the next level would load, but only some parts, and only sometimes.
Here's the code I added to run an action on the gameLayer once all children were removed, etc...
runAction(Sequence::createWithTwoActions(DelayTime::create(0.1), CallFunc::create( [&] ( ) {
loadLevel2(this);
})));

Flash Pro: Collision of objects only works correctly when on Frame 1

as you may have noticed just by reading the title it is quite hard to summarize the problem,
which is probably why I couldn't find a solution anywhere.
I am currently working on a flash game using adobe flash pro CC.
I started by creating the first level of my sidescrolling game and got everything to work just as I wanted it to. All my work was on a single frame (frame 1) and on a single layer and it worked fine.
However, when I then went on to move the frame a little further back and added frames in front of it for the menu I encountered a problem.
When I "gotoAndStop" to the frame with my game content on it, everything works except the collision between my character and my objects. My character is now sunk into the "floor object" about have his size, while when i move the frame back to "frame 1" everything works fine again.
I am losing my mind over this. Please help me :(
in flash when you start changing a dynamic property such as x or y, that object will be independent from the keyframe where it originally was ~forever~, even if you go back to the original keyframe. therefore if you start some enterframe and change the playhead afterwards you will get some awkward (but expected) behaviour.
ideally, jump to the game keyframe and just then call whatever methods needed to start the game.

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

symbols placed on the timeline become undefined if stepping backwards

I am using the frames in the timeline of a .swf as pages in a flash app. The user can advance to the next page by clicking a button that takes her to the next frame. Similarly, it is possible to navigate to the previous frame/page as well.
Most of the content is placed on the stage (i.e. created by dragging an instance of a library symbol to the stage) but properties of those instances, such as .visible might be changed via actionscript. Also, some objects are loaded from external flash files and displayed programmatically with addChild / addChildAt.
The problem is, if I am on Frame N+1 and there is an object displayed on the stage programmatically (i.e. with addChild, not by having it placed on the stage) and navigate to Frame N where there is an object that is placed on the stage (i.e. dragged from the library),
then the instance of that object is undefined/null and throws an error if I try to set its properties (like .visible).
The error does not occur if I am moving to the NEXT frame, only if I am moving to the PREVIOUS one. Therefore I assume that some kind of initialization is not getting called while going one frame back.
I was also thinking that the objects would just not "live" to the next timeframe, that is, their value would be lost and re-initialized because of scope, but if there is no dynamically created object on the stage, I can navigate back and forth just fine.
Is there a way to ensure that the objects created on the stage do not disappear while navigating back to the previous frame?
The first, and more useful, part of the answer is this: timeline keyframes and scripts can give conflicting information about display objects - whether they should exist, where they should be, and so on. For example, when you add an item by playing into its frame, and then delete it with script, and then play into its frame again. When this happens, there's no unambiguously correct thing for Flash to do, so it tends to be unpredictable. I believe what generally happens is that once you fiddle with a given object via script, it's considered to no longer pay attention to the timeline - but your mileage will vary.
Having said that, the reason things are different when you play backwards is the second and more arcane part of the answer. Internally Flash functions differently when seeking forward and backwards on the timeline. Flash internally treats keyframes as changes to be applied in the forward direction, so as you play forward, it applies those changes in sequence. When you move backwards, however, from frame N+X to frame N, it doesn't scan through the intervening X frames reversing those changes - it jumps back to frame 1 and fast-forwards along to frame N. Normally, it amounts to the same thing and you don't need to worry about it, but when you get into the twitchy area where scripts and the timeline have a different idea of what should be on the stage, you're liable to see things behave differently depending on which way you jump (as you are now).
The super-short version is, for things to work predictably, try to ensure that any given object gets added, updated, and removed the same way - either all via script, or all via the timeline. When that seems impossible, fiddle with your content structure - usually, the best solution is to change your object into two nested ones, so that the things you want to do with script occur one level higher or lower than the things you want to do with the timeline.
I'm not sure I got your question right, but as3 does not instantiate elements on the timeline as soon as you gotoAndSomething, but later that frame.
That is, you can't
this.gotoAndPlay(10)
this.elementOnTimelineFrame10.DoSomething()
without errors.
I remember using this chunk of code in the past to work around this problem. It uses the Stage.Invalidate() function to wait for an Event.RENDER before trying to access and children, more info (although vague as hell) is here
private function init():void
{
stage.addEventListener(Event.RENDER, stage_renderHandler);
}
private function stage_renderHandler(evt:Event):void
{
// Run your code here
updateChildren();
}
private function enterFrameHandler(evt:Event):void
{
// triggers the RENDER event
stage.invalidate();
}
This also might me very costly (performance wise). I would strongly advise against dynamically adding/removing objects to an existing timeline, is there any way in which you can place an empty Sprite above the timeline animation and use that for all your dynamic content?
Hope this helps

How to make an object be ignored and letting mouse evnets pass through?

Hallo, I been having this problem for a while and I have no idea how to solve it.
I have a flash game (very much like a normal memory game) that has a lot of Movieclips in it that has MouseEvents attached to them. But, when I add a bitmap over the stage (used for covering lots of unwanted things and has to be there) that is the full size of the screen non of my events are fired anymore. The reason is that the overlay bitmap is stealing all of the events.
How can I stop this behavior? Is there a way of letting the events pass through the overlay object? Or for the overlay object to be ignored when it comes to events?
Thanks.
Assuming your overlay is stored in a variable m_overlay, then
m_overlay.mouseEnabled = false;
However you said it is used for "covering lots of unwanted things" so perhaps we need more information on what you are trying to achieve?
I've solved this in the past by creating "proxy" object to capture mouse clicks. The MCs under the bitmap aren't going to receive events.