dcsimg
A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: change direction w/ keyboard input

  1. #1
    Junior Member
    Join Date
    Oct 2007
    Posts
    10

    change direction w/ keyboard input

    as3 question:

    I have code that allows me to control the player by moving it around the stage with the arrows on my keyboard.

    what if I want the player's visual position to reflect the arrow as well.

    as in the arrow is pointing like this --> BUT if user clicks the left arrow the arrow not only starts moving towards the left but looks like <--

    the code I have is:

    as always, grateful for your help.

    Code:
    package {
    	import flash.display.*;
    	import flash.text.*;
    	import flash.events.*;
    
    	public class KB_Control extends MovieClip {
    		public function KB_Control() {
    
    			//from keyboard
    // initialize arrow variables
    var leftArrow:Boolean = false;
    var rightArrow:Boolean = false;
    var upArrow:Boolean = false;
    var downArrow:Boolean = false;
    
    // set event listeners
    stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressedDown);
    stage.addEventListener(KeyboardEvent.KEY_UP, keyPressedUp);
    stage.addEventListener(Event.ENTER_FRAME, movePlayer);
    
    // set arrow variables to true
    function keyPressedDown(event:KeyboardEvent) {
    	if (event.keyCode == 37) {
    		leftArrow = true;
    	} else if (event.keyCode == 39) {
    		rightArrow = true;
    	} else if (event.keyCode == 38) {
    		upArrow = true;
    	} else if (event.keyCode == 40) {
    		downArrow = true;
    	}
    }
    
    // set arrow variables to false
    function keyPressedUp(event:KeyboardEvent) {
    	if (event.keyCode == 37) {
    		leftArrow = false;
    	} else if (event.keyCode == 39) {
    		rightArrow = false;
    	} else if (event.keyCode == 38) {
    		upArrow = false;
    	} else if (event.keyCode == 40) {
    		downArrow = false;
    	}
    }
    
    // move every frame
    function movePlayer(event:Event) {
    	var speed:Number = 5;
    
    	if (leftArrow) {
    		player.x -= speed;
    	}
    	if (rightArrow) {
    		player.x += speed;
    	}
    	if (upArrow) {
    		player.y -= speed;
    	}
    	if (downArrow) {
    		player.y += speed;
    	}
    }
    		
    		}
    	}
    	
    }

  2. #2
    Junior Member
    Join Date
    Oct 2007
    Location
    Canada
    Posts
    14
    I found that using this._xscale = 100 for going right and changing the xscale value to -100 to go left worked for me.

    Here's my code

    PHP Code:
    onClipEvent(load) {
        
    speed=7;
    }
    onClipEvent(enterFrame){
        
    playerSound = new Sound(this);
        
    playerSound.attachSound("bee");

        if (
    Key.isDown(Key.RIGHT)) {
            
    _x+=speed;
            
    playerSound.start();
            if (
    _x >769) {
                
    _x 769;
            }
            
    this._xscale = -100;
        }
        if (
    Key.isDown(Key.LEFT)) {
            
    _x-=speed;
            
    playerSound.start();
            if (
    _x<27){
                
    _x=27;
            }
            
    this._xscale 100;
        }
        if (
    Key.isDown(Key.UP)) {
            
    _y-=speed;
            
    playerSound.start();
            if (
    _y<24) {
                
    _y=24;
            }
        }
        if (
    Key.isDown(Key.DOWN)) {
            
    _y+=speed;
            
    playerSound.start();
            if (
    _y>535) {
                
    _y=535;
            }
        }
        
    //trace("x:"+_x+"/y:"+_y);

    The x and y checks are making sure the player can't fly outside the viewable screen And I'm playing a buzzing sound as my bee floats around the screen collecting items

    Thanks
    Eliia

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