Home Tutorials Forums Articles Blogs Movies Library Employment Press Buy templates

Go Back   ActionScript.org Forums > ActionScript Forums Group > ActionScript 3.0

Reply
 
Thread Tools Rate Thread Display Modes
Old 10-24-2008, 07:28 PM   #1
Xande!
Registered User
 
Join Date: Oct 2008
Posts: 4
Question Key Control inside MovieClip Class

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!
Xande! is offline   Reply With Quote
Old 10-24-2008, 09:17 PM   #2
ramywhite
Registered User
 
Join Date: Oct 2008
Posts: 5
Default

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:
package {     import flash.display.MovieClip;     import flash.display.DisplayObject;     import flash.events.*;     import flash.ui.Keyboard;         public class test extends MovieClip {     private var mc:MovieClip = new MovieClip();                 public function test():void {         mc.graphics.beginFill(0xff0000);             mc.graphics.drawRect(0, 0, 100, 100);             mc.graphics.endFill();             addChild(mc);                         mc.addEventListener(KeyboardEvent.KEY_DOWN, keyPressedDown);             mc.addEventListener(MouseEvent.CLICK, clickHandler);                     }          private function clickHandler(event:MouseEvent):void {             stage.focus = mc;         }         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;             }         }     } }

I think the next step is how to get focus without clicking on movie clip; by action script.
ramywhite is offline   Reply With Quote
Old 10-24-2008, 09:27 PM   #3
Hypnoz
Registered User
 
Join Date: Oct 2008
Posts: 6
Default

Try this out. I'm no expert so this may be totally wrong, but this is an idea.

ActionScript Code:
stage.addEventListener(KeyboardEvent.KEY_DOWN, hearKey); function hearKey(e:KeyboardEvent):void {         if (e.keyCode==Keyboard.RIGHT) {         if(mc.x == stage.stageWidth) { mc.x = stage.stageWidth }         else mc.x+=5     }     if (e.keyCode==Keyboard.LEFT) {         if(mc.x == 0) { mc.x = 0 }         else mc.x-=5     }     if (e.keyCode==Keyboard.UP) {         if(mc.y == 0) { mc.y = 0 }         else mc.y-=5     }     if (e.keyCode==Keyboard.DOWN) {         if(mc.y == stage.stageHeight) { mc.y = stage.stageHeight }         else mc.y+=5     } }
Hypnoz is offline   Reply With Quote
Old 10-25-2008, 12:12 AM   #4
Xande!
Registered User
 
Join Date: Oct 2008
Posts: 4
Default

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!
Xande! is offline   Reply With Quote
Old 10-25-2008, 09:41 PM   #5
Xande!
Registered User
 
Join Date: Oct 2008
Posts: 4
Default Stage

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!
Xande! is offline   Reply With Quote
Old 10-26-2008, 12:00 AM   #6
Mazoonist
Site Contributor
 
Mazoonist's Avatar
 
Join Date: Jun 2006
Posts: 2,194
Send a message via AIM to Mazoonist
Default

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:
package {     import flash.display.*;     import flash.events.*;     import flash.ui.Keyboard;     public class Test extends MovieClip {         private var leftArrow:Boolean;         private var rightArrow:Boolean;         private var upArrow:Boolean;         private var downArrow:Boolean;         private var speed:Number = 10;         public function Test():void {             addEventListener(Event.ADDED_TO_STAGE, addedHandler);         }         private function addedHandler(event:Event):void {             stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);             stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);             stage.addEventListener(Event.ENTER_FRAME, everyFrame);         }         private function keyPressed(event:KeyboardEvent):void {             switch (event.keyCode) {                 case Keyboard.LEFT :                     leftArrow = true;                     break;                 case Keyboard.RIGHT :                     rightArrow = true;                     break;                 case Keyboard.UP :                     upArrow = true;                     break;                 case Keyboard.DOWN :                     downArrow = true;                     break;             }         }         private function keyReleased(event:KeyboardEvent):void {             switch (event.keyCode) {                 case Keyboard.LEFT :                     leftArrow = false;                     break;                 case Keyboard.RIGHT :                     rightArrow = false;                     break;                 case Keyboard.UP :                     upArrow = false;                     break;                 case Keyboard.DOWN :                     downArrow = false;                     break;             }         }         private function everyFrame(event:Event):void {             if(leftArrow) {                 this.x -= speed;             }             if(rightArrow) {                 this.x += speed;             }             if(upArrow) {                 this.y -= speed;             }             if(downArrow) {                 this.y += speed;             }         }     } }
I tested this in a fla file, using a library MovieClip linked to this class. The code on frame 1 was:
ActionScript Code:
var ball:Test = new Test(); addChild(ball);
Now that I think of it, "Test" probably isn't a very good name, either. I would go for something more descriptive.
__________________
My Tutorials * My Website

Last edited by Mazoonist; 10-26-2008 at 12:04 AM..
Mazoonist is offline   Reply With Quote
Old 10-26-2008, 12:15 AM   #7
Xande!
Registered User
 
Join Date: Oct 2008
Posts: 4
Default

Mazoonist, thanks a lot for that reply, it's exactly the answer I was looking for!
Xande! is offline   Reply With Quote
Reply


Thread Tools
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

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


All times are GMT. The time now is 06:26 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2009 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.