package {
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class helloworld extends Sprite {
public static var x:int = 0;
public static var y:int = 0;
public function helloworld() {
graphics.lineStyle(1, 0, 1);
stage.focus = this;
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
}
private function onKeyDown(event:KeyboardEvent):void {
if (event.keyCode == Keyboard.DOWN)
{
y++;
graphics.moveTo(x,y);
graphics.drawCircle(x, y, 10);
}
if (event.keyCode == Keyboard.RIGHT)
{
x++;
graphics.moveTo(x,y);
graphics.drawCircle(x, y, 10);
}
}
}
The circles that are drawn first also move. How can I stop it from doing that?
You think you’re using your
public static x & yvalues, but actually you’re using the Sprite’s built inxandyproperties which control its location on the stage. When you usey++andx++it moves the entire sprite down/right.You should either make sure you’re always calling
helloworld.x&&helloworld.y(bad idea, easy to forget).OR
You should not use variables named
xandy. Try:circleXandcircleYor something that is more descriptive of what you’re using it for.