A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: Alternate keys to speed up, Friction

  1. #1
    Junior Member
    Join Date
    Jul 2006
    Posts
    8

    Question Alternate keys to speed up, Friction

    I'm attempting to make a racing game.

    I want to make it so the user has press two other keys alternately in order to speed up. While the user is not pressing those keys the player should slow down gradually.

    I'd also like to add friction.

    If anyone could help me out with either of these, it would be greatly appreciated.

    Right now, I have code for basic movement:

    Code:
    var keyPressed:uint;
    var rightKeyIsDown:Boolean;
    var leftKeyIsDown:Boolean;
    var upKeyIsDown:Boolean;
    var downKeyIsDown:Boolean;
    var speed:Number = 8;
    
    function initializeGame():void {
    	rightKeyIsDown=false;
    	leftKeyIsDown=false;
    	upKeyIsDown=false;
    	downKeyIsDown=false;
    
    	stage.addEventListener(KeyboardEvent.KEY_DOWN, pressKey);
    	stage.addEventListener(KeyboardEvent.KEY_UP, releaseKey);
    }
    
    function pressKey(event:KeyboardEvent):void {
    	keyPressed=event.keyCode;
    
    	switch (keyPressed) {
    		case (Keyboard.RIGHT) :
    			rightKeyIsDown=true;
    			break;
    		case (Keyboard.LEFT) :
    			leftKeyIsDown=true;
    			break;
    		case (Keyboard.UP) :
    			upKeyIsDown=true;
    			break;
    		case (Keyboard.DOWN) :
    			downKeyIsDown=true;
    			break;
    	}
    	player_mc1.addEventListener(Event.ENTER_FRAME, movePlayer1);
    }
    
    function releaseKey(event:KeyboardEvent):void {
    	var thisKey:uint=event.keyCode;
    	switch (thisKey) {
    		case (Keyboard.RIGHT) :
    			rightKeyIsDown=false;
    			break;
    		case (Keyboard.LEFT) :
    			leftKeyIsDown=false;
    			break;
    		case (Keyboard.UP) :
    			upKeyIsDown=false;
    			break;
    		case (Keyboard.DOWN) :
    			downKeyIsDown=false;
    			break;
    	}
    }
    
    function movePlayer1(event:Event):void {
    	if (rightKeyIsDown && !player_mc1.hitTestObject(wallRight)) {
    		player_mc1.x+=speed;
    		player_mc1.rotation=0;
    	}
    	if (leftKeyIsDown && !player_mc1.hitTestObject(wallLeft)) {
    		player_mc1.x-=speed;
    		player_mc1.rotation=180;
    	}
    	if (upKeyIsDown && !player_mc1.hitTestObject(wallUp)) {
    		player_mc1.y-=speed;
    		player_mc1.rotation=270;
    	}
    	if (downKeyIsDown && !player_mc1.hitTestObject(wallDown)) {
    		player_mc1.y+=speed;
    		player_mc1.rotation=90;
    	}
    	if (rightKeyIsDown && downKeyIsDown) {
    		player_mc1.rotation=45;
    	}
    	if (rightKeyIsDown && upKeyIsDown) {
    		player_mc1.rotation=315;
    	}
    	if (leftKeyIsDown && downKeyIsDown) {
    		player_mc1.rotation=135;
    	}
    	if (leftKeyIsDown && upKeyIsDown) {
    		player_mc1.rotation=225;
    	}
    }

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    The alternating keypresses is a good game mechanic, but may be tricky to build. Don't let that discourage you though.

    One approach would be to keep track of which of the two keys was last pressed, and if the current pressed key is different, bump up the speed by a bit. Then set the last pressed to that key. You should probably make the bump quite small, but you can play with the exact values to get a feel for it.

    As for friction, its simply a matter of multiplying speed by some factor less than (but close to) 1 each frame.

Tags for this Thread

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