dcsimg
A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: Very basic question about Classes

  1. #1
    Senior Member
    Join Date
    Feb 2003
    Posts
    129

    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!

  2. #2
    newb of many sorts Ralgoth's Avatar
    Join Date
    Apr 2002
    Posts
    466
    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");
    }
    Last edited by Ralgoth; 11-10-2009 at 09:04 AM.
    Search first, asked questions later.

  3. #3
    newb of many sorts Ralgoth's Avatar
    Join Date
    Apr 2002
    Posts
    466
    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. #4
    Senior Member
    Join Date
    Feb 2003
    Posts
    129
    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. #5
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    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. #6
    Mod cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,448
    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 | Flashscript Biz Classes/Components |

  7. #7
    Senior Member
    Join Date
    Feb 2003
    Posts
    129
    Thanks for the help everyone, my understanding has improved now

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center