Question

How to call a method in document class, from loader content movie AS3

Asked by: gxp071

Hi im trying to make as small app but I'm having trouble accessing functions in the document class from the sibling movie that has been externally loaded.

I'm sure its that I'm just going about this totally the wrong way, I'm fairly new to AS3 and usually use AS2.

See the code and comments below,

// In the external child mc im Trying to call
 
stage.lift();
 
// lift(); being a public function of the document class.  However I get the following compile error
// 1061: Call to a possibly undefined method lift through a reference with static type flash.display:Stage.
 
 
// if I cast the stage to a MovieClip, or StageManager (the doc Class extends MovieClip) the compile error goes away but I get a runtime error
// TypeError: Error #1034: Type Coercion failed: cannot convert flash.display::Stage@2de94b51 to flash.display.MovieClip.
 
 
//So I am stuck any suggestions would be welcomed.

                                  
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:

Select allOpen in new window

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-07-07 at 04:09:37ID24549119
Tags

Actionscript 3

,

Flash

,

swf loader

Topics

ActionScript

,

Adobe Flash

Participating Experts
3
Points
500
Comments
14

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Unload swf movie AS2 inside swf AS3
    Hi I'm using Flash CS3 with Action Script 3, and I load a movie in AS2 insede another movie in AS3. The code is var contenedor = new Loader(); contenedor.load(new URLRequest("05_01_menu.swf")); addChild(contenedor); contenedor.x = 180 contenedor.y = 288 I don't kn...
  2. Actionscript 2 MovieClipLoader issue when AS2 movie is l…
    Hi, I have a legacy actionscript 2 movie that is giving me trouble when loaded within an Actionscript 3 movie although it works great when the actionscript 2 movie is ran by itself. I have attached the code below. This script works great when ran in the AS2 movie by itself bu...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: blue-geniePosted on 2009-07-07 at 05:31:22ID: 24793562

howdy.
this is how I do it. And so far no one has been able to tell me if its the right way or not.

assume your document class is called Box

Box(root).lift();

 

by: gxp071Posted on 2009-07-07 at 05:48:31ID: 24793696

Hi thanks for the comment,

My document class is called StageManager, and ive tried

StageManager(root).lift();

but i get the following runtime error,

TypeError: Error #1034: Type Coercion failed: cannot convert flash.display::MovieClip@270cac41 to StageManager.

 

by: rascalpantsPosted on 2009-07-07 at 08:04:06ID: 24794860

A better way to do this is to have both classes setup to be able to dispatch and listen for custom events.

here is an example of a custom event tutorial...

http://flexcomps.wordpress.com/2008/04/29/dispatching-custom-events-in-as3-with-data-or-object/


but basically, this allows your application to be built in a way that it doesn't need to know anything about the structure of a class, it just sends off a message, and who ever wants to listen to that message, will, and respond appropriately.


rp

 

by: blue-geniePosted on 2009-07-07 at 23:41:11ID: 24801141

thanks for that rp.
But i'm curious why my little hack doesn't work.
your stageManager class extends Movieclip right?

 

by: gxp071Posted on 2009-07-08 at 01:42:59ID: 24801714

Afraid not,

and yes StageManager does extend MovieClip.

And I agree with you that it should work but for some unknown reason it does not.  If I cast it as a MovieClip it doesn't get the 1034 Error but instead cant find the function/ not defined which makes sense as MovieClip doesn't have the functions defined in StageManager...

Will try out the custom event tutorial but I have a feeling that there must be something else in my code thats angering the Flash IDE compiler.

 

by: ruijietanPosted on 2009-07-08 at 02:45:17ID: 24802001

The reason it doesn't work is because the document class is NOT the 'stage', and you're trying to call functions on 'stage'.

This is how the layout works.

When you start the Flash Player, your main class is not loaded as a stage instance, but instead at one level lower.

Stage -> Your App's Document Class -> Loader -> content property of Loader.

So technically by accessing stage.lift(); you are trying to call a non-existent function on Stage.

Also in AS3 the _lockroot property is always enforced, so by calling root.lift(); you will be instead calling the lift(); function in the loaded movie, not your main movie, which is not your intended purpose.

You should try rascalpants' suggestion on event dispatch, it is generally not good practice to call functions in the main class from a loaded one, however if you still wish to do so (calling functions in document class) this is how I usually do it:

//according to your previous code (I suppose you still want to use stage as a global reference)
//use this to access document class
stage.getChildAt(0).lift();
 
//getChildAt(0) accesses the first child of Stage, which is usually your document class in most cases.

                                              
1:
2:
3:
4:
5:

Select allOpen in new window

 

by: blue-geniePosted on 2009-07-08 at 03:07:28ID: 24802111

i disagree ruijietan in practice, not in theory.

reason being I've been using that (whether correctly or not) successfully for the past month or so.
We're trying to access a function in the document class not the stage.
i agree as per my previous comment using dispatchers is probably a better more OOP way of doing it.

 

by: ruijietanPosted on 2009-07-08 at 03:32:42ID: 24802237

Hmm.. I thought by using your code:
StageManager(root).lift();

In a loaded movie, it would access the root of the loaded movie, not the loader?
And in that case you would be trying to type your loaded movie's root to StageManager, which would fail because the two files are not related in any way and have no reference to each other.

I have attached a sample class files which demonstrate both my method and blue-genie's method, unfortunately I cannot get blue-genie's variation to work, as you will see in comments.

Simply save both files as loader.as and external.as respectively, and associate them with two different FLA files with them as their document classes.

//SAVE THIS AS loader.as
 
package {
	import flash.display.*;
	import flash.net.URLRequest;
 
	public class loader extends Sprite {
		//vars
		public var test:String = "loader test string";
		
		private var urlReq:URLRequest = new URLRequest("external.swf");
		private var ldr:Loader;
 
		public function loader():void {
			ldr = new Loader();
			ldr.load(urlReq);
			addChild(ldr);
		}
	}
}
 
//SAVE THIS AS external.as
package {
	import flash.display.*;
	import flash.events.*;
	
	public class external extends MovieClip {
		//vars
		public var test:String = "external test string";
		
		public function external():void {
			this.addEventListener(Event.ADDED_TO_STAGE, addedToStageListener);
		}
		
		private function addedToStageListener(event:Event) {
			if (parent is Loader) {
				trace("This SWF file was loaded in by Loader.swf.");
				trace("External SWF attempting to access test string from Loader SWF's Main Class:", stage.getChildAt(0)["test"]);
				//The following code uses blue-genie's method, which if you uncomment you will see that it does not work.
				//trace("External SWF attempting to access test string from Loader SWF's Main Class alternate method:", loader(root)["test"]);
				//_lockroot is always enforced in AS3, so by referring to root here it is actually pointing to external, not loader.
				//so if you try and type/convert it to loader by using loader(root) you are trying to convert external to loader, which
				//fails because they have no relation at all.
			} else if (parent is Stage) {
				trace("This SWF was loaded standalone.");
				trace("External SWF attempting to access test string from external SWF's Main Class:", stage.getChildAt(0)["test"]);
			}
			trace("External SWF's Parent:", parent);
			trace("External SWF's Stage:", stage);
			
			
		}
	}
}

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:

Select allOpen in new window

  • loader.txt
    • 30 KB

    change file extension to .fla and use loader.as as document class.

  • external.txt
    • 54 KB

    change file extension to .fla and use external.as as document class

 

by: gxp071Posted on 2009-07-08 at 03:59:30ID: 24802377

Thanks Guys,  

this was all really useful, and I know its a total hack, but time and needs must, Really need to get my head round the hierarchy of AS3.  And at the earliest opportunity im going to throw my self at custom event.

But for now thanks for the hack.   I'm going to split the points if that OK as you all put in your time to help.

 

by: gxp071Posted on 2009-07-08 at 04:02:28ID: 31600546

Thanks For all the Help!

 

by: blue-geniePosted on 2009-07-08 at 06:18:53ID: 24803388

ok i've just realised where my confusion is.
I'm sorry - didn't mean to confuse everyone on the way - to clarify.

if you've created a swf file with a document class and another one -  let's call in base file with its document class, and you load the first swf into the base file, yes you'll have an error.

sorry not thinking beyond my own breathing space.
What I'm doing when I do that is with a movieclip that i give a class linkage to and adding to the stage. and then from that class accessing the parent - not from a something with its own document class.

 

by: blue-geniePosted on 2009-07-08 at 06:59:52ID: 24803832

 

by: rascalpantsPosted on 2009-07-08 at 07:57:39ID: 24804475

just to share my personal opinion about using a document class...  

I don't.


I normally contain everything into a main_mc  movieclip and then setup a base class in the library.

Then when I have other visual assets that need their own classes within the Main, I do the same.

This is a nice way to setup bubbling for custom events.

to me, a document class just seems redundant redundant.

rp

 

by: blue-geniePosted on 2009-07-08 at 23:26:53ID: 24811088

that's interesting. thanks for that rp.

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...