dispatch change value and start tween - actionscript-3

I try to make simple scorebug. It’s feed from external XML file.
I want to start an animation when score changing, but i doesn’t found event listener or dispatcher to get change of value.
var myTimer:Timer = new Timer(100);
myTimer.addEventListener(TimerEvent.TIMER, timerListener);
myTimer.start();
function timerListener (e:TimerEvent):void
{
var myXMLLoader:URLLoader = new URLLoader();
myXMLLoader.load(new URLRequest("Xml.xml"));
myXMLLoader.addEventListener(Event.COMPLETE, processXML);
function processXML (e:Event):void
{
var myXML:XML = new XML(e.target.data);
ShotClock.text = myXML.timestamp;
GameClock.text = myXML.Clock;
HS.HomeScore.text = myXML.HomeScore;
AS.AwayScore.text = myXML.AwayScore;
Period.text = myXML.Period;
AwayTeam.text = myXML.AwayName;
HomeTeam.text = myXML.HomeName;
}
if ( (myXML.HomeScore).CHANGE )
{ var myTween:Tween = new Tween(HS.HomeScore, "alpha", Strong.easeIn, 0, 1, 1, true); }
}

You need to change the logic of loading. Instead of starting timed loadings, which won't be accurate 100 ms anyway, could lag, could fail, could arrive in order other than you issued them, etc., you need a single-thread asynchronous loop that does, simply, the following:
Start data loading.
(async pause)
Handle the loaded data.
Get XML data from loaded text.
Parse XML attributes.
Check if the score variable changed and trigger the things you want to.
Wait 100 ms.
(async pause)
Go to step №1.
Something like that:
var theLoader:URLLoader;
var theScore:Number = 0;
var theTimer:Timer;
// Start the tracking routine.
trackNext();
function trackNext():void
{
theLoader = new URLLoader;
theLoader.addEventListener(Event.COMPLETE, onXML);
// Error tracking is a must or the logic flow
// might just stop when you least expect it to.
theLoader.addEventListener(IOErrorEvent.IO_ERROR, onError, false, 0, true);
theLoader.load(new URLRequest("xml.xml"));
}
function onXML(e:Event):void
{
// Sanity check. There should be only one valid URLLoader instance.
if (e.target != theLoader)
{
return;
}
var X:XML;
try
{
X = new XML(theLoader.data);
}
catch (fail:Error)
{
// Processing the case where data is loaded successfully,
// but it is not a valid XML String.
onError(e);
return;
}
// Here's the place for your code
// that extracts data from XML.
// ...
// Get the new score value.
var aScore:Number = X.HomeScore;
// Compare it to previous value.
if (aScore != theScore)
{
// ATTENTION!!! THIS IS THE PLACE YOU ARE LOOKING FOR!!!
// Trigger things if the new score is any different.
// ...
// Keep the new score value.
theScore = aScore;
}
finishLoading();
}
// This method just ignores an error, think of it
// as of blank to process the exceptional cases.
function onError(e:Event):void
{
// Sanity check. There should be only one valid URLLoader instance.
if (e.target != theLoader)
{
return;
}
finishLoading();
}
// Call it finishLoading(true) to stop the tracking routine.
function finishLoading(thenstop:Boolean = false):void
{
// Dispose of the URLLoader instance if any.
if (theLoader)
{
var aLoader:URLLoader = theLoader;
theLoader = null;
aLoader.removeEventListener(Event.COMPLETE, onXML);
try
{
aLoader.close();
}
catch (fail:Error)
{
// Do nothing about it.
}
}
finishTimer();
if (thenstop)
{
return;
}
// Wait 100 ms to give Flash Player a breather
// before starting to load the file once more.
theTimer = new Timer(100, 1);
theTimer.addEventListener(TimerEvent.TIMER, onTime);
theTimer.start();
}
function finishTimer():void
{
// Dispose of the Timer instance if any.
if (theTimer)
{
theTimer.removeEventListener(TimerEvent.TIMER, onTime);
theTimer.stop();
theTimer = null;
}
}
function onTime(e:TimerEvent):void
{
// Now go for the next iteration.
finishTimer();
trackNext();
}

Related

Can anyone help me how to access a data from an fla file to another fla file?

Can anyone help me how to access a data from an fla file to another fla file?
I have a starting fla file called "savescore.fla" which is connected to the class called "score.as".I have a "btnAdd" button that if clicked, it will add by one to the variable "scoring" in the class "score.as". I have also a movieclip "btnSave" that would save the score. Lastly, I have a "next_btn" button that would go directly to my another fla file
which is "finalscore.fla". I wanted the "finalscore.fla" to show the current "scoring" variable from the "score.as" class but the problem is that it will only show 0/zero as the score. I think that as "savescore.fla" saves and edits the variable in the "score.as", it will only make changes to itself and not to the "score.as" that's why I still got 0 as the final score because it comes from the "score.as" and not from "savescore.fla". So I was thinking if I could just access the score from the "savescore.fla", it would solve the problem. But I have no idea what codes to put...or can you give me another way?
Here are the codes: (Btw, I am using actionscript 3.0)
savescore.fla:
var lol:score = new score();
var saveDataObject:SharedObject;
//var currentScore:int;
init(); // this line goes directly beneath the variables
function init():void{ // call once to set everything up
saveDataObject = SharedObject.getLocal("test"); // give the save data a location
btnAdd.addEventListener(MouseEvent.CLICK, addScore); // clicking on +1
btnSave.addEventListener(MouseEvent.CLICK, saveData); // clicking on Save
next_btn.addEventListener(MouseEvent.CLICK, sunod); // clicking on +1
if(saveDataObject.data.savedScore == null){ // checks if there is save data
trace("No saved data yet."); // if there isn't any data on the computer...
saveDataObject.data.savedScore = lol.scoring; // ...set the savedScore to 0
} else {
trace("Save data found."); // if we did find data...
loadData(); // ...load the data
}
}
function addScore(e:MouseEvent):void{
lol.scoring+=1; // add 1 to the score
}
function saveData(e:MouseEvent):void{
saveDataObject.data.savedScore = lol.scoring; // set the saved score to the current score
trace("Data Saved!");
saveDataObject.flush(); // immediately save to the local drive
trace(saveDataObject.size); // this will show the size of the save file, in bytes
trace(lol.GetFullScore());
}
function loadData():void{
lol.scoring = saveDataObject.data.savedScore; // set the current score to the saved score
trace("Data Loaded!");
}
function sunod(event:MouseEvent):void{
var ldr:Loader=new Loader();
ldr.load(new URLRequest("finalscore.swf"));
addChild(ldr);
}
score.as:
package{
public class score{
public var scoring:int;
function score()
{
}
public function SetScore(val:int):void
{
scoring = val;
}
public function GetFullScore():int
{
return scoring;
trace(scoring);
}
}
}
finalscore.fla:
var lol1:score = new score();
txtScore.text = ("Score: " + lol1.GetFullScore()); // set the text property of the txtScore
trace(lol1.GetFullScore());
Once you have loaded an external swf you can grab a reference to it in an onLoadedHandler. So in savescore:
var myReference:Object;
function loadComplete(event:Event):void {
myReference= event.target.content;
myReference.lol1.GetFullScore();
... or...
var myScore = event.target.lol1.GetFullScore();
etc...
}
you'll want to make sure the reference exists before you use it
EDIT
in your Child SWF create a variable and method to store the parent:
var parentSWF:MovieClip;
function setParent(myParent:MovieClip):void{
parentSWF = myParent;
}
then in the parent send the child a reference :
function loadComplete(event:Event):void {
myReference= event.target.content;
// use the reference to child to call
// any variables or functions in it
myReference.lol1.GetFullScore();
// set the reference in child with the following
// so that the child can call the parent
myReference.setParent(this);
}
then in child you can use lol :
parentSWF.lol;
If you had to go this way you would ideally create measures to check the objects/methods etc exist before they are used
FINAL EDIT
For a start, move
var ldr:Loader=new Loader();
out of the sunod function and put it at the top with the other declaration so that loadcomplete can use it. You need to set up a listener for the loadComplete handler. Put this after your other listeners
ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete);
in your loadcomplete handler in savescore add
myReference.doThisWhenLoaded();
in finalscore put
function doThisWhenLoaded():void{
txtScore.text = ("Score: " + parentSWF.lol);
}
This just seems complete overkill and I'm sure I've overcomplicated your over complications(!). My advice would be to do away with the second swf and just create a finalscore.as object in savescore. This would be so much easier. At the moment I can't see why you want to have another swf floating around. I've never been a fan of multiple swfs though so someone else may well jump in and correct this.
Here's the changes of my code:
savescore.fla:
var lol:score = new score();
var saveDataObject:SharedObject;
init(); // this line goes directly beneath the variables
function init():void{ // call once to set everything up
saveDataObject = SharedObject.getLocal("test"); // give the save data a location
btnAdd.addEventListener(MouseEvent.CLICK, addScore); // clicking on +1
btnSave.addEventListener(MouseEvent.CLICK, saveData); // clicking on Save
next_btn.addEventListener(MouseEvent.CLICK, sunod); // clicking on +1
if(saveDataObject.data.savedScore == null){ // checks if there is save data
trace("No saved data yet."); // if there isn't any data on the computer...
saveDataObject.data.savedScore = lol.scoring; // ...set the savedScore to 0
} else {
trace("Save data found."); // if we did find data...
loadData(); // ...load the data
}
}
function addScore(e:MouseEvent):void{
lol.scoring+=1; // add 1 to the score
}
function saveData(e:MouseEvent):void{
saveDataObject.data.savedScore = lol.scoring; // set the saved score to the current score
trace("Data Saved!");
saveDataObject.flush(); // immediately save to the local drive
trace(saveDataObject.size); // this will show the size of the save file, in bytes
trace(lol.GetFullScore());
}
function loadData():void{
lol.scoring = saveDataObject.data.savedScore; // set the current score to the saved score
trace("Data Loaded!");
}
function sunod(event:MouseEvent):void{
var ldr:Loader=new Loader();
ldr.load(new URLRequest("finalscore.swf"));
addChild(ldr);
}
var myReference:Object;
function loadComplete(event:Event):void {
myReference= event.target.content;
myReference.lol1.GetFullScore();
myReference.setParent(this);
}
finalscore.fla:
var lol1:score = new score();
var parentSWF:MovieClip;
txtScore.text = ("Score: " + parentSWF.lol); // this line has the error because of parentSWF.lol
trace(lol1.GetFullScore());
function setParent(myParent:MovieClip):void{
parentSWF = myParent;
}
score.as:
package{
public class score{
public var scoring:int;
function score()
{
}
public function SetScore(val:int):void
{
scoring = val;
}
public function GetFullScore():int
{
return scoring;
trace(scoring);
}
}
}

AS3 multiple file download, check when completed?

I am looping through a small array of file names to grab from a remote server and save locally. This bit works OK.
The problem is, I need to run my next function when the downloads are complete.
Currently, my function, lets call it step2(), gets called before all the FileStreams can finish (infact, its called before that function even starts!)
I read that there is a complete event listener when using openAsynch, but I need to check that all files, e.g. 3 remote swfs) have been written and then start step2() function.
How can I go about this?
Code below:
function onPlaylistComplete(e:Event)
{
var myArray:Array = new Array();
for each(var i in list_of_files)
{
myArray.push(i);
}
for(i = 0; i < myArray.length; i++)
{
swfItem = myArray[i];
var urlString:String = site_url + myArray[i];
var urlReq:URLRequest = new URLRequest(urlString);
var urlStream:URLStream = new URLStream();
urlStream.addEventListener(Event.COMPLETE, urlStreamComplete(swfItem));
urlStream.load(urlReq);
}
function urlStreamComplete(swfItem):Function
{
var downloadSwf:Function = function (event:Event):void {
var fileData:ByteArray = new ByteArray();
var stream = event.target;
stream.readBytes(fileData, 0, stream.bytesAvailable);
try {
var f : File = File.documentsDirectory.resolvePath(swfItem);
var fs : FileStream = new FileStream();
fs.addEventListener(Event.COMPLETE, fileCompleteHandler);
fs.openAsync(f, FileMode.WRITE);
fs.writeBytes(fileData);
}
catch (err : Error) {
trace(err.message);
}
}
return downloadSwf;
}
function fileCompleteHandler(e:Event):void {
e.target.close();
}
step2(); // this seems to run before UrlStreamComplete()
}
Problem with your for loop it will never work with Async model. so better need work to with recursive function.
downloadAllFiles method second arg that is function called when all download are completed.Then you can call step2() or whatever may be.
Make sure if any problem while download file like IOErrorEvent,etc...
downloadAllFiles(myArray,function():void
{
step2();
});
function downloadAllFiles(myArray:Array, complete:Function):void
{
if(myArray && myArray.length == 0)
{
if(complete)
complete.call();
return;
}
swfItem = myArray.shift();
var urlString:String = site_url + swfItem;
var urlReq:URLRequest = new URLRequest(urlString);
var urlStream:URLStream = new URLStream();
urlStream.addEventListener(Event.COMPLETE, function (event:Event):void
{
var fileData:ByteArray = new ByteArray();
var stream = event.target;
stream.readBytes(fileData, 0, stream.bytesAvailable);
try {
var f : File = File.documentsDirectory.resolvePath(swfItem);
var fs : FileStream = new FileStream();
fs.addEventListener(Event.CLOSE, function(closeEvent:Event):void
{
downloadAllFiles(myArray,complete);
return;
});
fs.openAsync(f, FileMode.WRITE);
fs.writeBytes(fileData);
}
catch (err : Error)
{
trace(err.message);
}
});
urlStream.load(urlReq);
}
Can't you just move step2(); at the end of fileCompleteHandler function?
Because at this point step2(); is called right after going through for(i = 0; i < myArray.length; i++)
loop (Which is way to early).
What you are missing is a way to check that all files from your list are loaded, 3vliguy is right and your step2(); should be in the complete handler but, not alone, you have to also test if ALL files are loaded. you can do it in various ways, e.g. use file counter and when it is 0 (all files loaded) you will execute step2() else you will ignore it and just decrease counter.
p.s. the Raja Jaganathan way will work as well, only it is queued loading whereas with the counter you can start loading of all files at the beginning.

Flash Error #1063 Argument count mismatch AS3

I'm trying to mix two tutorials in one game. Level 3 is previously used in a action script file but I transferred it into a normal AS3 timeline.
I get this error:
ArgumentError: Error #1063: Argument count mismatch on adventure_fla::MainTimeline/newObjects(). Expected 0, got 1.
at flash.events::EventDispatcher/flash.events:EventDispatcher::dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at flash.utils::Timer/flash.utils:Timer::tick()
This is the code... sorry if its messy.
const speed:Number = 5.0;
var nextObject:Timer;
// array of objects
var objects:Array = new Array();
function initGame():void{
player.x=200;
player.y=400;
stage.addEventListener(MouseEvent.MOUSE_MOVE,movePlayer);
Mouse.hide();
player.gotoAndStop("arrow");
setGameTimer();
newObjects();
addEventListener(Event.ENTER_FRAME, moveObject);
}
function movePlayer(e:MouseEvent):void{
player.x=mouseX;
e.updateAfterEvent();}
function setGameTimer():void {
trace("GAME TIMER STARTED");
var gameTimer:Timer = new Timer(40000, 1);
gameTimer.addEventListener(TimerEvent.TIMER_COMPLETE, endGame);
gameTimer.start()
}
function endGame(e:Event):void {
trace("Game Over");
// remove all objects
for (var i:int=objects.length-1; i>=0; i--) {
removeChild(objects[i]);
objects.splice(i, 1);
} }
function setNextObject():void {
nextObject = new Timer(1000, 1);
nextObject.addEventListener(TimerEvent.TIMER_COMPLETE, newObjects);
nextObject.start();
function newObjects():void{
//create next object
// array of good and bad objects
var badObjects:Array = ["Bad_1", "Bad_2"]
var goodObjects:Array = ["Good_1", "Good_2"]
// create random number
if (Math.random() < .5 ) {
//based of treat object length
var r:int = Math.floor(Math.random()*goodObjects.length);
// get the treat object by name and cast it as its own class in a temporary variable
var classRef:Class = getDefinitionByName(goodObjects[r]) as Class;
// now we can make a new version of the class
var newObjects:MovieClip = new classRef();
// dynamic var (because mc is an object) typestr it as good
newObjects.typestr = "good";
} else {
// for bad same as above
r = Math.floor(Math.random()*badObjects.length);
var classRef:Class = getDefinitionByName(badObjects[r]) as Class;
var newObjects:MovieClip = new classRef();
// typestr it bad
newObjects.typestr = "bad";
}
// random pos
newObjects.x = Math.random()* 500;
newObjects.scaleX = newObjects.scaleY = .4;
addChild(newObjects);
// push it to array
objects.push(newObjects);
// create another one
setNextObject();
}
function moveObject(e:Event):void {
// cycle thru objects using a for loop
for (var i:int=objects.length-1; i>=0; i--) {
//move objects down based on speed
objects[i].y += speed;
objects[i].rotation += speed;
// if objects leaves the stage
if (objects[i].y > 400) {
removeChild(objects[i]);
objects.splice(i, 1);
}
}
}
newObjects does not take any arguments, but it's used as an event listener (which requires it to take the event object).
It should probably look something like function newObjects(event:TimerEvent):void.
A function used as event listener should accept one parameter of type Event, depending on what class of events it listens to. You are listening to an event of a TimerEvent class, so yes, declare parameter as TimerEvent. To add a function that does not need parameters passed to it as an event listener, use default value construction like this:
function newObjects(event:TimerEvent=null):void {...}
The "=null" addition will allow you to pass no parameters to your function, and the declared parameter will allow you to not get exceptions when it will be called as an event handler.

ActionScript 3: How to remove EventListener with anon functions

I have written code as follows.
Problem is that I can't remove Event.COMPLETE event listener and when I call the loadData function twice or more, it works 2 times or more. Sorry for my bad english and worse explanation but I need to fix it today and I don't know what to do.
I think the code is pretty obvious. please help!
var ldr:URLLoader = new URLLoader();
function loadData(text_place, scrollbar, fileURL:String):void {
text_place.wordWrap = true;
var f:TextFormat = new TextFormat();
f.align = TextFormatAlign.RIGHT;
text_place.setTextFormat(f);
ldr.dataFormat = URLLoaderDataFormat.TEXT;
ldr.load(new URLRequest(fileURL));
ldr.addEventListener(Event.COMPLETE, function ldr_complete(evt:Event){
initText(text_place, ldr.data, scrollbar);
});
ldr.addEventListener(IOErrorEvent.IO_ERROR, loadError);
}
function initText(text_place:TLFTextField, fileContent, scrollbar):void {
ldr.removeEventListener(IOErrorEvent.IO_ERROR, loadError);
text_place.htmlText = "";
text_place.tlfMarkup = fileContent;
scrollbar.update();
trace("Data loaded");
}
function loadError(e:IOErrorEvent):void {
trace("Error loading an external file.");
}
just avoid writing function enclosures and extend the scope of the complete function's passed arguments so it can access them.
var ldr:URLLoader = new URLLoader();
var text_place:TextField;
var scrollbar:Object; //or whatever it is
function loadData(text_place, scrollbar, fileURL:String):void
{
var f:TextFormat = new TextFormat();
f.align = TextFormatAlign.RIGHT;
text_place.wordWrap = true;
text_place.setTextFormat(f);
scrollbar = scrollbar;
ldr.dataFormat = URLLoaderDataFormat.TEXT;
ldr.load(new URLRequest(fileURL));
ldr.addEventListener(IOErrorEvent.IO_ERROR, loadError);
ldr.addEventListener(Event.COMPLETE, loadComplete);
}
function initText(text_place:TLFTextField, fileContent, scrollbar):void
{
removeLoaderEventListeners();
text_place.htmlText = "";
text_place.tlfMarkup = fileContent;
scrollbar.update();
trace("Data loaded");
}
function loadError(e:IOErrorEvent):void
{
removeLoaderEventListeners();
trace("Error loading an external file.");
}
function loadComplete(evt:Event):void
{
removeLoaderEventListeners();
initText(text_place, ldr.data, scrollbar);
}
function removeLoaderEventListeners():void
{
ldr.removeEventListener(IOErrorEvent.IO_ERROR, loadError);
ldr.removeEventListener(Event.COMPLETE, loadComplete);
}
if you want to stop listening for an event after it triggered, you can unregister the anonymous listener in itself:
ldr.addEventListener(Event.COMPLETE, function(event:Event):void
{
event.target.removeEventListener(event.type, arguments.callee);
// ... do whatever you need to do here
});
But if you also want to stop listening for other events from the same dispatcher when it completes, such as your IOErrorEvent.IO_ERROR listener, you'd still need a reference to that listener to remove it.
There is a simpler way. Instead of removing event listeners, close the loader.
ldr.close();
Per the documentation:
Closes the load operation in progress. Any load operation in progress
is immediately terminated. If no URL is currently being streamed, an
invalid stream error is thrown.

AS3: Shared Objects Issue

Slight problem i have been pondering on, i have a class that loads a text from a url as a means to sync the time.
The swf file can be on the same page multiple times, but i want it to check to see if one of the swf is sync'ing, if so then wait for it to complete and load its sync'd value (rather then query the url itself), so that they are all sync'd to the same value (of the 1st swf file to load).
I am trying this using Shared Objects, which looks a little like this (snippets):
public function sync():void
{
sharedObject = SharedObject.getLocal("synctime", "/");
trace(sharedObject.data.startOfRequest, sharedObject.data.endTime);
if ( ((new Date().getTime() - 10000) < sharedObject.data.startOfRequest) && (sharedObject.data.endTime !== undefined))
{
loadUTCFromSharedObject();
}
else if ( ((new Date().getTime() - 10000) < sharedObject.data.startOfRequest) && (sharedObject.data.endTime == undefined ) )
{
timer.addEventListener(TimerEvent.TIMER, loadUTCFromSharedObject);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, startSync);
timer.start();
} else {
startSync()
}
}
private function startSync(e:Event = null)
{
var syncRequest:URLRequest = new URLRequest(TIMEURL + "?cb=" + Math.random());
var syncTimeLoader:URLLoader = new URLLoader();
syncTimeLoader.dataFormat = URLLoaderDataFormat.TEXT;
syncTimeLoader.addEventListener(Event.COMPLETE, syncTimeComplete );
syncTimeLoader.addEventListener(IOErrorEvent.IO_ERROR, syncTimeFailed );
sharedObject.clear();
startOfRequest = new Date();
sharedObject.data.startOfRequest = startOfRequest.getTime();
sharedObject.flush();
syncTimeLoader.load(syncRequest);
}
private function loadUTCFromSharedObject(e:Event = null):void
{
var sharedObjectCheck:SharedObject = SharedObject.getLocal("synctime", "/");
trace (sharedObject.data.endTime, sharedObjectCheck.data.endTime);
if ( sharedObject.data.endTime !== undefined )
{
timer.removeEventListener(TimerEvent.TIMER, loadUTCFromSharedObject);
timer.removeEventListener(TimerEvent.TIMER_COMPLETE, startSync);
dispatchEvent( new LoadUTCTimeEvent(LoadUTCTimeEvent.SYNC_COMPLETE, null, null, sharedObject, true));
trace(sharedObject.data.UTCOffset);
}
}
private function syncTimeComplete(event:Event):void
{
var loadedText:URLLoader = URLLoader(event.target);
dispatchEvent( new LoadUTCTimeEvent(LoadUTCTimeEvent.SYNC_COMPLETE, loadedText.data, startOfRequest, sharedObject, false));
}
Event Dispatcher:
public function LoadUTCTimeEvent(eventStatus:String, timeString:String = null, startOfRequest:Date = null, sharedObject:SharedObject = null, loadFromSharedObject:Boolean = false):void
{
super(eventStatus);
if (eventStatus == SYNC_COMPLETE) {
if (loadFromSharedObject == true) {
time = sharedObject.data.syncTime;
UTCOffset = sharedObject.data.UTCOffset;
}
else
{
//...snip....//
sharedObject.data.UTCOffset = UTCOffset;
sharedObject.data.syncTime = time;
sharedObject.data.endTime = curDate.getTime();
sharedObject.flush(1000);
}
}
}
The problem i am getting is that the 1st swf creates the sharedobject fine, but the second one reads the endTime as undefined, unless i refresh the page (i.e it doesnt pick up live changes). Is this method not posible, should i try another aproach?
Thanks in advance.
Edit: decided to add another stack to explain a little more:(as3) Sync data between 3 or more identical flash objects on the same page
you can call a javascript function to do the locking if you stuck with this solution
Okay, use localconnection
Typically, you should not use Local SharedObjects to sync things. The flush() command has never worked for me in the way you are intending it to.