| Home | Tutorials | Forums | Articles | Blogs | Movies | Library | Employment | Press | Buy templates |
|
|
#1 |
|
Registered User
Join Date: Oct 2008
Posts: 4
|
Ok, I wrote this code to test Keyboard Control from a MovieClip class in AS3, however before I could go on to create more practical uses for this code I got stuck. I have spent several hours rewriting and changing every little aspect of the code but no matter what I do it doesn't work.
The problem is it just doesn't respond. I get no compile errors. I get nothing. The Movieclip just sits there. Am I missing something obvious? Is what I am trying to do possible? I have written this code using things other than the Switch statement, but I cannot get it to work either way. I have seen some people add thier event handlers to the stage from inside the class but whenever I try that I get a compile error. Here's the code: // Code is written for a MovieClip exported for Actionscript package { import flash.display.*; //import flash.display.Stage; import flash.events.*; import flash.ui.Keyboard; import flash.events.KeyboardEvent; public class test extends MovieClip { public function test():void { addEventListener(KeyboardEvent.KEY_DOWN, keyPressedDown); } private function keyPressedDown(event:KeyboardEvent):void { var key:uint = event.keyCode; switch (key) { case Keyboard.LEFT : trace("Left"); break; case Keyboard.RIGHT : trace("Right"); break; case Keyboard.UP : trace("Up"); break; case Keyboard.DOWN : trace("Down"); break; } } } } Any help or suggestions would be much appreciated! |
|
|
|
|
|
#2 |
|
Registered User
Join Date: Oct 2008
Posts: 5
|
Hi,
I think you should add this event to movie clip or sprite object then you should make focus on it with mouse click: ActionScript Code:
I think the next step is how to get focus without clicking on movie clip; by action script. |
|
|
|
|
|
|
|
|
#3 |
|
Registered User
Join Date: Oct 2008
Posts: 6
|
Try this out. I'm no expert so this may be totally wrong, but this is an idea.
ActionScript Code:
|
|
|
|
|
|
#4 |
|
Registered User
Join Date: Oct 2008
Posts: 4
|
ramywhite, thanks for that help, i didn't realize that focus needed to be placed on the movieclip for it to work, i think i can work something up with that
hypnoz, thanks also, i think that code would work well but it wasn't exactly the an answer to my question now I have one more question. the movieclip I am working with is actually a movieclip exported for AS with several animated movieclips nested inside it. is there any way I can control a movieclip without using "stage.focus = mc" type code or is this how it has to be done? because I would like to use a MC created in FLASH instead of drawing it using AS, again thank you for your replies and any more help would be greatly appreciated! |
|
|
|
|
|
#5 |
|
Registered User
Join Date: Oct 2008
Posts: 4
|
whenever I try to access the stage form a class I get the error:
"Cannot access a property of method of null object reference..." does the location of the class file effect accessing the stage? is there some special way to access the stage?, besides importing flash.display.Stage; because that doesn't work! |
|
|
|
|
|
#6 |
|
Site Contributor
|
You must understand that stage is now a property of display objects, not the globally accessible thing it was in AS2. Since the class you are making is a MovieClip, it's a display object. So it has a stage property. However, until an object of your class is added to the stage with addChild (by some external code), it's stage property is null. After it's added with addChild, it's stage property will refer to the stage. The trick is to use the ADDED_TO_STAGE event in your class to detect that moment when an object of the class has been added to the stage.
This event is structured like any other event in AS3: you add an event listener to your class and create a handler function that gets called when the event happens. In this case, when your event happens, the object has been added to the stage, and then you can then add event listeners to the stage (Keyboard events must be added to the stage). Here's your class, with some revisions. To use it, just link it to your MovieClip symbol in the library. For more information on using AS3 classes, check the links in my signature. By the way, it's an incredibly bad idea to name a class with an initial lowercase letter. ActionScript Code:
ActionScript Code:
![]()
__________________
My new website: theflashconnection.com Last edited by Mazoonist; 10-26-2008 at 12:04 AM.. |
|
|
|
|
|
#7 |
|
Registered User
Join Date: Oct 2008
Posts: 4
|
Mazoonist, thanks a lot for that reply, it's exactly the answer I was looking for!
|
|
|
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Using the mx.util.Delegate class within your classes! | madgett | ActionScript 2.0 | 3 | 09-03-2007 12:11 AM |
| How to handle LoadVars inside a class? | ironchefmoto | ActionScript 2.0 | 0 | 07-27-2007 08:59 PM |
| text box inside movieclip class | amansoori | ActionScript 2.0 | 1 | 09-12-2006 09:34 PM |
| Call Class Method inside another class. | SecretAgentRege | ActionScript 2.0 | 3 | 05-03-2006 06:39 PM |
| How do I approach class dataMembers from a function inside a class method? | danielanvar | ActionScript 2.0 | 3 | 08-31-2005 08:32 AM |