05-12-2012, 10:05 AM
|
#1
|
|
Registered User
Join Date: May 2012
Posts: 6
|
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
|
|
|
05-12-2012, 03:28 PM
|
#2
|
|
DontJustEnjoyIt,SvEnjoyIt
Join Date: Sep 2005
Location: California
Posts: 1,388
|
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?
|
|
|
05-12-2012, 05:10 PM
|
#3
|
|
Registered User
Join Date: May 2012
Posts: 6
|
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.
|
|
|
05-12-2012, 07:05 PM
|
#4
|
|
DontJustEnjoyIt,SvEnjoyIt
Join Date: Sep 2005
Location: California
Posts: 1,388
|
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.
|
|
|
05-13-2012, 01:18 AM
|
#5
|
|
Registered User
Join Date: May 2012
Posts: 6
|
Perfect, it works and I learned a precious method!
Thank you very much sven!!!
|
|
|
| Thread Tools |
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT. The time now is 09:25 PM.
///
|
|