-
(as3) Classes and methods
Okay... I'm trying to call methods from a class to no avail. It just tells me that I have undefined methods.
Why?
main.as:
Actionscript Code:
package { import flash.display.Sprite; import flash.events.Event; import _display.as; /** * ... * @author devon */ public class Main extends Sprite { public function Main():void { if (stage) init(); else addEventListener(Event.ADDED_TO_STAGE, init); } private function init(e:Event = null):void { removeEventListener(Event.ADDED_TO_STAGE, init); // entry point movie(); txt(); } } }
_display.as
Actionscript Code:
package { import flash.display.*; import flash.text.TextField;
public class _display extends MovieClip { public var mc:MovieClip = new MovieClip(); public function movie():void { mc.height = stage.stageHeight; mc.width = stage.stageWidth; mc.x = 0; mc.y = 0; stage.addChild(mc); trace("mc created"); } public var myText:TextField = new TextField(); public function txt():void { myText.text = "Yo."; mc.addChild(myText); } } }
-
Flash/Flex Developer
You did not instantiate the class file. If you want to call the movie function within the _display class, you have to let Flash know where and what it is.
actionscript Code:
package { import flash.display.Sprite; import flash.events.Event; import _display; /** * ... * @author devon */ public class Main extends Sprite { private var thisDisplay:_display = new _display();
public function Main():void { if (stage) init(); else addEventListener(Event.ADDED_TO_STAGE, init); } private function init(e:Event = null):void { removeEventListener(Event.ADDED_TO_STAGE, init); // entry point thisDisplay.movie(); thisDisplay.txt(); } } }
One other thing, there is a sort of a industry standard when it comes to naming your class file. They usually begin with a capital letter and not an underscore. This makes it easier for anyone else who many have to edit for material to understand that the class file is a custom class and not necessarily within the current document.
Some people are like Slinkies, not really good for anything, but they bring a smile to your face when pushed down the stairs.
-
I see... Thanks a lot!!!!! But now I get this:
[Fault] exception, information=TypeError: Error #1009: Cannot access a property or method of a null object reference.
Any thoughts?
-
Flash/Flex Developer
Can you be more specific as to where the error is pointing?
Some people are like Slinkies, not really good for anything, but they bring a smile to your face when pushed down the stairs.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|