LiTWol @ Oleg Terenchuk

  • Contact
  • About me

User login

What is OpenID?
To prevent automated spam submissions leave this field empty.
  • Log in using OpenID
  • Cancel OpenID login
  • Request new password

litwol's tweets

  • If a song is not tellig you which side to take. Then the song is making a terrible mistake. — 51 weeks 3 days ago
  • ‹‹
  • 2 of 9
  • ››
more
Home

Adding sprite from inside an external class onto stage and animating it using arrow key movement.

Submitted by litwol on Mon, 01/18/2010 - 03:41

Flash demo is at the bottom of the post, scroll there!

Initially i had difficulty attaching a sprite created inside a class onto the stage. I haven't found documentation yet that explains the right way of doing it but i did stumble onto my own solution. I will not know whether this is the right way of doing it until i find some documentation that explains it. If some one has a tip please leave a comment.

I instantiated my class from the main FLA using the following code passing 'this' into the construct. "This" represents /stage/ in this file :

import foo;
var d = new foo(this);

stage.addEventListener(KeyboardEvent.KEY_DOWN, d.myKeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, d.myKeyUp);

and here's the class itself ( for explanation on what it does read the end of the post ):

package {
  import flash.ui.Keyboard;
  import flash.events.KeyboardEvent;
  import flash.display.Sprite;

  public class foo extends Sprite {
    public var keys:Array = new Array();
    var sprite:Sprite = new Sprite();
   
    public function foo(stage):void {
      createAttachSprite(stage);
     
      keys[Keyboard.LEFT] = false;
      keys[Keyboard.RIGHT] = false;
      keys[Keyboard.DOWN] = false;
      keys[Keyboard.UP] = false;
    }
    private function createAttachSprite(stage) {
      sprite.graphics.beginFill(0x41C5D2, 1);
      sprite.graphics.drawRect(0, 0, 50, 50);
      sprite.graphics.endFill();     
      stage.addChild(sprite);
    }
    public function myKeyDown(event:KeyboardEvent):void {
      keys[event.keyCode] = true;
      processmove(event);
    }
    public function myKeyUp(event:KeyboardEvent):void {
      keys[event.keyCode] = false;
      processmove(event);
    }
    public function processmove(event:KeyboardEvent) {
      if ( keys[Keyboard.LEFT] == true) {
        sprite.x -= 5; 
      }
      if ( keys[Keyboard.UP] == true ) {
        sprite.y -= 5;
      }
      if ( keys[Keyboard.RIGHT] == true ) {
        sprite.x += 5;
      }
      if ( keys[Keyboard.DOWN] == true ) {
        sprite.y +=5;
      } 
    }
  }
}

In this example i was practicing simple arrow key controls. This code creates a simple 50x50 pixel sprite and attaches keyup and keydown event listeners to stage. when pressing the arrow keys i increment or decrement accordingly the x and y coordinate values of the sprite forcing it to move about the stage. The most interesting part is how i perform the movement. if you see my previous demo you will notice that you cannot perform object movement using 2 keys at the same time. pressing up + left will not move the object left while rotating it forward. Possibly the issue with dual key presses is because i registered single key event listeners and there was no "key press state" available that could tell you that 2 keys were pressed at the time, it could however tell you if one key was pressed or released. in the above example i added a simple registry that tracks all keys that were clicked and released, performing movement of the sprite on each key press. So in the above example if i press up and left the sprite will move diagonally as we would expect because as you see in processmove() it processes movement according to registry check for each of the arrow keys.

p.s. Don't mind the choice in naming convention. this is merely a sunday night practice demo which is likely not to make it into the final game design/implementation (movement handling is too choppy for my liking and i will likely revise it time based in later demos).

  • 1979 reads
Tags:
  • ActionScript 3
  • CS4
  • Flash

2 reponses to "Adding sprite from inside an external class onto stage and animating it using arrow key movement."

1. webroot informer

Submitted by Webroot (not verified) on Thu, 02/18/2010 - 06:30.

This page was indexed by Google and added to blogs search 18 Feb 2009 17:11:43 GMT.

Google cache: http://google.com/search?q=cache:http://litwol.com/content/adding-sprite-inside-external-class-stage-and-animating-it-using-arrow-key-movement&ei=AFQjCNHajN_OX0kgxzx7UGA1yBfsEX VIdsdfWq

Webroot informers.

  • reply

2. Time based movement processing

Submitted by litwol on Mon, 01/18/2010 - 03:56.

This example helps solve the issue where above example is dependent on how fast your key press repeat rate is as configured in your keyboard settings.

The following example performs time based movement. Key presses are now only important to register that they were clicked, they do not perform actual movement.

package {
  import flash.ui.Keyboard;
  import flash.events.KeyboardEvent;
  import flash.display.Sprite;
  import flash.utils.*;
 
  public class foo extends Sprite {
    public var keys:Array = new Array();
    var sprite:Sprite = new Sprite();
    public function foo(stage):void {
      createAttachSprite(stage);
     
      keys[Keyboard.LEFT] = false;
      keys[Keyboard.RIGHT] = false;
      keys[Keyboard.DOWN] = false;
      keys[Keyboard.UP] = false;
    }
    private function createAttachSprite(stage) {
      sprite.graphics.beginFill(0x41C5D2, 1);
      sprite.graphics.drawRect(0, 0, 50, 50);
      sprite.graphics.endFill();     
      stage.addChild(sprite);
      runmove();
    }
    public function myKeyDown(event:KeyboardEvent):void {
      keys[event.keyCode] = true;
    }
    public function myKeyUp(event:KeyboardEvent):void {
      keys[event.keyCode] = false;
    }
    public function runmove() {
      setInterval(processmove, 50);
    }
    public function processmove() {
      trace(keys);
      if ( keys[Keyboard.LEFT]) {
        sprite.x -= 5; 
      }
      if ( keys[Keyboard.UP]) {
        sprite.y -= 5;
      }
      if ( keys[Keyboard.RIGHT]) {
        sprite.x += 5;
      }
      if ( keys[Keyboard.DOWN]) {
        sprite.y +=5;
      } 
    }
  }
}
  • reply

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Lines and paragraphs break automatically.

More information about formatting options

To prevent automated spam submissions leave this field empty.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.

Recent posts

  • Developing PHP in VIM - VIM IDE for PHP
  • Creating custom fields using Drupal 7 field api
  • Creating 3d cube movieClip using Sprites and animating rotation through ActionsScript 3.0
  • Adding sprite from inside an external class onto stage and animating it using arrow key movement.
  • Playing with flash keyboard click events
  • Script to benchmark API execution time.
  • Very good dvcs guide
  • High throughput web architecture with drupal - *For authenticated users*
  • About me - Oleg Terenchuk
  • Note to self
  • Pre-generating drupal forms
  • Change memcached admin stats page
  • The cult of done

LiTWoL © Oleg Terenchuk - Hosted on Linode.com 512