Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 05-12-2012, 10:05 AM   #1
fabyo
Registered User
 
Join Date: May 2012
Posts: 6
Unhappy Pass function between classes?

Hi, I'm an AS3 beginner and I need some help.
I bumped into a problem that I can't solve by myself.
My application has 3 pages (page1, page2 and page3) and every page is an external .as class.

In my .fla file I have the main class to create all the instances of the pages (one within the stage width and the others outside it) and also the function to move them right and left on the stage according to the relative clicked menu icon (m1, m2, m3).

In page1 I have a movie clip which is supposed to do, with an event listener, the same of the function in the main class, moving the pages instances to the left as m3 icon does.

Just to explain, if I click on that movie clip in page1 I want to show the page 3 on the stage.
How can I use the function in the main class from an instance located in another external class?
Hope someone can please help me asap cause I have no idea!!!

Thanks in advance
fabyo is offline  
Old 05-12-2012, 03:28 PM   #2
svenjoypro
DontJustEnjoyIt,SvEnjoyIt
 
svenjoypro's Avatar
 
Join Date: Sep 2005
Location: California
Posts: 1,389
Default

If your page classes are children of your main class (in other words do you have something like this on your main function: addChild(page1)), then you can access a function of the main class from within your page class like this:
ActionScript Code:
//Inside Page1 Object(this.parent).functionName();

If the pages aren't children of your main class, then you will need to pass in an instance of the main class to the pages. Something like this:
ActionScript Code:
//Inside Main var page1:Page1 = new Page1(this); //Inside Page1 function Page1(o:Object):void {     //Then you can access the Main class's function like this:     o.functionName(); }

Does that make sense?
svenjoypro is offline  
Old 05-12-2012, 05:10 PM   #3
fabyo
Registered User
 
Join Date: May 2012
Posts: 6
Default

Yes, of course it does make sense!!! Thank you!
I just tried it right now, it works but there is something more.
My Page1 class is a child of the main class but the function I want to call in page1 has an event:MouseEvent that refers to a currentTarget which is an index of an array.
Something like this:
ActionScript Code:
public function clicked(evt:MouseEvent):void{                 if(evt.currentTarget.id == 0){                     TweenLite.to(allPages[0], 0.5, {x:posX1});                     TweenLite.to(allPages[1], 0.5, {x:posX2});                     TweenLite.to(allPages[2], 0.5, {x:posX3});                 }else if(evt.currentTarget.id == 1){                     TweenLite.to(allPages[0], 0.5, {x:posX3});                     TweenLite.to(allPages[1], 0.5, {x:posX1});                     TweenLite.to(allPages[2], 0.5, {x:posX2});                 }else if(evt.currentTarget.ident == 2){                     TweenLite.to(allPages[0], 0.5, {x:posX2});                     TweenLite.to(allPages[1], 0.5, {x:posX3});                     TweenLite.to(allPages[2], 0.5, {x:posX1});                 }

While I'm calling the function with your first code, flash tells me there is a missing argument obviously. Do I have to pass the array values from main to page1 (if yes...how??)
Thank you again.
fabyo is offline  
Old 05-12-2012, 07:05 PM   #4
svenjoypro
DontJustEnjoyIt,SvEnjoyIt
 
svenjoypro's Avatar
 
Join Date: Sep 2005
Location: California
Posts: 1,389
Default

I would do something like this:
ActionScript Code:
public function clicked(evt:MouseEvent):void{     tweenPages(int(evt.currentTarget.id)); } public function tweenPages(id:int) {                 if(id == 0){                     TweenLite.to(allPages[0], 0.5, {x:posX1});                     TweenLite.to(allPages[1], 0.5, {x:posX2});                     TweenLite.to(allPages[2], 0.5, {x:posX3});                 }else if(id == 1){                     TweenLite.to(allPages[0], 0.5, {x:posX3});                     TweenLite.to(allPages[1], 0.5, {x:posX1});                     TweenLite.to(allPages[2], 0.5, {x:posX2});                 }else if(id == 2){                     TweenLite.to(allPages[0], 0.5, {x:posX2});                     TweenLite.to(allPages[1], 0.5, {x:posX3});                     TweenLite.to(allPages[2], 0.5, {x:posX1});                 } }
And call tweenPages(x) from your page1 class substituting x for whatever int you want.
svenjoypro is offline  
Old 05-13-2012, 01:18 AM   #5
fabyo
Registered User
 
Join Date: May 2012
Posts: 6
Talking

Perfect, it works and I learned a precious method!
Thank you very much sven!!!
fabyo is offline  
Closed Thread


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 11:42 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.