To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here


A Flash Developer Resource Site

Go Back   Flash Kit Community Forums > Flash Help > Actionscript 3.0

Reply
 
Thread Tools Search this Thread Rate Thread Display Modes
Old 11-10-2009, 05:03 AM   #1
RansomHostage
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();
Inside buttonWork.as is:

Code:
package {
	import flash.display.*;
	import flash.events.*;

	public class buttonWork extends MovieClip {
		public function buttonWork() {
			gotoAndStop("success");
		}
	}
}
This gives me the following error:
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!
RansomHostage is offline   Reply With Quote
Old 11-10-2009, 08:55 AM   #2
Ralgoth
newb of many sorts
 
Ralgoth's Avatar
 
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.
Ralgoth is offline   Reply With Quote
Old 11-10-2009, 09:04 AM   #3
Ralgoth
newb of many sorts
 
Ralgoth's Avatar
 
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.
Ralgoth is offline   Reply With Quote
Old 11-10-2009, 10:45 AM   #4
RansomHostage
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.
RansomHostage is offline   Reply With Quote
Old 11-10-2009, 11:49 AM   #5
5TonsOfFlax
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.
5TonsOfFlax is offline   Reply With Quote
Old 11-10-2009, 12:23 PM   #6
cancerinform
Mod
 
cancerinform's Avatar
 
Join Date: Mar 2002
Location: press the picture...
Posts: 12,312
Quote:
Originally Posted by 5TonsOfFlax View Post
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.
...and add
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 |
cancerinform is offline   Reply With Quote
Old 11-10-2009, 01:17 PM   #7
RansomHostage
Member
 
Join Date: Feb 2003
Posts: 98
Thanks for the help everyone, my understanding has improved now
RansomHostage is offline   Reply With Quote
Reply

Go Back   Flash Kit Community Forums > Flash Help > Actionscript 3.0

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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 -4. The time now is 10:28 AM.


internet.commerce
Be a Commerce Partner
 »  »  »  »  »  »  »
 »  »  »  »  »  »
 

    

Acceptable Use Policy


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.