Home Tutorials Forums Articles Blogs Movies Library Employment Press

Go Back   ActionScript.org Forums > ActionScript Forums Group > ActionScript 3.0

Reply
 
Thread Tools Rate Thread Display Modes
Old 07-07-2012, 05:02 AM   #1
nightkarnation
Senior Member
 
Join Date: Apr 2007
Posts: 114
Question Communication problem between a loaded mc class and Main Class

Hey guys!
I have the following problem:
Simplified example:

I have a loaded Movie Clip on my main.fla called McContactUs.as (exporting)...

On my main.fla = Main.as

Code:
public function userRequestCheckCurrentEmailForContactForm(username:String)
{
	trace("INSIDE CHECKING FORM...bla bla bla");
}
On McContactUs.as I have the following:

PHP Code:
public class McContactUs extends MovieClip
{
    public function 
McContactUs()
    {
        
init();
    }
        
    public function 
init()
    {
       
MovieClip(root).userRequestCheckCurrentEmailForContactForm("test");    
    }
}

I get TypeError: Error #1009:

You can see that I am calling the function on Main.as exactly at the moment the class is initialized...This is where the problem is...
If I try to call the function through a btn.addEventListener...it works just fine...but I need to call the function at the time the class/movieclip is loaded...

Any ideas and or suggestions how I can get around this without using a timer :[ ??

Thanks a lot in advance!!
Cheers!
nightkarnation is offline   Reply With Quote
Old 07-07-2012, 11:01 AM   #2
arkitx
Senior Member
 
Join Date: Jan 2011
Posts: 703
Default

ActionScript Code:
package {     import flash.display.MovieClip;     public class Main extends MovieClip {         private var contact:McContactUs;         public function Main() {             contact=new McContactUs();             addChild(contact);         }         public function userRequestCheckCurrentEmailForContactForm(username:String) {             trace("INSIDE CHECKING FORM...bla bla bla");         }     } }

ActionScript Code:
package {     import flash.display.MovieClip;     import flash.events.Event;         public class McContactUs extends MovieClip {         public function McContactUs() {             addEventListener(Event.ADDED_TO_STAGE,addedToStage);         }         private function addedToStage(evt:Event):void{             removeEventListener(Event.ADDED_TO_STAGE,addedToStage);             init();         }         public function init() {             MovieClip(root).userRequestCheckCurrentEmailForContactForm("test");         }     } }



__________________
Regards

arkitx
arkitx is offline   Reply With Quote
Old 07-07-2012, 01:36 PM   #3
ASWC
Super Moderator
 
ASWC's Avatar
 
Join Date: Dec 2007
Location: Greenville, SC
Posts: 6,528
Default

This is really not the proper and professional way to do this. The loaded swf should dispatch a custom event and let the main swf handle the rest. This makes everything easily maintainable and updatable without breaking anything. The way you are doing it right now (which many people do unfortunately) would be a nightmare to maintain and update if you were working with multiple swfs or if you had to make any change later on.

Point is, by using dirty references to call directly methods of the main swf from a loaded swf you just create a system that is ready to break at any moment if you make any changes to both files. With a simple event system (which is even easier to write and implement) (loaded swf dispatching event, main swf catching it) you get a solid system that allows you to maintain, change, update everything without breaking anything.
__________________
aswebcreations
Super Duper!
ASWC is offline   Reply With Quote
Old 07-07-2012, 02:21 PM   #4
arkitx
Senior Member
 
Join Date: Jan 2011
Posts: 703
Default

@ASWC: Let him understand this first. I am Sure he will learn the right way too. Sometimes a Starter need the simple way first as that is his range of thinking. Me too want to use sometimes for small and chip coded apps. I know that is not good, but you also have to care of time and effort too.


__________________
Regards

arkitx
arkitx is offline   Reply With Quote
Old 07-07-2012, 03:58 PM   #5
[afz]snickelfitz
Senior Member
 
[afz]snickelfitz's Avatar
 
Join Date: Dec 2011
Location: Tucson, AZ
Posts: 2,331
Default

Good advice ASWC!
[afz]snickelfitz is offline   Reply With Quote
Old 07-07-2012, 07:43 PM   #6
ASWC
Super Moderator
 
ASWC's Avatar
 
Join Date: Dec 2007
Location: Greenville, SC
Posts: 6,528
Default

Quote:
Originally Posted by arkitx View Post
@ASWC: Let him understand this first.
Of course, I'm not criticizing anything here. The important thing for him is to get his system working. It's also important to point out "the right way" which in this case requires less code and is more efficient and flexible.

I remember working on a project last year with a very talented coder who had never used custom events and had the preconception that it required more code and was painful to work with and so on ... I introduced him to the powerful custom event system and he couldn't believe how easy and fast it was to write and setup. In fact, he realized that his system (similar to what we see on this thread) was the one time consuming and not reliable and using custom events was saving him hours of code writing and many headaches fixing communication between swf or classes. He is now of course using custom events on every projects.
__________________
aswebcreations
Super Duper!
ASWC is offline   Reply With Quote
Old 07-07-2012, 08:01 PM   #7
arkitx
Senior Member
 
Join Date: Jan 2011
Posts: 703
Default

@ASWC and [afz]snickelfitz I totally agree with you both.

I love your comment.

And I also believe that you will guide him in right direction by showing examples.



__________________
Regards

arkitx

Last edited by arkitx; 07-07-2012 at 08:04 PM.
arkitx is offline   Reply With Quote
Old 07-08-2012, 03:01 AM   #8
henke37
Senior Member
 
henke37's Avatar
 
Join Date: Mar 2009
Location: Sweden
Posts: 10,679
Send a message via Skype™ to henke37
Default

Thing is, events are not a perfect solution. You have to have a listener if you dispatch the event for things to work. Events invert the dependencies between the two pieces of code, now the one adding the listener needs to know of the event dispatches as opposed to the caller needing to know the callee. Not all designs work well with this inversion.

Events are great for observable objects, but they are not suitable for ordering things around.
__________________
Signature: I wrote a pair of articles about the timeline.
henke37 is online now   Reply With Quote
Old 07-10-2012, 12:15 AM   #9
nightkarnation
Senior Member
 
Join Date: Apr 2007
Posts: 114
Default

Thanks A LOT for your help guys!!
Very useful!!
nightkarnation is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 10:09 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.