A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: Moving a Movie Clip

Hybrid View

  1. #1
    Senior Member
    Join Date
    Jan 2008
    Posts
    107

    Moving a Movie Clip

    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:
    Code:
    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.


    Thanks in advance.

  2. #2
    trace("AKA: Biro Barna");
    Join Date
    Oct 2007
    Location
    RO.Timişoara
    Posts
    1,403
    Try something like this:

    PHP Code:
    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(0040);
                
    ball.graphics.endFill();
                
    ball.stage.stageWidth 2;
                
    ball.stage.stageHeight 2;
                
    stage.addEventListener(KeyboardEvent.KEY_DOWNonKeyboardEvent);
            }
            protected function 
    onKeyboardEvent(event:KeyboardEvent):void
            
    {
                switch(
    event.keyCode)
                {
                    case 
    Keyboard.UP ball.-= 10;
                    break;
                    
                    case 
    Keyboard.DOWN ball.+= 10;
                    break;
                    
                    case 
    Keyboard.LEFT ball.-= 10;
                    break;
                    
                    case 
    Keyboard.RIGHT ball.+= 10;
                    break;
                    
                    default : break;
                }
            }
        }

    Good luck.



    | Windows MSN: birobarna [at] hotmail [dot] com | Skype: barna.biro |
    WebLog: http://blog.wisebisoft.com/ |
    | Software Developer / Flash & Flex Developer | Student ( Computer Science ) | Interested in: Sharing Knowledge |
    |
    Romanian Adobe Flash, Flex, AIR Forum: http://www.flashforum.ro/
    | By perseverance the snail reached the ark. |


  3. #3
    Senior Member
    Join Date
    Jan 2008
    Posts
    107
    Quote Originally Posted by PlenaryCreation
    Try something like this:

    PHP Code:
    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(0040);
                
    ball.graphics.endFill();
                
    ball.stage.stageWidth 2;
                
    ball.stage.stageHeight 2;
                
    stage.addEventListener(KeyboardEvent.KEY_DOWNonKeyboardEvent);
            }
            protected function 
    onKeyboardEvent(event:KeyboardEvent):void
            
    {
                switch(
    event.keyCode)
                {
                    case 
    Keyboard.UP ball.-= 10;
                    break;
                    
                    case 
    Keyboard.DOWN ball.+= 10;
                    break;
                    
                    case 
    Keyboard.LEFT ball.-= 10;
                    break;
                    
                    case 
    Keyboard.RIGHT ball.+= 10;
                    break;
                    
                    default : break;
                }
            }
        }

    Good luck.
    Thanks for the input, but I get the following error: 1037: Packages cannot be nested.

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    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

  5. #5
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center