Flash AS3 LoaderContext parameters with "undefined" - actionscript-3

I have a main movie clip using loader.
There are two buttons at the child movie clip, That is used for calling the child movie clip with loader.
Main movie clip : main.wf
Initial Loader Movie clip : sub.swf
Button A Click : Call a.swf
Button B Click : Call b.swf
I mean this loader load two movie clip as I click every button.
The main movie clip control passing variables over the child movie clip with LoaderContext.
The problem is that second movie clip(b.swf) cannot get loader context parameters properly.
The error message is that the parameter value is undefined.
// main.swf
var context:LoaderContext = new LoaderContext();
context.parameters = {"qty": qty};
This make me crazy because when I click button A in order to call a.swf(child movie clip)
The parameter get the result correctly in the a.swf.
// a.swf
var qty = this.loaderInfo.parameters.qty; // it's okay at a.swf
However I've finished job in the a.swf that called method which is located in the main.swf.
The method do the job for calling b.swf. The problem happened at this time.
// b.swf
var qty = this.loaderInfo.parameters.qty; // error : undefined value
Please give me an advice to resolve it.
// main.swf
var context:LoaderContext = new LoaderContext();
context.parameters = {"qty": "start"};
var myLoader:Loader = new Loader();
var url:URLRequest = new URLRequest("sub.swf");
myLoader.load(url, context);
addChild(myLoader);
function nextStep0(qty:String):void{
// sub.swf's called this method for updating qty
this.qty = qty;
context.parameters = {"qty": qty};
url = new URLRequest("a.swf");
myLoader.load(url, context);
}
function nextStep1(qty:String):void{
// a.swf's called this method for updating qty
this.qty = qty;
context.parameters = {"qty": qty};
url = new URLRequest("b.swf");
myLoader.load(url, context);
}
This is a sub child what is called initially.
sub.swf
var qty_char = this.loaderInfo.parameters.qty; // "start" get it works
button.addEventListener(MouseEvent.CLICK, clichandle);.... // Event Listener Click
fuction clichandle(e:MouseEvent){
var r_movie:MovieClip = this.parent.parent as MovieClip;
r_movie.nextStep0("alpha");
}
This is a child movie clip
a.swf
var qty_char = this.loaderInfo.parameters.qty; // "alpha" get it works
button.addEventListener(MouseEvent.CLICK, clichandle);.... // Event Listener Click
fuction clichandle(e:MouseEvent){
var r_movie:MovieClip = this.parent.parent as MovieClip;
r_movie.nextStep1("beta");
}
This is a child movie clip too
b.swf
var qty_char = this.loaderInfo.parameters.qty; // error, undefined.
button.addEventListener(MouseEvent.CLICK, clichandle);.... // Event Listener Click
fuction clichandle(e:MouseEvent){
}

Related

Unloading swf file through button click

So I'd like to set this up to where you click on a button it loads a new scene and unloads the previous scene.
This is what I have so far.
staart.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler);
function fl_MouseClickHandler(event:MouseEvent):void
{
function loadSWF(swfURL){
var myLoader:Loader = new Loader();
var mySWF:URLRequest = new URLRequest(swfURL);
myLoader.contentLoaderInfo.addEventListener
(Event.COMPLETE,onCompleteHandler);
myLoader.contentLoaderInfo.addEventListener
(ProgressEvent.PROGRESS,onProgressHandler);
myLoader.load(mySWF);
}
function onCompleteHandler(loadEvent:Event){
addChild(loadEvent.currentTarget.content);
loadEvent.currentTarget.content.gotoAndStop(swfFrame);
}
function onProgressHandler(myProgress:ProgressEvent){
var percent:Number =
Math.round(myProgress.bytesLoaded/myProgress.bytesTotal*100);
trace(percent+"% loaded");
}
}
var swfFrame:Number= 1;
loadSWF("home.swf");
}
Is it possible to unload a specific swf file or previous swf file through a newly loaded swf file?
You should create a variable for your swf.
Add this to your code :
var mc:MovieClip = new MovieClip();
// Adds the movie clip at initialization.
addChild(mc);
and modify your onCompleteHandler method like this :
function onCompleteHandler(loadEvent:Event){
// Changes the content of your movie clip.
mc = loadEvent.currentTarget.content;
mc.gotoAndStop(swfFrame);
}
shawn gave you the right answer but there are 2 other problems in your code:
There's a typo in element staart and a potential memory leak because you add event listeners without removing them. You should add the following two lines in onCompleteListener function:
loadEvent.currentTarget.removeEventListeners(Event.COMPLETE,onCompleteHandler)
loadEvent.currentTarget.removeEventListeners(ProgressEvent.PROGRESS,onProgressHandler)
If you don't remove the listeners, the garbage collector will be unable to free the memory of every loader you create on each click

as3 error: access of possibaly undefind property through a reference with static type flash.display:DisplayObject

I have this as3 project, and in frame one of the timeline I tried to load a swf movie named "menu" and in this loaded movie I have an instance of a button named "button1", and I want to add a new EventListener to this "button1". my code is here:
var theLoader:Loader = new Loader();
var address:URLRequest = new URLRequest("menu.swf");
theLoader.load(address);
theLoader.contentLoaderInfo.addEventListener(Event.COMPLETE , swfDidLoad);
function swfDidLoad(evt:Event){
if(theLoader.content){
addChild(theLoader);
var button:SimpleButton = theLoader.content.button1;
button.addEventListener(MouseEvent.CLICK, handler1);
}
}
function handler1 (event:MouseEvent):void
{
removeChild(theLoader);
gotoAndStop(10);
};
but I get this undefind property error. what should I do? Am i doing this right at all?
The reason you are getting that error is because you are trying to access button1 on theLoader.content which is a non-dynamic DisplayObject (this means that only explicitly defined properties/methods are valid). You must first cast it to a MovieClip (which is dynamic).
You should change that line to:
var button:SimpleButton = MovieClip(theLoader.content).button1;

Modify stage size cause error - Flash CC

I have an application with 2 scene, the first scene contains 4 movieclips which are placed manually into the scene, every movieClip had an instance name (mc1,mc2..mc4), and I create an array with this objects var arr:Array = [mc1..mc4];
I add them an mouse event listener , for each (var i in arr){ i.addEvent...mouse.click)};
In this scene I also have an button "next scene" which have this code : nextScene();
In the second scene I have one button "back" which have this code : prevScene();
My app is 1200 x 720 px size, and I want it 800 x 600, so when I'm changing this manually .
When I'm running the application, everything is good, a go to next scene, and when I press back, it gives me an error at first scene
for each(var i in arr){
i.addEvent...mouse.click)
};
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Main/frame2()
at flash.display::MovieClip/prevScene()
at Main/onBack()
When I trace mc1 , at first run scene1 the output is //movieclip
when I'm pressing back the output is // null
and if I trace arr, the output is // ,movieclip,movieclip,movieclip (first only comma)
what can be the problem? thank you
Scene 1 code:
stop();
trace(mc1); // first run -> object [MovieClip]
// when back pressd -> null
var selectedIm:MovieClip = mc1;
var selectedD = d1;
var difficulty:uint = 3;
var imgs:Array = [mc1,mc2,mc3,mc4];
var diff:Array = [d1,d2,d3,d4];
goBtn.addEventListener(MouseEvent.CLICK, onGo);
for each(var i in imgs){
i.addEventListener(MouseEvent.CLICK, onImage); //here is the error, NULL OBJECT
}
function onGo(e:MouseEvent):void{ //next button
new Clk().play();
nextScene();
}
function onImage(e:MouseEvent):void{
new Clk().play();
if(selectedIm) selectedIm.filters = [];
selectedIm = e.target as MovieClip;
addOutline(selectedIm,0xFFFFFF,6);
}
...
Scene 2 code:
...
function onBack(e:MouseEvent):void{
new Clk().play();
removeChild(pz);
timer.reset();
timer.stop();
timer.removeEventListener(TimerEvent.TIMER, numara);
prevScene();
....
}

Trying to access another MovieClip on frame 2 of a movieclip on main timeline

I am working with nesting of movieclips.
I am trying to access another movieclip in frame 2 of main movieclip on stage.
=> test2 movieclip is present on frame2 of test movieclip.
I am getting this error because of accessing test2 variable..
TypeError: Error #1009: Cannot access a property or method of a null
object reference. at Untitled_fla::MainTimeline/Access() at
Untitled_fla::MainTimeline/Untitled_fla::frame1()
Any alternate method to access test2 movieclip on frame2 of main movieclip?
My Code is :
var test1:MovieClip;
var test2:MovieClip;
test.stop();
function Start(){
//test.addChild(test1);
//Test1 = new test1();
//Test2 = new test2();
trace(test.numChildren);
test1 = MovieClip(test.getChildByName("test1"));
test.gotoAndStop(2);
test2 = MovieClip(test.getChildByName("test2"));
}
function Access(color:String){
var r:RegExp=new RegExp(/#/);
var uintColor:uint = uint(String(color).replace(r,"0x"));
var c: ColorTransform = new ColorTransform();
c.alphaMultiplier = 0.9;
c.color = uintColor;
test.gotoAndPlay(2);
test2.transform.colorTransform = c;
}
Start();
Access("#666666");
In flash while moving though he timeline flash player creates the nested children only in the end of frame (except the first frame) that is, if you have child named test2 in he second frame of clip and navigate to this frame you have to wait 1 frame before the children in this 2nd frame will be creared. Try the following solution:
function Start(){
trace(test.numChildren);
test1 = MovieClip(test.getChildByName("test1"));
test.gotoAndStop(2);
addEventListener(Event.ENTER_FRAME, function(event:Event):void
{
EventDispatcher(event.target).removeEventListener(event.type, arguments.callee);
test2 = MovieClip(test.getChildByName("test2"));
});
}
Try changing your Access function. I think test2 wont be available if you are playing test MovieClip.
function Access(color:String){
var r:RegExp=new RegExp(/#/);
var uintColor:uint = uint(String(color).replace(r,"0x"));
var c: ColorTransform = new ColorTransform();
c.alphaMultiplier = 0.9;
c.color = uintColor;
test.gotoAndStop(2);
test2.transform.colorTransform = c;
}

Some problems regarding removing a child added dynamically

I have this function where I want to get a movie clip (the function target) and change it to another one. The problem is that it obviously removes the movie clip before the new one is loaded.
var changePeca:Loader = new Loader;
var changeLoad:URLRequest = new URLRequest(e.target.name.substr(0,4)+".png");
changePeca.load(changeLoad);
e.target.removeChildAt(0);
e.target.addChild(changePeca);
I know that I must use the Event.COMPLETE thing, but how do I say which movie clip to remove, since I cant use e.target anymore?
"The problem is that it obviously removes the movie clip before the new
one is loaded."
Because you code says do so! :) You need to add the event listener which checks if the stuff is loaded.
private var holderMC:Sprite;
private var imageLoader:Loader;
private function load(e:Event):void
{
holderMC = e.target as Sprite // or something else you have there, just store it.
imageLoader = new Loader ();
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, handleLoadComplete)
imageLoader.load(new URLRequest(e.target.name.substr(0, 4) + ".png"));
}
private function handleLoadComplete(e:Event):void
{
if(holderMC.numChildren > 0)
holderMC.removeChildAt(0);
holderMC.addChild(imageLoader.content)
}