View Full Version : Character Movement
frenchcs1
January 25th, 2008, 05:00 PM
Sorry if this has been covered before because well its a very simple conscept but I can not seem to be able to find the answer to it. I am trying to register two key presses at the same time. For instance if I want my character to move diagonally I would press UP and LEFT or something like that. However my old way of doing this was with the isDown function which they have decided to remove from AS3.0 so I was trying listeners but they only register the last key pressed down. This is my code:
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyHandler);
function keyHandler(event:KeyboardEvent):void {
if (event.keyCode == 37) {
PlayMove.x = PlayMove.x - 1;
}
if (event.keyCode == 38) {
PlayMove.y = PlayMove.y - 1;
}
if (event.keyCode == 39) {
PlayMove.x = PlayMove.x + 1;
}
if (event.keyCode == 40) {
PlayMove.y = PlayMove.y + 1;
}
}
Hopefully someone can help me out. Thanks.
frenchcs1
January 25th, 2008, 05:26 PM
Ok well I came up with a solution, not sure its the best though. If someone has a better way let me know. Thanks.
Solution:
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, keyHandlerRelease);
var UP:Boolean = false;
var DOWN:Boolean = false;
var LEFT:Boolean = false;
var RIGHT:Boolean = false;
function keyHandler(event:KeyboardEvent):void {
if (LEFT) {
PlayMove.x = PlayMove.x - 1;
}
if (UP) {
PlayMove.y = PlayMove.y - 1;
}
if (RIGHT) {
PlayMove.x = PlayMove.x + 1;
}
if (DOWN) {
PlayMove.y = PlayMove.y + 1;
}
if (event.keyCode == 37 && !LEFT) {
PlayMove.x = PlayMove.x - 1;
LEFT =true;
}
if (event.keyCode == 38 && !UP) {
PlayMove.y = PlayMove.y - 1;
UP=true;
}
if (event.keyCode == 39 && !RIGHT) {
PlayMove.x = PlayMove.x + 1;
RIGHT=true;
}
if (event.keyCode == 40 && !DOWN) {
PlayMove.y = PlayMove.y + 1;
DOWN=true;
}
}
function keyHandlerRelease(event:KeyboardEvent):void {
if (event.keyCode == 37) {
LEFT=false;
}
if (event.keyCode == 38) {
UP=false;
}
if (event.keyCode == 39) {
RIGHT=false;
}
if (event.keyCode == 40) {
DOWN=false;
}
}
ajcates
January 25th, 2008, 05:58 PM
PlayMove.x = PlayMove.x + 1;
Is the same as
PlayMove.x += 1;
I don't see anything wrong with your code, I wonder what would happen if you hold down the left and right keys tho. Also next time you post as code in the forums try and use
Your as code here
Lou
January 26th, 2008, 12:32 AM
ActionScript Code:
PlayMove.x =+ 1;
Its MovieClip._x += 1 isn't it? with + before =?
parkerdc
January 26th, 2008, 01:30 AM
I am sure there are 100 ways to do it, but here is how I do it...
This code will allow you to use 2 keys at the same time.
setInterval(function () {
if (Key.isDown(Key.RIGHT)) {
PlayMove._x += 1;
}
if (Key.isDown(Key.LEFT)) {
PlayMove._x -= 1;
}
if (Key.isDown(Key.DOWN)) {
PlayMove._y += 1;
}
if (Key.isDown(Key.UP)) {
PlayMove._y -= 1;
}
updateAfterEvent();
}, 10);
ajcates
January 26th, 2008, 01:58 AM
yes it is += sorry about that.
LittleFenris
January 26th, 2008, 12:20 PM
I am sure there are 100 ways to do it, but here is how I do it...
This code will allow you to use 2 keys at the same time.
Only issue I see with your code is that it looks to be AS2 and from what I can tell the poster is programming in AS3.
parkerdc
January 26th, 2008, 01:59 PM
Only issue I see with your code is that it looks to be AS2 and from what I can tell the poster is programming in AS3.
Thanks, if I would have read more closely would have noticed that.
I found this article that has an AS3 tutorial on keyboard code.
http://www.actionscript.org/resources/articles/698/5/Make-your-own-reusable-classes-using-Flash-and-AS3/Page5.html
Dark Viper
January 31st, 2008, 05:27 AM
Hi,
Just to let you know that a lot of diagonal code (including the code found in that tutorial) will allow you to move quicker when you are moving diagonally which is wrong.
There is a small tutorial on creating diagonal movement which includes AS3 code here:
Movement Tutorial (http://blog.fatal-exception.co.uk/?p=9)
hatu
January 31st, 2008, 07:02 AM
Hi,
Just to let you know that a lot of diagonal code (including the code found in that tutorial) will allow you to move quicker when you are moving diagonally which is wrong.
There is a small tutorial on creating diagonal movement which includes AS3 code here:
Movement Tutorial (http://blog.fatal-exception.co.uk/?p=9)
I just stumbled on that a few days ago :geek:
It is a small but important thing, like the player can tell something is wrong with the controls but can't put his finger on it.
parkerdc
January 31st, 2008, 12:30 PM
I have read the article for the AS3 keyboard class at the following website.
http://www.actionscript.org/resources/articles/698/5/Make-your-own-reusable-classes-using-Flash-and-AS3/Page5.html
I took Jody Hall's code and created a sample Flash 9, AS3 application: attached zip.
He has the keyboard and movement code in the KeyMover.as file.
frenchcs1
February 11th, 2008, 04:24 PM
Thanks everyone for the links and information. Got busy with work and havent had time to come back and post till now.
and yes the -= or += is quicker to write but I just dont use it because it is less clear to me exactly what it is doing at a quick glance, even though I do know what its doing...if that made any sense. To each their own I suppose.
Anyway Thanks
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.