View Full Version : [AS3] Character Movement
PurpleZ
02-26-2009, 12:21 AM
Whats the code to move my character in 8 directions I got left right up down already just cant get the other
bluemagica
02-26-2009, 05:26 AM
in those 4(UDLR) you just modify x or y, But for the diagonals you modify both like for top+right : x+=4,y-=4;
so depending upon your code you can just use "if else" statements to do this. I am not giving you exact code, cause this is really easy, and you should try a bit yourself to solve this!
aaron_da_killa
02-26-2009, 09:02 AM
I assume this is a top down game?
PurpleZ
02-26-2009, 09:54 PM
no
EightySeven
02-27-2009, 02:51 AM
Like all math that starts in kindergarten grids have an X and Y variable. The same principles apply in flash.
X moves left and right
Y moves up and down
If you were to combine a positive X and Y movement.... OMG you move on an angle!!!
Sorry I assume people who wish to program have knowledge of basic mathematical skills
PurpleZ
02-28-2009, 12:39 AM
Like all math that starts in kindergarten grids have an X and Y variable. The same principles apply in flash.
X moves left and right
Y moves up and down
If you were to combine a positive X and Y movement.... OMG you move on an angle!!!
Sorry I assume people who wish to program have knowledge of basic mathematical skills
Im not dumb T,T" I understand that...
My question is how do I get it to respond once I click those two keys?
function checkKeysDown(event:KeyboardEvent):void {
if (event.keyCode==37 && event.keyCode==38) {
upleftDown=true;
}
}
Thats my function I do the same thing for CheckKeysUp...
but it doesnt work
aaron_da_killa
02-28-2009, 12:57 AM
You shouldn't have to do that and to be honest, I'm not sure if that code will even work.
What everybody is trying to say is that if theoretically you press the left key event1 should happen, if you press the up key event2 should happen, if you press the left and up kep event1 and event 2 should happen.
There is no need to do this:
if (event.keyCode==37 && event.keyCode==38) {
upleftDown=true;
}
By the way, are you copying code from that platform game tutorial and modifying it for your own use (like I did when I first started :P)?
PurpleZ
02-28-2009, 01:54 AM
Yup It was the only one...
Not alot of actionscript game tutorials :(
I understand that wont work cause that basically means if left key is down AND up key is down (at the same time speaking) it will do that action, unfortantley I can't get perfect timing and I doubt anyone else will as well..
Anyway of helping whatsoever?
Should I change code completely??
bluemagica
02-28-2009, 02:12 AM
sheesh, you are hopeless....
function kdown(e:keyboardEvent):void{
if(event.keyCode==37) //i totally forgot the keyCodes, so they may be wrong
{
left=true;
right=false;
}
else if(event.keyCode==39) //see we are using else here, so that noone can press both left right together
{
left=false;
right=true;
}
if(event.keyCode==38) //again no need of else here
{
up=true;
down=false;
}
else if(event.keyCode==40)
{
up=false;
down=true;
}
}
//Next handle key releases, and set there booleans to false
function kUp(e:keyboardEvent)
{
if(e.keyCode==37)
left=false;
if(e.keyCode==38)
up=false;
if(e.keyCode==39)
right=false;
if(e.keyCode==40)
down=false;
}
function eframe(e:Event) //this is enterframe
{
if(left==true && (this.x-4)>=(this.width/2))
this.x-=4;
else if(right==true && (this.x+4)<=(stage.stageWidth-(this.width/2)))
this.x+=4;
if(up==true && (this.y-4)>=(this.height/2))
this.y-=4;
else if(down==true && (this.y+4)<=(stage.stageheight-(this.height/2)))
this.y+=4;
}
//set up the listeners and booleans yourself!
aaron_da_killa
02-28-2009, 02:35 AM
Here is mine (don't copy it, just look at it and see if you can figure out how it works- this is pretty much from that tutorial), I added a bunch of comments to help ya out:
//this function is triggered every frame, function name is moveChar
private function moveChar(event:Event):void{
//if certain keys are down then move the character
if(leftKeyDown){
player.x -= mainSpeedLeft; //decrement the player's x axis by mainSpeedLeft (which is 4) every frame
}
if(rightKeyDown){
player.x += mainSpeedRight; //increment the player's x axis by mainSpeedLeft (which is 4) every frame
}
if(upKeyDown || playerJumping){ //if statement triggers if the up key is down or if the player is jumping (a bad way of doing things I guess)
playerJump(); //triggers a function named playerJump
}
}
//this function is triggered every time a key is pressed, function name is checkKeysDown
private function checkKeysDown(event:KeyboardEvent):void{
if(event.keyCode == 37){
leftKeyDown = true; //changes a variable to true
}
if(event.keyCode == 38){
upKeyDown = true;//changes a variable to true
}
if(event.keyCode == 39){
rightKeyDown = true;//changes a variable to true
}
if(event.keyCode == 40){
downKeyDown = true;//changes a variable to true
}
}
//this function is triggered every time a key is depressed, function name is checkKeysUp
private function checkKeysUp(event:KeyboardEvent):void{
if(event.keyCode == 37){
leftKeyDown = false;//changes a variable to false
}
if(event.keyCode == 38){
upKeyDown = false;//changes a variable to false
}
if(event.keyCode == 39){
rightKeyDown = false;//changes a variable to false
allowLeftScroll = 0;
}
if(event.keyCode == 40){
downKeyDown = false;//changes a variable to false
}
}
bluemagica
02-28-2009, 02:39 AM
i think he was asking 8 directional movement......not jumping! anyway, why is jumping a separate function?
aaron_da_killa
02-28-2009, 02:50 AM
i think he was asking 8 directional movement......not jumping! anyway, why is jumping a separate function?
Ah, I suppose so :p.
Here is the code then:
//this function is triggered every frame, function name is moveChar
private function moveChar(event:Event):void{
//if certain keys are down then move the character
if(leftKeyDown){
player.x -= mainSpeedLeft; //decrement the player's x axis by mainSpeedLeft (which is 4) every frame
}
if(rightKeyDown){
player.x += mainSpeedRight; //increment the player's x axis by mainSpeedLeft (which is 4) every frame
}
if(upKeyDown){
player.y -= 4; //decrement the player's y axis by 4 every frame
}
if(downKeyDown){
player.y += 4; //increment the player's y axis by 4 every frame
}
}
//this function is triggered every time a key is pressed, function name is checkKeysDown
private function checkKeysDown(event:KeyboardEvent):void{
if(event.keyCode == 37){
leftKeyDown = true; //changes a variable to true
}
if(event.keyCode == 38){
upKeyDown = true;//changes a variable to true
}
if(event.keyCode == 39){
rightKeyDown = true;//changes a variable to true
}
if(event.keyCode == 40){
downKeyDown = true;//changes a variable to true
}
}
//this function is triggered every time a key is depressed, function name is checkKeysUp
private function checkKeysUp(event:KeyboardEvent):void{
if(event.keyCode == 37){
leftKeyDown = false;//changes a variable to false
}
if(event.keyCode == 38){
upKeyDown = false;//changes a variable to false
}
if(event.keyCode == 39){
rightKeyDown = false;//changes a variable to false
}
if(event.keyCode == 40){
downKeyDown = false;//changes a variable to false
}
}
As for jumping being a separate function, I don't know. I suppose it doesn't have to be.
bluemagica
02-28-2009, 03:05 AM
now compare your 8 directional code to mine....without the else statements it will cause glitch if you press opposite keys like left+right
EightySeven
02-28-2009, 05:20 AM
Ok so I had this big long tutorial typed up for PurpleZ on how to go about making it work but then I realized how ugly it all looked on the forums so I did up a fla for'em.
PurpleZ enjoy and if you, or anyone else, have any questions on the code feel free to PM me
Its not a work of art but its a skeleton you can build off of.
runawayprisoner
02-28-2009, 08:37 AM
Well... *facepalm* I just find it amazing how this question is asked so often...
Anyway, to register more than one key at a time, you just need to deal with it status by status. Basically...
This will surely detect as many simultaneous key presses as your keyboard would support (some support more than 2 simultaneous presses):
// Define four boolean variables for the 4 keys before hand. Initialize them all to false.
addEventListener('keyDown',
function (e:KeyboardEvent) { // Since your function will be present all the time, assigning it a name is... pointless. Much less defining it outside.
switch(e.keyCode){
case Keyboard.UP:
kUp = true; // Flip the key's status to true
break;
case Keyboard.DOWN:
kDn = true;
break;
case Keyboard.RIGHT:
kRt = true;
break;
case Keyboard.LEFT:
kLt = true;
break;
}
});
addEventListener('keyUp',
function (e:KeyboardEvent) { // Since your function will be present all the time, assigning it a name is... pointless. Much less defining it outside.
switch(e.keyCode){
case Keyboard.UP:
kUp = false; // Now flip 'er to false
break;
case Keyboard.DOWN:
kDn = false;
break;
case Keyboard.RIGHT:
kRt = false;
break;
case Keyboard.LEFT:
kLt = false;
break;
}
});
And in your main loop or something like that, do something like this:
if(kUp) characterSprite.y-=speed;
if(kDn) characterSprite.y+=speed;
if(kRt) characterSprite.x+=speed;
if(kLt) characterSprite.x-=speed;
And off you go. All done. If you need any other key, just type in Keyboard. and then scroll down to their respective key code. Make a status variable for it, too, and you're good to go.
Edit: a list of more than 4 "if" statements is quite unsightly... by the way, and pretty... urg... redundant in cases where the input is not simultaneous. In other words... when you're catching the same thing multiple times. Use a "switch... case..." structure instead. It's easier to manage and in some cases hell of a lot faster than having 16 or so "if" statements.
PurpleZ
02-28-2009, 03:38 PM
THX all for help :D greeatly appreciate it..
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.