|
-
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;
}
}
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|