;

PDA

Click to See Complete Forum and Search --> : Moving a Movie Clip


Hurdarr
01-17-2008, 02:17 PM
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:
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. :confused:


Thanks in advance.

fx.barrett
01-17-2008, 02:29 PM
Try something like this:

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.

5TonsOfFlax
01-17-2008, 02:31 PM
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

Hurdarr
01-17-2008, 05:13 PM
Try something like this:

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.

5TonsOfFlax
01-17-2008, 05:22 PM
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.