Restart flash on click - actionscript-3

I'm trying to restart my flash piece with a restart button. I use gotoAndPlay(0), but nothing happens. I'm sure the click event handler is being called because I used a trace statement to verify.
rs.addEventListener(MouseEvent.CLICK, restart);
function restart(event:MouseEvent):void {
gotoAndPlay(0);
}

The first frame is frame 1, not 0.
Not sure why adobe decided against making frames zero-based, but they did :/

If you have added objects to the stage, like buttons or graphics, but never actually used the stage's timeline, the stage will start and stay at the first frame. So 'gotoAndPlay' wont work in this case. It would be only useful to restart an animation anyway, as it won't reset any code on its own.
You need to decide what parts you actually want to reset and what parts you can keep. You probably don't want to remove assets from memory you loaded at the beginning just to download them again. Some objects may be kept, others should be removed.
As far as I know there is no easy way to reset a flash application, other than maybe reloading the whole page. Here are some general steps to 'reset' an application by hand:
Create a method for your initialization code:
object creation, adding to the display list, adding event listeners.
On a click: remove all objects from the stage, remove all their event listeners.
Call the initialization method again.
Ideally you set the references in your init method to a new variable so the old ones can be garbage collected. Depending on the code structure you may have to manually set some to null. Make sure you don't keep any references to objects you don't need any more.

Related

List box is not updating in Windows Phone 8

I am developing one application, in that I have 3 pages. In first page I have List Box with some data and in 3rd page I have application bar for navigating to first page. I will get the list box data from the server. Whenever I am binding the List Box with server data, it is rendering properly but if the user navigate from 3rd page to 1st page using application bar then List box is not updating with fresh data, it is still displaying old data. If user comes to first page using back key press then new data is rendering.
I am using observable collection to bind List Box data and used NavigationService.Naviagte() for navigation.
Help me to resolve this issue.
Thanks in advance.
I am not very sure about your problem because you haven't cleared the whole context. But assuming that your data is refreshing on back key press, I can suggest you use NavigationService.GoBack() instead of NavigationService.Naviagte(). The former method call is equivalent to back key press. NavigationService.Naviagte() creates another instance of the page in the memory while NavigationService.GoBack() takes you back to the previous instance. Hope this helps.
As I understand everything is fine when you start. But the update is not happening upon second return, i.e. Through navigation.
My thought is that all your code to do this is in the constructor, and since the page is not removed from memory the constructor will not be called.
Two solutions move the code to a loaded event. Simply in the constructor write this.loaded += eventname;
Or you could put code in the onnavigatedto event. Write as a new function protected override onnavigatedto.
Putting it in the navigatedto, would probably make your app less responsive if you do server calls. If you have the code in the loaded event then the information will be uploaded when it is done. Which means the user will have a moment with old information. You could then introduce a waiting screen if it is an issue.

Application_Launching timing

What exactly is the timing/thread of the Application_Launching method on WP8? Specifically, in relation to the UI loading/rendering sequence?
I have an app where some global init is being done within Application_Launching. I'm getting a crash report from a method that's called during data binding on the start page's XAML; the crash is consistent with said global init not taking place.
EDIT: I'm calling a native (C++) method which is reading a file into a mallocated memory block in a global variable that's initialized to null. Said variable is dumped as a part of crash reporting; I've got a report where it's null.
Pasting the code would be rather pointless IMHO.
When starting the app, the Launching event is raised. However, the app can later be put in a dormant state, in a process that is called "tombstoning". When a tombstoned app is resumed, it won't raise the Launching event but the Activated event instead. It's very likely that you forgot to handle that case.
To test it easily, go in the properties of your Windows Phone project, in the Debug tab, and check the "Tombstone upon deactivation while debugging" option. From there, every time the app is deactivated while the debugger is attached (typically, when pressing the home button on the emulator), the app will be tombstoned, and you can make sure that it resumes properly when switching back to it.
I've got another theory. It's not about the library being loaded at the wrong time, it's about the library being unloaded. Since almost all of my native functions are static and the state is global, there are no active native objects, and the COM subsystem has a zero ref count on the module. As per COM rules, modules like that are fair game for unloading anytime. On a subsequent native function call, the library is reloaded, but the global state is gone.
From the next version, I'll keep one live native object for the app's lifetime. We'll see if the crash comes back.

Actionscript: SharedObject and addEventListener

Could someone explain when does local SharedObject triggers event handlers added via addEventListener?
I have tried and it doesn't trigger, after flushing.
For example i have two object.swf both in separate browser tabs.
I'm adding data inside object.swf on one tab and want event to be triggered in object.swf from another tab.
Is it possible with native functionality and without remote type of SharedObject?
Ofc i could write infinite loop and check local storage for changes, but it's the last solution i would like to implement. :D
I was reading docs and played with example over there, but it doesn't trigger event, even if it is added before flushing.
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/SharedObject.html
Thanks.
Only remote shared objects dispatch events. If you want to communicate between two swfs, try LocalConnection instead. http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/LocalConnection.html

Design Pattern to require multiple events before executing method?

There are many times that I've needed to execute some code after a number of events have fired, and I've come up with counters and such but I feel there must be a better way.
For example, say five files need to be loaded, after which a UI component will become active.
If I set up a counter that increments each time a file is requested, then decrements each time one has loaded, I run the risk that the first two or three files may somehow get completely loaded before my code gets around to requesting the fourth and fifth, which would mean that my counter would be at zero when I still have two files to load, thus allowing the UI component to be prematurely activated.
There are some cases where you could know the number that need to be loaded before the requests go out, but it's possible that the first file contains the paths (and therefore the number of) files. (And this file-loading scenario is only an example of the pattern I'm trying to explain.)
Does anyone have an elegant solution for this? (Does my description make sense?) Thanks!
You could do something with a task framework like spicelib
Using that as an example
Create a FileRecursionLoadTask which grabs a file and completes when that file and any references it makes are loaded.
Add each FileRecursionLoadTask to a SequentialTaskGroup.
When the TaskGroup is completed, then you know all of the file loads have completed.
There are also plenty of other task frameworks which you might like better. For example, Spring ActionScript also has one.
Before executing a request, store a reference (a unique request uri, the loader object or a special command object) in a list. When a loader has finished, remove that object and call a function that checks if there are remaining active tasks in the list.
This isn't specific to file requests nor request in general, it can be used for anything that needs to wait for multiple actions to finish. Multiple list can be used to process multiple types of action at the same time. The object stored in the list could be implemented as a command object, which could provide more information about the task. This is called command pattern.
If you're doing just loading, like Jacob, I would also suggest a library that handles loading
If the case of a more complicated situation like mixing loaders and other event listeners, I would suggest using an event that fires whenever there is any change to any of the dependencies. In addition all the objects/classes would have a state.
Then I would create a listener adding function for the class that would need to do the function or initiate it, that would have 3 parameters
object with event dispatcher (assuming they all use the same update event) ie. assetLoader
name of object state ie. headerLoaded
state value's desired ie. true
the function would add the listener to a chain of listeners, and any time any of the listeners fires, all objects would check if the state value.
This would allow for regression as well (like when a user presses a button, the content starts loading, but then the user presses cancel, even if all the assets load, the state of one object would be false, thus not allowing the item to complete) If you were using counters, it would be the equivalent to adding instead of subtracting, but much more reliable.
Looking for a design pattern? Try the command pattern (http://johnlindquist.com/2010/09/09/patterncraft-command-pattern/)
(The video is a great example of what command pattern is and how it works - using Starcraft as an example.
The implementation is that you queue your load commands so that they do not execute out of order, and you can add the enable or disable commands to your command que. So the command pattern will play back your commands something like: load, load, load, enable ui item, load, load, enable another item
Good luck

Find all instances in a currentFrame (AS 3.0)

I have a movie with several imported videos which are instances of FLVPlayback. I want to make some Pause and Play buttons. Thus I need to know which video should I refer the stop() method to when I'm in a certain Frame. I believe it is something related to finding all existing instances in this.currentFrame and stopping the found instances. Please, Help!!!
you should be able to loop through numChildren using getChildAt() method.