Jump to content
Search Community

Accessing movieclips/sprites inside loaded swf

Chrysto test
Moderator Tag

Recommended Posts

Hi,

 

I have an splashscreen with some animation and button at the end of the animation. It is about 300k, so I want to make it another SWF file, load it, and unload it when it is no longer needed. The problem is - I have to access the buton inside the loaded swf. I can't make it happen, I have the button at the stage with name pink_mc, at the loaded swf document class I put public var pink_mc:MovieClip... When I load with Loader Max I can't access it via

 

var m:MovieClip = loader.content

 

m.pink_mc.addEventListener.... gives me error : there's not such a variable. Any suggestions?

 

And I will appriciate if somebody can help me with calling function onto parent swf - the one that loads the splash screen

Link to comment
Share on other sites

Remember, the "content" of a SWFLoader refers to the wrapper ContentDisplay Sprite into which the swf is loaded. So you were targeting the wrong object. SWFLoader has a convenient getSWFChild() method that allows you to grab a DisplayObject on the root level of its stage, like this:

 

var myButton:DisplayObject = loader.getSWFChild("pink_mc"); 

 

Or you can just use the "rawContent" property of the SWFLoader to reference the root of the swf, like this:

 

loader.rawContent.pink_mc;

 

It's all in the ASDocs :)

 

Enjoy.

Link to comment
Share on other sites

Yes, but none of the code is working for me...

 

In the timeline I got MovieClip called "pink_mc"

 

the loaded class:

 

 

package  {

import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.display.DisplayObject;

public class LoadedMain extends MovieClip {

	public var pink_btn:MovieClip;

	public function LoadedMain() {
		pink_btn.addEventListener(MouseEvent.CLICK, function():void{
								  trace("working");
								  });
	}

}//end
}

 

this is working perfectly

 

 

In the SWF I'm loading to:

 

 

package  {

import com.greensock.loading.LoaderMax;
import com.greensock.loading.SWFLoader;
import com.greensock.events.LoaderEvent;
import flash.events.Event;
import flash.display.MovieClip;
import flash.display.DisplayObject;
import flash.events.MouseEvent;

public class Main extends MovieClip{

	private var a:SWFLoader = new SWFLoader("loadedswf.swf");

	public function Main() {
		a.addEventListener(LoaderEvent.COMPLETE, onComplete);
		a.load();
	}

	private function onComplete(e:LoaderEvent):void{
		trace("loaded");	
		addChild( a.content);

		var s:DisplayObject = a.getSWFChild("pink_mc");
		s.addEventListener(MouseEvent.CLICK, function():void{trace("yabaaaaaaaaa")});
                   // 
                  trace(a.content.getSWFChild("pink_mc")
                  trace(a.getSWFChild("pink_mc")

	}


}//end
}

 

They both return null (it is said in the documentation they will) , but when I try to addEventListeners to them it gives me error:

 

var mk:DisplayObject = a.getSWFChild("pink_mc");

mk.addEventListener(MouseEvent.CLICK, function():void{trace("yabaaaaaaaaa")});

 

 

gives me

 

 

l

oaded
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Main/onComplete()
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at com.greensock.loading.core::LoaderCore/_completeHandler()
at com.greensock.loading::SWFLoader/_completeHandler()

Link to comment
Share on other sites

Strange, I make it working, i din't figured out what is the problem, but it seems that the methods are ot working with obects directly placed on stage EVEN IF they're declared like public variables in the application class. However, if you dynamicly add object, it is working....

 

The code that worked for me is:

 

--> in the loaded swf application class:

public var bys:bySprite = bySprite.newRect({width:100,height:100, color:"cyan"}); //it uses my drawing API, just to visiulize some simple square

 

public function LoadedMain() { //constructur function

 

addChild(bys);

 

--> in the class with the loader, after loading is finished:

 

 

public var a:SWFLoader = new SWFLoader("loadedswf.swf");

 

and ater it completes loading:

 

 

(a.rawContent.bys as bySprite).addEventListener(MouseEvent.CLICK, function():void{trace("yabaaaaaaaaa")});

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...