View Full Version : Trying to get externally loaded SWF to act as movieclip from external Class (.as file
makoinater
01-21-2009, 06:13 PM
Right I have a problem - when I load a swf and I want to be able to add event listeners to propertys on it I use the code
var headerMovie = MovieClip(loader.content);
Which works fine but what I've have created now is a class to load external SWF's and add them saving me to repeat that code, which works fine for loading the swf but as for the part where I get it to convert to MovieClip (which is what I assume that bit of code does) I'm out of Idea's.
here is the class so far - the error it gives is that it cannot acces the property mloader, I've even declared it outside of the function but to no avail.
package {
public class PreLoaderSwf3 extends MovieClip {
var myTween:Tween;
public var loaderSpin:MovieClip = new Loading_MOV();
public function PreLoaderSwf3() {
//stuff for preloader spinner
loaderSpin.x = 500;
loaderSpin.y = 80;
loaderSpin.alpha = .5;
}
public function startLoad(swfAddress) {
var mLoader:Loader = new Loader();
var mRequest:URLRequest = new URLRequest(swfAddress);
mLoader.contentLoaderInfo.addEventListener(Event.C OMPLETE, onCompleteHandler);
mLoader.contentLoaderInfo.addEventListener(Progres sEvent.PROGRESS, onProgressHandler);
mLoader.load(mRequest);
mLoader.x = -400;
addChild(mLoader);
//loadEvent.currentTarget.content = MovieClip(mloader.content);
//mLoader.alpha = .5;
//return convertName;
}
function onCompleteHandler(loadEvent:Event) {
loadEvent.currentTarget.content = MovieClip(mloader.content); //The code giving me trouble
addChild(loadEvent.currentTarget.content);
loadEvent.currentTarget.content.alpha = 0;
this["myTween"] = new Tween(loadEvent.currentTarget.content, "alpha", None.easeInOut, 0, 1, 8, false);
removeChild(loaderSpin);
}
//stuff for adding spinner loaders and shit all works grand
function onProgressHandler(mProgress:ProgressEvent) {
var percent:Number = mProgress.bytesLoaded/mProgress.bytesTotal;
trace(percent);
addChild(loaderSpin);
}
}
}
makoinater
01-22-2009, 10:28 AM
Anyone? I'll try and reword it into a simpler form later if I can - I really want to know how to send a variable into a class and then return it with a specific value? ye know
Mazoonist
01-22-2009, 01:14 PM
You need to be more consistent in your capitalization. You've got mloader in places and mLoader in others.
I've even declared it outside of the function but to no avail.
Probably because your capitalization varied. Let's stick with mLoader.
Add these to your variables list:
private var headerMovie:MovieClip;
private var mLoader:Loader;
private var myTween:Tween
Now the scope of mLoader, headerMovie, and myTween is the entire class.
Inside your startLoad function, change this line:
var mLoader:Loader = new Loader();
to this:
mLoader = new Loader();
Replace your onComplete handler function with this:
private function onCompleteHandler(loadEvent:Event) {
headerMovie = MovieClip(mLoader.content);
addChild(headerMovie);
headerMovie.alpha = 0;
myTween = new Tween(headerMovie, "alpha", None.easeInOut, 0, 1, 8, false);
removeChild(loaderSpin);
}
You can see in the above that I also fixed the Tween issue that maybe you didn't know about. If you declare tweens inline with the var keyword, they have a danger of being garbage collected before the tween plays out. Basically, the function's over very rapidly, but the tween isn't. So the variable you used can get garbage collected while the tween is still playing. So they need to be declared separately from the function they're being used in. Also, in my opinion, you should never use the form this["variableName"] in AS3. There's almost always no need for it anymore.
Also, if you're going to tween anything, you'll have to import the tween classes:
import fl.transitions.Tween;
import fl.transitions.easing.*;
makoinater
01-22-2009, 03:00 PM
Cheers for response - Okay so the class doesnt give any errors now, but I'm trying to get it to allow me to use the buttons from the loaded SWF if you get me? which its still bringing up errors.
Here is my class (in full, didnt change anything with the tween, I used that whole "" thing which has the effect of declareing it outside the function, Ill change it proper eventually)
package {
import flash.net.URLRequest;
import flash.display.Loader;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.display.MovieClip;
import flash.events.Event;
import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.transitions.TweenEvent;
public class PreLoaderSwf3 extends MovieClip {
var myTween:Tween;
var mLoader:Loader;
private var loaderMovie:MovieClip;
public var loaderSpin:MovieClip = new Loading_MOV();
public function PreLoaderSwf3() {
loaderSpin.x = 500;
loaderSpin.y = 80;
loaderSpin.alpha = .5;
}
public function startLoad(swfAddress) {
mLoader = new Loader();
var mRequest:URLRequest = new URLRequest(swfAddress);
mLoader.contentLoaderInfo.addEventListener(Event.C OMPLETE, onCompleteHandler);
mLoader.contentLoaderInfo.addEventListener(Progres sEvent.PROGRESS, onProgressHandler);
mLoader.load(mRequest);
mLoader.x = -400;
addChild(mLoader);
}
function onCompleteHandler(loadEvent:Event) {
loaderMovie = MovieClip(mLoader.content);//Shouldnt this code allow me to work with the external SWF's buttons?
addChild(loaderMovie);
loadEvent.currentTarget.content.alpha = 0;
this["myTween"] = new Tween(loadEvent.currentTarget.content, "alpha", None.easeInOut, 0, 1, 8, false);
removeChild(loaderSpin);
}
function onProgressHandler(mProgress:ProgressEvent) {
var percent:Number = mProgress.bytesLoaded/mProgress.bytesTotal;
trace(percent);
addChild(loaderSpin);
}
}
}
Now here is the code on my main FLA which creates an instance and loads the loginMain swf onto the stage.
On the loginMain I have a button called loginButTest as you can see
var loginMain:PreLoaderSwf3 = new PreLoaderSwf3();
addChild(loginMain);
loginMain.y = 130;
loginMain.startLoad("xswf/LoginMain.swf");
loginMain.loginButTest.addEventListener(MouseEvent .CLICK,myLoginFunction2);
function myLoginFunction2(evt:MouseEvent):void {
trace("works");
}
and this is the error it gives -
1119: Access of possibly undefined property loginButTest through a reference with static type PreLoaderSwf3.
any ideas?
Mazoonist
01-22-2009, 03:24 PM
The buttons you want to control are inside the loaded movie, right? But the instance of your class is not itself the loaded movie. The error message is telling you that the instance of your class doesn't have a property called "loginButTest," which is true. You can search all through your class, and there's no property called that.
But it has a property called loadedMovie, which (I assume) has a property called loginButTest. To make this work, you have to make the loadedMovie property a public variable in your class. Then the path to the buttons would be:
loginMain.loadedMovie.loginButTest
But using public variables is generally considered a no-no. I'm just trying to show you why you got the error. Structuring your application would be up to you. I don't really have a clear picture as to how you're using this.
[Edit: Correction: loginMain.loaderMovie.loginButTest
I had your "loaderMovie" as "loadedMovie"]
makoinater
01-22-2009, 06:08 PM
Its for a website, which when you hit a certain changes pages - each page is a separete SWF file which in itself may contain some buttons which I want to control the main FLA,
Its hurting my head thinkin about it, although its not giving a compile error, it is giving a runtime, I'm pretty sure because its looking for buttons beforew the swf has full loaded, How would you go about only putting the event listener for the button after the SWF has fully loaded and all the functions in the PreLoaderSwf class are completed?
I was trying something like.
var loginMain:PreLoaderSwf3 = new PreLoaderSwf3();
addChild(loginMain);
loginMain.y = 130;
loginMain.startLoad("xswf/LoginMain.swf");
//This is the code for checking has the function onCompleteHandler finished, or at least I want it to be
loginMain.onCompleteHandler.addEventListener(flash .events.Event.COMPLETE, login_finish);
function login_finish{
loginMain.loaderMovie.loginButTest.addEventListene r(MouseEvent.CLICK,myLoginFunction2);
}
function myLoginFunction2(evt:MouseEvent):void {
trace("works");
}
I dont get why you shouldnt have public variables in a class though - surely its the only way to achieve something like this?
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.