1 Reply Latest reply on Jul 1, 2010 9:50 AM by Flex harUI

    Moving a Sprite

    Warentester

      Hello everyone,

       

      it would be great if you could help me out with this.

       

      I would like to move the Sprite i created whenever I push a Button.

       

      I wrote some code but it didn't work.

       

      Thanks a lot in advance!

       

      Here is the code.

       

      MXML-File:

      <?xml version="1.0" encoding="utf-8"?>
      <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
                        xmlns:s="library://ns.adobe.com/flex/spark" 
                        xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" xmlns:local="*">
           <fx:Declarations>
                
           </fx:Declarations>
           <local:KeyboardMove /> 
           
      
      </s:Application>
      
      

       

      package
      {
           import flash.display.Graphics;
           import flash.display.Sprite;
           import flash.events.KeyboardEvent;
           
           import mx.core.UIComponent;
           
           
      
           public class KeyboardMove extends UIComponent
           {
                public var quad:Sprite;
                
                public function KeyboardMove() 
                {
                     quad = new Sprite();
                     quad.graphics.beginFill(0x00ff00);
                     quad.graphics.lineStyle(1);
                     quad.graphics.drawRect(0,0,20,20);
                     
                     addChild(quad);
                     quad.focusRect = true;
                     
                     addEventListener(KeyboardEvent.KEY_DOWN,myMove);
                     
                
                     
                }
      
                
                public function myMove(e:KeyboardEvent):void
                {
                     quad.x += 20;
                }
           }
      }