• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 276
  • Last Modified:

actionscript keypress multiple keys

Hey guys,

I'm creating a game with gravity, which is working good at this moment. But I've got a small problem with movement and changing gravity.

For example when you move right, and then press the gravity key (g) you stop moving right after a second while you're in air. I want the player to be able to move left/right while in air and also when you hold right, hit gravity, it should be still moving right.

I know the problem is in the keyPressedUp code, but I cant seem to fix
dimension-switch-CS4.fla
0
ErnstJacobs
Asked:
ErnstJacobs
  • 4
1 Solution
 
dgofmanCommented:
It is working,
I will recommend to use lower and upper keys

function keyPressedDown(e:KeyboardEvent) {
      var key:uint = e.keyCode;
      var char:String = String.fromCharCode(e.charCode);
      lastKeyCodePressed = e.keyCode;
      
      //trace(e.keyCode);
      if(char == "N" || char == "n") {
            if(plr_action == "NEW") {
                  plr_action = "START";
            }
            if(plr_action != "NEW" && plr_action != "START") {
                  plr_action = "NEW"
            }
      }
      if(char == "I" || char == "i") {
            plr_action = "HELP";
      }
      if(char == "Q" || char == "q") {
            if(menu.visible) {
                  plr_action = "EXIT";
            }
            if(!menu.visible) {
                  plr_action = "QUIT";
            }
      }
      
      if(key == Keyboard.LEFT) {
            plr_direction = "LEFT";
      }
      if(key == Keyboard.RIGHT) {
            plr_direction = "RIGHT";
      }
      if(char == "G" || char == "g") {
            plr_action = "GRAVITY";
      }
} //keyPressedDown
0
 
dgofmanCommented:
After couple glass of coffee I finally start understanding what you want.

Ok I hope you will understand my code better
import flash.display.Stage;
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.Event;
import flash.text.TextField;

var GRAVITY:uint = Keyboard.F1;

var mainText:TextField = menu.main_text as TextField;
var helpText:TextField = menu.help_text as TextField;
var optionsText:TextField = menu.options_text as TextField;
var continueText:TextField = menu.continue_text as TextField;
var scoreText:TextField = time as TextField;
var scoreTimer:Timer = new Timer(50, 0);

//variables player
var player:MovieClip = player;
var lastDownKeyChar:String;
var pressedKeys:Object = {};
var plr_inAir:Boolean = true;
var IMAGE_SIZE:uint = 1250;

//variables movement
var velocityY:Number = 0;
var velocityYMax:Number = 5;
var velocityYMaxReverse:Number = -5;
var acceleration:Number = 0.5;
var friction:Number = 0.89;

//variables level
var lvl_bottom:uint = 350;
var lvl_top:uint = 0;
var lvl_left:uint = 10;
var lvl_gravity:Boolean = true;

//arrays
var hwalls:Array = new Array(h_wall0, h_wall1, h_wall2, h_wall3, h_wall4, h_wall5, h_wall6, h_wall7, h_wall8, h_wall9, h_wall10, h_wall11, h_wall12, h_wall13);
var vwalls:Array = new Array(v_wall1, v_wall2, v_wall3);
var enemy:Array = new Array(enemy1, enemy2, enemy3);
var bullets:Array = new Array(bullet1, bullet2, bullet3, bullet4);


//initiate
showMenu(true);
mainText.text = "Dimension Switch";
stage.frameRate = 29;

//Events
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressedDown);
stage.addEventListener(KeyboardEvent.KEY_UP, keyPressedUp);
scoreTimer.addEventListener(TimerEvent.TIMER, getScore);

//functions
function showMenu(b:Boolean) {
	menu.visible = b;
	mainText.visible = true;
	optionsText.visible = true;
	helpText.visible = false;
	continueText.visible = false;
	stage.removeEventListener(Event.ENTER_FRAME, onTick);
	scoreTimer.stop();
}

function onTick(e:Event) {
	playerMove();
	playerCollide();
	getGravity();
}

function getScore(event:TimerEvent) {
	scoreText.text = "Time: " + scoreTimer.currentCount.toString();
}

function reset() {
	//reset variables
	mainText.text = "Dimension Switch";
	scoreTimer.reset();
	player.x = 15;
	player.y = 150;
	
	plr_inAir = true;
	lvl_gravity = true;
	scoreTimer.start();
	
	if(backGround.x != IMAGE_SIZE){
		backGround.x = IMAGE_SIZE;
		for(var i = 0; i < hwalls.length; i++) {
			hwalls[i].x += IMAGE_SIZE;
		}
		for(var c = 0; c < vwalls.length; c++) {
			vwalls[c].x += IMAGE_SIZE;
		}
		for(var e = 0; e < enemy.length; e++) {
			enemy[e].x += IMAGE_SIZE;
		}
		finish.x += IMAGE_SIZE;
	}
	for(var b = 0; b < bullets.length; b++) {
		bullets[b].visible = false;
	}
}

function getGravity() {
	if(plr_inAir) {
		if(lvl_gravity == true) {
			velocityY -= 0.5;
		}
		if(lvl_gravity == false) {
			velocityY += 0.5;
		}
		velocityY *= friction;
		player.y -= velocityY;		
	}
} //getGravity

function playerCollide() {
	for(var i = 0; i < hwalls.length; i++) {
		if(player.hitTestObject(hwalls[i])) {
			plr_inAir = false;
			//velocityY = 0;
		}
	}
	for(var c = 0; c < vwalls.length; c++) {
		if(player.hitTestObject(vwalls[c])) {
			player.x -= 2;
		}
	}
	for(var e = 0; e < enemy.length; e++) {
		if(player.hitTestObject(enemy[e])) {
			showMenu(true);
			mainText.text = "Game Over";
		}
	}
	for(var b = 0; b < bullets.length; b++) {
		if(player.hitTestObject(bullets[b])) {
			showMenu(true);
			mainText.text = "Game Over";
		}
	}
	if(player.hitTestObject(finish)) {
		showMenu(true);
		mainText.text = "You Win time " + scoreTimer.currentCount; 
	}
	if(player.y <= lvl_top || player.y >= lvl_bottom) {
		showMenu(true);
		mainText.text = "Game Over";
	}
} //playerCollide

function playerMove() {
	trace("LEFT: " + (pressedKeys[Keyboard.LEFT] == true) + 
		  ", RIGHT: " + (pressedKeys[Keyboard.RIGHT] == true) + 
		  ", GRAVITY: " + (pressedKeys[GRAVITY] == true));
	//Move Left or Right
	if(pressedKeys[Keyboard.LEFT] == true) {
		player.x -= 2;
	}else if(pressedKeys[Keyboard.RIGHT] == true) {
		player.x += 2;
	}

	//And can be GRAVITY
	if(pressedKeys[GRAVITY] == true){
		if(!plr_inAir){
			if(lvl_gravity) {
				lvl_gravity = false;
				plr_inAir = true;
				player.y -= 10;
			}
			else {
				lvl_gravity = true;
				plr_inAir = true;
				player.y += 10;
			}
		}
	}
	velocityY *= friction;
	moveLevel();

	
	if(player.x <= lvl_left) {
		player.x += 2;
	}
} //movePlayer

function moveLevel() {	
	if(player.x > IMAGE_SIZE) {
		backGround.x -= IMAGE_SIZE;
		
		for(var i = 0; i < hwalls.length; i++) {
			hwalls[i].x -= IMAGE_SIZE;
		}
		for(var c = 0; c < vwalls.length; c++) {
			vwalls[c].x -= IMAGE_SIZE;
		}
		for(var e = 0; e < enemy.length; e++) {
			enemy[e].x -= IMAGE_SIZE;
		}
		
		finish.x -= IMAGE_SIZE;
		player.x = 0;
	}
}

function keyPressedDown(e:KeyboardEvent) {
	var char:String = String.fromCharCode(e.charCode).toUpperCase();
	
	if(char != "C" && (lastDownKeyChar == "H")){ //wait till user will not press continue key
		return;
	}
	
	switch(char){
		case "H":
			stage.removeEventListener(Event.ENTER_FRAME, onTick);
			menu.visible = true;
			continueText.visible = true;
			helpText.visible = true;
			mainText.visible = false;
			optionsText.visible = false;
			break;
		case "Q":
			showMenu(true);
			fscommand("quit", "");
			break;
		case "C":
			if(lastDownKeyChar == "H" || lastDownKeyChar == "Q"){
				showMenu(false);
				stage.addEventListener(Event.ENTER_FRAME, onTick);
				break;
			}
		case "N":
			showMenu(false);
			reset();
			stage.addEventListener(Event.ENTER_FRAME, onTick);
			break;
	}
	
	if(e.keyCode == Keyboard.LEFT || e.keyCode == Keyboard.RIGHT) {
		pressedKeys[e.keyCode] = true;
	}else if(char == "G"){
		pressedKeys[GRAVITY] = true;
	}
	lastDownKeyChar = char;
} //keyPressedDown

function keyPressedUp(e:KeyboardEvent):void {
	var char:String = String.fromCharCode(e.charCode).toUpperCase();
	if(char == "G"){
		delete pressedKeys[GRAVITY];
	}else{
		delete pressedKeys[e.keyCode];
	}
} //keyPressedUp

Open in new window

0
 
dgofmanCommented:
0
 
ErnstJacobsAuthor Commented:
Thank you dgofman! The script looks much cleaner, and the mutiple keys thing work.
But what I can't really understand is how the pressedKeys variable works. You made it an object variable, but I do not really understand how it works.

Can you explain it to me? Because you make it like pressedKeys[GRAVITY] = true
and I have no idea how that works.
0
 
dgofmanCommented:
Check on top new variable declaration

var GRAVITY:uint = Keyboard.F1;

Now when user press down any buttons I am validating and storing only those keys

if(e.keyCode == Keyboard.LEFT || e.keyCode == Keyboard.RIGHT) {
            pressedKeys[e.keyCode] = true;
      }else if(char == "G"){
            pressedKeys[GRAVITY] = true;
      }

Unil you holding those keys you can get status by using

trace(pressedKeys[Keyboard.LEFT]):
trace(pressedKeys[Keyboard.RIGHT]):
trace(pressedKeys[GRAVITY]):

If you will release some key I am deleting from a object (hashmap)

function keyPressedUp(e:KeyboardEvent):void {
      var char:String = String.fromCharCode(e.charCode).toUpperCase();
      if(char == "G"){
            delete pressedKeys[GRAVITY];
      }else{
            delete pressedKeys[e.keyCode];
      }
}
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now