Solved

Help with AS3 event handler. Loading SWF into an MC.

Posted on 2008-10-25
3
1,193 Views
Last Modified: 2013-11-11
A colleague in the UK provided a solution to a problem I was having with AS3. I'm very new to Flash 9 and have some experience of AS2 and Flash 8. The colleague provided me a script that will load an external SWF into my movie. I assumed that the script would require me to import it when publishing. I also assumed that I would need to create a button on my desktop with a suitable instance name referenced by the script and a container on my stage such as a movie clip to act as a location for the external SWF to load into. (Excuse my ignorance, you can probably see basic AS2 logic here when it no doubt should be done differently). I used the following in frame 1 of the timeline: import folioas.*; I created a directory in the same location as the FLA and dropped the file test.as shown in the code snippet. I created an empty movie clip on the stage and set the instance name to loader. I also created a simple button and set the instance name to clickButton. I assumed this would have the desired effect. I published the file, but found that although no errors appeared in the compiler window, when I launched the resulting SWF it wouldn't load the other desired SWF 'news.swf in to the location I intended on the stage. I have confidence in my colleagues code, but can't reach him over the weekend to ask his advice and am on deadline for Monday for a presentation I have to give. Any help would be greatly appreciated.
package {

// Imports

	import flash.display.MovieClip;

	import flash.display.DisplayObject;

	import flash.display.Loader;

	import flash.net.URLLoader;

	import flash.net.URLRequest;

	import flash.events.Event;

	import flash.events.MouseEvent;
 
 

	public class Main extends MovieClip {

// =============================================================================================

// PROPERTIES

// =============================================================================================
 

// ===============================================================================

// PRIVATE CLASS METHODS & PROPS

		protected var mc                        :MovieClip;

		protected var clickButton               :MovieClip;
 

// ===============================================================================

// PRIVATE CLASS METHODS & PROPS
 

// ===============================================================================

// PRIVATE INSTANCE METHODS
 

// ----------------------------------------------------------------------------------------------

// Constructor

// ----------------------------------------------------------------------------------------------		

		public function Main() {

			setupButton();

		}

// ----------------------------------------------------------------------------------------------

// Setup the button

// ----------------------------------------------------------------------------------------------		

		protected function setupButton():void {

			clickButton.addEventListener(MouseEvent.CLICK, mainClick);

			clickButton.addEventListener(MouseEvent.MOUSE_OVER, mainRoll);

			clickButton.addEventListener(MouseEvent.MOUSE_OUT, mainRollOut);

			clickButton.buttonMode = true;

			clickButton.mouseChildren = false;

		}

// ----------------------------------------------------------------------------------------------

// Rollover script

// ----------------------------------------------------------------------------------------------		

		protected function mainRoll(e:Event):void {

			//	Rollover script

		}

// ----------------------------------------------------------------------------------------------

// Rollout script

// ----------------------------------------------------------------------------------------------		

		protected function mainRollOut(e:Event):void {

			//	Rollout script

		}

// ----------------------------------------------------------------------------------------------

// Loading script

// ----------------------------------------------------------------------------------------------		

		protected function mainClick(e:Event):void {

			var loader:Loader = new Loader();

			loader.contentLoaderInfo.addEventListener(Event.INIT, allComplete);

	 		var urlReq:URLRequest = new URLRequest('news.swf');

			loader.load(urlReq);

		}

// ----------------------------------------------------------------------------------------------

// When the file has uploaded attach the loaded stuff to the movieclip 'mc' that is on the stage

// ----------------------------------------------------------------------------------------------		

		protected function allComplete(e:Event):void{

   			mc.addChild(e.target.content);

		}

	}

}

Open in new window

0
Comment
Question by:tabrat
3 Comments
 
LVL 39

Accepted Solution

by:
blue-genie earned 250 total points
ID: 22812284
hi this is an unnamed package so technically if you created a Main.fla and set the document class to Main and saved the 2 files in the same location you should have liftoff.
the code creates a Loader object which loads the swf, so no you don't need a movieclip on the Stage.
you would need something called clickButton.
0
 

Assisted Solution

by:chimtrum
chimtrum earned 250 total points
ID: 22829037
Try blue-genie's suggestion and this

this["clickButton"].addEventListener(MouseEvent.CLICK, mainClick);
this["clickButton"].clickButton.addEventListener(MouseEvent.CLICK, mainClick);
this["clickButton"].clickButton.addEventListener(MouseEvent.MOUSE_OVER, mainRoll);
this["clickButton"].clickButton.addEventListener(MouseEvent.MOUSE_OUT, mainRollOut);

Make sure a movieclip with instance name "clickButton" exists in the Main movie


0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how can i learn adobe flex step by step 4 96
D3, mouseover, SVG, Javacript 6 82
Adobe Acrobat Pro: chasing the white background wallpaper dimensions 3 148
AutoCad licenses 9 55
Sometimes you know that one object has a specific child in it, but you can't find the child. This happened to me when I was trying to code some actionScript to make a toolbar work with its embedded buttons.  My partner had created the toolbar usi…
I have been doing hardcore actionscripting for some time; and needless to say I have faced a lot of problems in just understanding others' code rather than understanding what the code executes. A programmer's life can become hell when there are a lo…
Learn the basics of inputting and editing your text components in Prezi. We will cover how to set styles, position, and group your text components. In your Prezi editor, click anywhere on the canvas to add text: A flashing cursor informs you that yo…
Learn how to download your full Prezi presentation for offline presenting. Prezi doesn’t have to be viewed and shared in a web browser, even with a free account you can download your full presentation to share with others. Be sure to download any vi…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now