|
-
Moving a Movie Clip
Hi folks,
I'm new to Flash and Actionscript and I'm trying to move a movie clip with the directional arrows, using AS 3.0.
The old code I had:
Code:
var speed = 7;
this.onEnterFrame = function() {
if (Key.isDown(Key.UP)) {
square._y -= speed;
}
if (Key.isDown(Key.DOWN)) {
square._y += speed;
}
if (Key.isDown(Key.LEFT)) {
square._x -= speed;
}
if (Key.isDown(Key.RIGHT)) {
square._x += speed;
}
};
The code is broken for AS 3.0 and I don't how to fix it.
Thanks in advance.
-
trace("AKA: Biro Barna");
Try something like this:
PHP Code:
package { import flash.display.Sprite; import flash.events.KeyboardEvent; import flash.ui.Keyboard; public class KeyCodes extends Sprite { protected var ball:Sprite; public function KeyCodes() { init(); } protected function init():void { ball = new Sprite(); addChild(ball); ball.graphics.beginFill(0xff0000); ball.graphics.drawCircle(0, 0, 40); ball.graphics.endFill(); ball.x = stage.stageWidth / 2; ball.y = stage.stageHeight / 2; stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyboardEvent); } protected function onKeyboardEvent(event:KeyboardEvent):void { switch(event.keyCode) { case Keyboard.UP : ball.y -= 10; break; case Keyboard.DOWN : ball.y += 10; break; case Keyboard.LEFT : ball.x -= 10; break; case Keyboard.RIGHT : ball.x += 10; break; default : break; } } } }
Good luck.

| Windows MSN: birobarna [at] hotmail [dot] com | Skype: barna.biro | WebLog: http://blog.wisebisoft.com/ |
| Software Developer / Flash & Flex Developer | Student ( Computer Science ) | Interested in: Sharing Knowledge |
| Romanian Adobe Flash, Flex, AIR Forum: http://www.flashforum.ro/ | By perseverance the snail reached the ark. |
-
 Originally Posted by PlenaryCreation
Try something like this:
PHP Code:
package { import flash.display.Sprite; import flash.events.KeyboardEvent; import flash.ui.Keyboard; public class KeyCodes extends Sprite { protected var ball:Sprite; public function KeyCodes() { init(); } protected function init():void { ball = new Sprite(); addChild(ball); ball.graphics.beginFill(0xff0000); ball.graphics.drawCircle(0, 0, 40); ball.graphics.endFill(); ball.x = stage.stageWidth / 2; ball.y = stage.stageHeight / 2; stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyboardEvent); } protected function onKeyboardEvent(event:KeyboardEvent):void { switch(event.keyCode) { case Keyboard.UP : ball.y -= 10; break; case Keyboard.DOWN : ball.y += 10; break; case Keyboard.LEFT : ball.x -= 10; break; case Keyboard.RIGHT : ball.x += 10; break; default : break; } } } }
Good luck.
Thanks for the input, but I get the following error: 1037: Packages cannot be nested.
-
Much has changed from as2. You should probably do a little introductory reading on that before diving in. Check the resources sticky thread on top of the forum for links.
In a nutshell, the "on" handlers have been removed in favor of addEventListener. Also all properties that used to be preceded by an underscore ("_") now do not have the underscore. Also also, Key.isDown is no longer native, though senocular has re-implemented it in a package that I've found useful. http://senocular.com/flash/actionscript.php
-
You just copied and pasted that into the actions of a fla, didn't you?
Don't do that. Classes should be defined in .as files which match the class name, and in a directory structure which matches the package name.
Since, in this case, the package name is empty, the .as file should live somewhere directly in your classpath. The same directory as the fla is a good bet.
Copy and paste that into a new file, and save that file as KeyCodes.as
Then, to use it as your document class, set the document class in the fla.
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
|