-
Load SWF into MC
Trying to load row.swf into the hold1_MC which is on the stage. What is wrong with this code? It loads it into the stage but not the MC. missing an import?
Thanks
Code:
import flash.net.URLRequest;
import flash.display.Loader;
import flash.events.Event;
import flash.events.ProgressEvent;
function startLoad()
{
var hold1_MC:Loader = new Loader();
var mRequest:URLRequest = new URLRequest("row.swf");
hold1_MC.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteHandler);
hold1_MC.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressHandler);
hold1_MC.load(mRequest);
}
function onCompleteHandler(loadEvent:Event){
addChild(loadEvent.currentTarget.content);
}
function onProgressHandler(mProgress:ProgressEvent){
var percent:Number = mProgress.bytesLoaded/mProgress.bytesTotal;
trace(percent);
}
startLoad();
-
You are making a Loader object called hold1_MC. I changed your code, so now I am making a Loader object called "holdLoader". Once it is done it loads the content of the holdLoader object into hold1_MC.
Code:
import flash.net.URLRequest;
import flash.display.Loader;
import flash.events.Event;
import flash.events.ProgressEvent;
function startLoad()
{
var holdLoader:Loader = new Loader();
var mRequest:URLRequest = new URLRequest("row.swf");
holdLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteHandler);
holdLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressHandler);
holdLoader.load(mRequest);
}
function onCompleteHandler(loadEvent:Event){
hold1_MC.addChild(loadEvent.currentTarget.content);
}
function onProgressHandler(mProgress:ProgressEvent){
var percent:Number = mProgress.bytesLoaded/mProgress.bytesTotal;
trace(percent);
}
startLoad();
-
-
Right now I have an event listener on a button and when that button is clicked it calls the startLoad() function. I have another function setup to remove the loaded swf from the movieclip.
My Q: How can I setup an if statement to see if the swf has been loaded into the MC or not?
Thanks
Matt
-
Declare a variable of Boolean type.
Code:
import flash.net.URLRequest;
import flash.display.Loader;
import flash.events.Event;
import flash.events.ProgressEvent;
var swf_Loaded_in_mc:Boolean = false;
myButton.addEventListener(MouseEvent.CLICK, addSWFtoMC);
function addSWFtoMC(e:MouseEvent):void
{
if (!swf_Loaded_in_mc) // If this variable equals false, start loading procedure
{
startLoad();
} else {
theFunctionThatRemovesTheSwfFromTheMC();
}
}
function startLoad()
{
var holdLoader:Loader = new Loader();
var mRequest:URLRequest = new URLRequest("row.swf");
holdLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteHandler);
holdLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressHandler);
holdLoader.load(mRequest);
}
function onCompleteHandler(loadEvent:Event){
hold1_MC.addChild(loadEvent.currentTarget.content);
swf_Loaded_in_mc = true; // Sets that variable to 'true' when the SWF has been added to the MC
}
function onProgressHandler(mProgress:ProgressEvent){
var percent:Number = mProgress.bytesLoaded/mProgress.bytesTotal;
trace(percent);
}
EDIT: Don't forget to change the value of swf_Loaded_in_mc back to "false" in your function that removes the loaded swf from the movieclip.
Last edited by Beathoven; 04-28-2009 at 04:09 PM.
-
it wants an argument in each function in the if statement.
-
Can you copy and paste the exact error your getting
-
Here is the error: 1136: Incorrect number of arguments. Expected 1.
And here is the code
Code:
var MB:Boolean = false;
var RR:Boolean = false;
var SM:Boolean = false;
var ND:Boolean = false;
import flash.net.URLRequest;
import flash.display.Loader;
import flash.events.Event;
import flash.events.ProgressEvent;
function loadMB(e:MouseEvent):void
{
if (!MB) // If this variable equals false, start loading procedure
{
loadMemberBenefits();
} else {
unloadMemberBenefits() ;
}
}
//////////////LOAD MEMBER BENE\\\\\\\\\\\\\\\\\\\
function loadMemberBenefits(evt :MouseEvent):void
{
var holdLoader:Loader = new Loader();
var mRequest:URLRequest = new URLRequest("row.swf");
holdLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, memberBenefits);
holdLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressHandler);
holdLoader.load(mRequest);
}
/////////////END LOAD MEMBER BENE\\\\\\\\\\\\\\\\\
/////////////UNLOAD MEMBER BENE\\\\\\\\\\\\\\\\\\\
function unloadMemberBenefits(evt :MouseEvent):void
{
var holdLoader:Loader = new Loader();
var mRequest:URLRequest = new URLRequest("row-remove.swf");
holdLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, memberBenefits);
holdLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressHandler);
holdLoader.load(mRequest);
}
//////////////END UNLOAD MEMBER BENE\\\\\\\\\\\\\\\
-
forgot to include my event listener
Code:
btn1.addEventListener(MouseEvent.CLICK, loadMB);
The error is showing up for both functions inside the if statement
-
You can't do that. The functions you defined are event handling functions. You are basicly trying to call an event handling function when no event was fired. And your "unloadMemberBene" function is a duplicate of your loadMemberBene function. That's just gonna add another instance of your SWF to the MC.
When you define your function, whatever you put in between the "()" will be expected when you call that function, understand?
So if you say: function myFunction(aVariable:int):void
It means when you call "myFunction", it will expect a variable of type "int" to be passed to it and it will return nothing (:void)
So, for instance, if I wanted to call myFunction, I would have to write "myFunction(1);" or "myFunction(anInteger);" (1 being an integer, and 'anInteger' being a variable of type 'int').
This is the proper code (Assuming myButton is the name of your button instance on stage):
Code:
var MB:Boolean = false;
var RR:Boolean = false;
var SM:Boolean = false;
var ND:Boolean = false;
import flash.net.URLRequest;
import flash.display.Loader;
import flash.events.Event;
import flash.events.ProgressEvent;
myButton.addEventListener(MouseEvent.CLICK, loadMB);
function loadMB(e:MouseEvent):void
{
if (!MB) // If this variable equals false, start loading procedure
{
loadMemberBenefits();
} else {
unloadMemberBenefits() ;
}
}
//////////////LOAD MEMBER BENE\\\\\\\\\\\\\\\\\\\
function loadMemberBenefits():void
{
var holdLoader:Loader = new Loader();
var mRequest:URLRequest = new URLRequest("row.swf");
holdLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, memberBenefits);
holdLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressHandler);
holdLoader.load(mRequest);
}
/////////////END LOAD MEMBER BENE\\\\\\\\\\\\\\\\\
/////////////UNLOAD MEMBER BENE\\\\\\\\\\\\\\\\\\\
function unloadMemberBenefits():void
{
for (var i:int = 0; i < hold1_MC.numChildren; i++)
{
hold1_MC.removeChildAt(i); // this will remove all objects inside your hold1_MC.
}
}
//////////////END UNLOAD MEMBER BENE\\\\\\\\\\\\\\\
Last edited by Beathoven; 04-28-2009 at 05:19 PM.
-
Forgot to say thanks for this, Beathoven! This worked and helped me understand as3 a little better.
-
is it possible to access the classes within ""row.swf"
like lets say row.as was the document class for row.swf after the load could we go row.init() where init is a public function in the row.as?
-
Originally Posted by aemciv
Forgot to say thanks for this, Beathoven! This worked and helped me understand as3 a little better.
No problem buddy. Make sure you mark the thread as 'resolved'.
(Top right of first post in the thread, 'Thread tools', 'Mark Thread Resolved')
-
Originally Posted by shamimsaad03
is it possible to access the classes within ""row.swf"
like lets say row.as was the document class for row.swf after the load could we go row.init() where init is a public function in the row.as?
You should make a new thread for this.
As for your question, no you wouldn't go 'row.init();'.
First you'd have to give a name to your SWF this way:
whateverObjectLoadedTheSWF.name = "row";
Then you'd try to go:
stage.getChildByName("row").init();
I don't know if calling that function like that would work, but that's the way you'd get to your SWF. Try it, lemme know.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|