user581734
user581734

Reputation: 1239

actionscript circle entire drawing moves why?

  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?

Upvotes: 0

Views: 93

Answers (1)

zzzzBov
zzzzBov

Reputation: 179284

You think you're using your public static x & y values, but actually you're using the Sprite's built in x and y properties which control its location on the stage. When you use y++ and x++ 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 x and y. Try: circleX and circleY or something that is more descriptive of what you're using it for.

Upvotes: 1

Related Questions