A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: Only rotating a clip upon key press

  1. #1
    bibuti. nolen's Avatar
    Join Date
    Sep 2002
    Location
    az.
    Posts
    191

    Only rotating a clip upon key press

    I'm working on some rotation/movement code where you press an arrow key, the clip rotates to the desired angle, and then moves in the desired direction once it has reached said angle.

    I'm having an issue forcing the clip to rotate in the proper direction towards the desired angle.

    http://www.nolentabner.com/test/keytest.swf

    If you follow the link above, press and hold the arrow keys to move and rotate the clip around the stage. Try rotating the clip all the way until it's facing down then press and hold the LEFT arrow key. Instead of rotating to the left, the clip rotates all the way around (counter-clockwise) until it is facing left.

    Code:
    var speed:int = 3;
    var dir:String = "right";
    var targetDir:String = " ";
    
    var keys:Object = new Object();
    
    keys[Keyboard.UP] = {down:false, dirx:0, diry:-1, rot:-90, dir:"up"};
    keys[Keyboard.DOWN] = {down:false, dirx:0, diry:1, rot:90, dir:"down"};
    keys[Keyboard.LEFT] = {down:false, dirx:-1, diry:0, rot:-180, dir:"left"};
    keys[Keyboard.RIGHT] = {down:false, dirx:1, diry:0, rot:0, dir:"right"};
    
    
    stage.addEventListener(KeyboardEvent.KEY_DOWN, downKeys);
    stage.addEventListener(KeyboardEvent.KEY_UP, upKeys);
    stage.addEventListener(Event.ENTER_FRAME, onFrame);
    
    function downKeys(e:KeyboardEvent):void
    {
        if(keys[e.keyCode] != undefined)
        {
            keys[e.keyCode].down = true;
            targetDir = keys[e.keyCode].dir;
    
        }
    }
    
    function upKeys(e:KeyboardEvent):void
    {
        if(keys[e.keyCode] != undefined)
        {
            keys[e.keyCode].down = false;
        }
    }
    
    function onFrame(e:Event):void
    {
       
        //trace(dir+" "+box.rotation+" "+targetDir);
    
        for each(var keyOb:Object in keys)
        {
            if(keyOb.down == true)
            {
                // if we are already at our target direction, move ahead
    
                if(box.rotation == keyOb.rot)
                {
                    dir = keyOb.dir;
    
                    box.x += speed*keyOb.dirx;
                    box.y += speed*keyOb.diry;
                }
                else
                {
                    // if our desired angle is less than our current angle, decrease
                    if(keyOb.rot < box.rotation)
                    {
    					box.rotation -= speed;
    
                    }
                    // if our desired angle is greater than our current angle, increase
                    else
                    {
    					box.rotation += speed;
                    }
                }
            }
        }
    }
    I understand that this is happening because technically the angle for the LEFT direction is less than the current angle when facing down (-180 and 90, respectively). What I'm curious about is how to accomplish proper rotation without having specific handlers for each direction. Wouldn't I need to use something like the distance formula and check it against 180, then use that to determine which direction to rotate?

    Sorry if this is lengthy, I just want to make sure it's understandable. Check the .swf and you will see what I'm trying to accomplish.
    i'm obsessed with video games.

  2. #2
    bibuti. nolen's Avatar
    Join Date
    Sep 2002
    Location
    az.
    Posts
    191
    moved this to games.
    Last edited by nolen; 05-05-2008 at 06:37 PM.
    i'm obsessed with video games.

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