If you're running a game of some sort, you might only want the user to be able to play the game once at one concurrent time. One way to stop multiple instances of SWF is by creating a `LocalConnection` in the game.
There are lots of ways to do this, however this is just a simple solution.
Here is some pseudo code:
var connection:LocalConnection = new LocalConnection();
connection.connect("connectionName");
connection.send("connectionName", "instanceRunning");
function instanceRunning(){
connection.send("connectionName", "alreadyRunning");
}
A new code snippet will be posted nearly every day as well as weekly tutorials!
If you have any suggestions for new code snippets and tutorials, please comment them below.
Error #2044: Unhandled AsyncErrorEvent:. text=Error #2095: flash.net.LocalConnection was unable to invoke callback instanceRunning. error=ReferenceError: Error #1069: Property instanceRunning not found on flash.net.LocalConnection and there is no default value.
ReplyDeleteThat is the error I got when I copied and pasted that coding into Flash and ran the movie.
Also, in your coding, if I were to make two frames and put that coding on the first frame, where would I put the gotoAndStop(2) command to go to the next frame if there are two or more SWF instances running at the same time?
You need to set a client for the LocalConnection...
ReplyDeleteLocalConnectionInstance.client = { instanceRunning:instanceRunningFunction }
Please note that the above is pseudo code, not complete
Could you post an example of a complete code? The code I'm using isn't working right. This is the coding I have in my test file:
ReplyDeleteimport flash.net.*;
import flash.events.*;
stop();
var localCon:LocalConnection = new LocalConnection();
localCon.client = this;
localCon.connect("con1");
localCon.send("con1", "instanceRunning");
function instanceRunning():void {
localCon.send("con1", "alreadyRunning");
}
function alreadyRunning():void {
trace("alreadyRunning");
gotoAndStop(2);
}
closeConnectionBtn.addEventListener(MouseEvent.CLICK, closeLocalConnection);
function closeLocalConnection(evt:MouseEvent):void {
localCon.close();
}
Can you tell me what to do to make it so it goes to the second frame if there are two or more instances open, and to stay on the first frame if only one instance is open?
Why are you using stop();?
ReplyDeleteI have two frames in the Flash file. The first frame shows the text "1 instance is running", the second frame shows "2 or more instances are running."
ReplyDeleteIf you pastebin your code at http://paste.hostingforoz.com/ i can help you with that
ReplyDeletehttp://pastebin.com/Rx2S4V1C
ReplyDeletehttp://paste.hostingforoz.com/index.php/view/a9bf8a90
ReplyDeleteOkay thanks, i'll post a full example soon
ReplyDeleteAlright, cool. Thanks dude!
ReplyDelete