|
|
|
#1 |
|
Member
Join Date: Feb 2003
Posts: 98
|
Very basic question about Classes
Hello,
Yesterday I began to study Actionscript 3 for the first time. I did a lot of reading and I think I understand the main principals behind it. What's confusing me is actually how to put it into practise. I have testpage.fla and buttonWork.as. On the stage of testpage.fla I've placed a movieclip (it's not been Linked in any kind of way) and given it the instance name "theButton". There are ten frames on the timeline, with the tenth frame being labelled "success" and the first containing the following code: Code:
theButton.addEventListener(MouseEvent.CLICK, buttonWork); stop(); Code:
package {
import flash.display.*;
import flash.events.*;
public class buttonWork extends MovieClip {
public function buttonWork() {
gotoAndStop("success");
}
}
}
1067: Implicit coercion of a value of type Class to an unrelated type Function. I don't really understand what that means. What I would like someone to tell me is if I've actually grasped the concept of how using external .as files should be done. Should the EventListener be placed in the external .as file instead of the timeline? Should the MC be Linked? Does "gotoAndStop" cause a problem being written the way I've done it? Could somebody please answer these questions and also rearrange my code so I can see the exact correct way of doing things? As I mentioned, I'm completely new to this so being shown the precise way to go about this would be perfect. Thank you very much! |
|
|
|
|
|
#2 |
|
newb of many sorts
Join Date: Apr 2002
Posts: 443
|
eventListeners need to have a function as the second parameter. buttonWork is not a function, it's a class...
public class buttonWork extends MovieClip { If you're working in the timeline, it will be easiest to keep all your code there. Code:
theButton.addEventListener(MouseEvent.CLICK, buttonClick);
stop();
function buttonClick(e:MouseEvent) {
gotoAndStop("success");
}
__________________
Search first, asked questions later. Last edited by Ralgoth; 11-10-2009 at 09:04 AM. |
|
|
|
|
|
#3 |
|
newb of many sorts
Join Date: Apr 2002
Posts: 443
|
sorry, I only half read your post (my fault, obviously)
Here is how you would do it in your Class file Code:
package {
import flash.display.*;
import flash.events.*;
public class buttonWork extends MovieClip {
public function buttonWork()
{
theButton.addEventListener(MouseEvent.CLICK, clickEvent);
}
private function clickEvent(e:MouseEvent):void
{
gotoAndStop('success');
}
}
}
__________________
Search first, asked questions later. |
|
|
|
|
|
#4 |
|
Member
Join Date: Feb 2003
Posts: 98
|
Thanks for the response, Ralgoth! I think I understand how to use functions better now.
Sadly, the code doesn't work. Clicking on the button does nothing (I replaced the gotoAndStop with a trace and it doesn't show when clicked). Is it because the MC is not Linked in any way? Or do I need some code on the main timeline to import the buttonWorks class? Edit: I hadn't set the Document Class. Thanks again for your help! Last edited by RansomHostage; 11-10-2009 at 11:45 AM. |
|
|
|
|
|
#5 |
|
Will moderate for beer
Join Date: Apr 2007
Location: Austin, TX
Posts: 4,350
|
Ralgoth's first code requires no external .as file, it is all timeline code.
Ralgoth's second code is all external .as file. The only thing it requires is that you set the "Document Class" of your fla to buttonWork. |
|
|
|
|
|
#6 | |
|
Mod
Join Date: Mar 2002
Location: press the picture...
Posts: 12,312
|
Quote:
public var theButton:MovieClip (if it is a MovieClip).
__________________
![]() - The right of the People to create Flash movies shall not be infringed. - | www.Flashscript.biz | Help a little girl, Ana, who has cancer. | Flashscript Biz Classes/Components | The new Event design pattern | Clothing for Haiti | |
|
|
|
|
|
|
#7 |
|
Member
Join Date: Feb 2003
Posts: 98
|
Thanks for the help everyone, my understanding has improved now
|
|
|
|
![]() |
|
||||||
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|