View Full Version : Key down question
Thrash
06-04-2007, 04:41 AM
ok im making a realy simple rpg (its a face down one, i already have the map working) and i want the character to change looks when hes traveling in certain directions.
this is the code i have currently:
onEnterFrame=function(){
if(Key.isDown(Key.UP)){
game.map._y+=speed
game.player.gotoAndStop(2)
}
if(Key.isDown(Key.DOWN)){
game.map._y-=speed
game.player.gotoAndStop(6)
}
if(Key.isDown(Key.LEFT)){
game.map._x+=speed
game.player.gotoAndStop(8)
}
if(Key.isDown(Key.RIGHT)){
game.map._x-=speed
game.player.gotoAndStop(4)
}
if(Key.isDown(Key.UP) && Key.isDown(Key.RIGHT)){
game.player.gotoAndStop(3)
}
if(Key.isDown(Key.UP) && Key.isDown(Key.LEFT)){
game.player.gotoAndStop(9)
}
if(Key.isDown(Key.DOWN) && Key.isDown(Key.RIGHT)){
game.player.gotoAndStop(5)
}
if(Key.isDown(Key.DOWN) && Key.isDown(Key.LEFT)){
game.player.gotoAndStop(7)
}
if(Key.isNotDown(Key.DOWN) && Key.isDown(Key.LEFT)){
game.player.gotoAndStop(7)
}
}
i have the player as a movie clip with like 8 or something diffrent frames. the first is when hes still, the second is when hes traveling up & right, the 3rd is when hes traveling right, the 4th is when hes traveling down and right and so on.
the only problem is that above code doesent work. it moves the map as required, but doesent change the player at all.
i thought that maybe it should be something like "if(Key.isNotDown(Key.DOWN)) && (Key.isDown(Key.LEFT)){" or "if(Key.isNotDown(Key.DOWN)) and (Key.isDown(Key.LEFT)){" but that musent be right otherwise the ones where just the left, right, up and down would work.
also i need a bit added so when no arrow keys are pressed it goes back to frame 1 (standing still image)
---
it would also be great if you explained it instead of just giving me code
Thanks :)
ry79418
06-04-2007, 05:38 AM
use rotation
onEnterFrame=function(){
if(Key.isDown(Key.UP)){
game.map._y+=speed
game.player.rotation=0
}
if(Key.isDown(Key.DOWN)){
game.map._y-=speed
game.player.rotation=180
}
if(Key.isDown(Key.LEFT)){
game.map._x+=speed
game.player.rotation=270
}
if(Key.isDown(Key.RIGHT)){
game.map._x-=speed
game.player.rotation=90
}
if(Key.isDown(Key.UP) && Key.isDown(Key.RIGHT)){
game.player.rotation=45
}
if(Key.isDown(Key.UP) && Key.isDown(Key.LEFT)){
game.player.rotation=315
}
if(Key.isDown(Key.DOWN) && Key.isDown(Key.RIGHT)){
game.player.rotation=135
}
if(Key.isDown(Key.DOWN) && Key.isDown(Key.LEFT)){
game.player.rotation=225
}
}
that code should work
Insane Knux
06-04-2007, 12:23 PM
Does NO ONE use else if anymore?
onEnterFrame=function(){
if(Key.isDown(Key.UP)){
game.map._y+=speed
game.player.rotation=0
}
else if(Key.isDown(Key.DOWN)){
game.map._y-=speed
game.player.rotation=180
}
else if(Key.isDown(Key.LEFT)){
game.map._x+=speed
game.player.rotation=270
}
else if(Key.isDown(Key.RIGHT)){
game.map._x-=speed
game.player.rotation=90
}
else if(Key.isDown(Key.UP) && Key.isDown(Key.RIGHT)){
game.player.rotation=45
}
else if(Key.isDown(Key.UP) && Key.isDown(Key.LEFT)){
game.player.rotation=315
}
else if(Key.isDown(Key.DOWN) && Key.isDown(Key.RIGHT)){
game.player.rotation=135
}
else if(Key.isDown(Key.DOWN) && Key.isDown(Key.LEFT)){
game.player.rotation=225
}
}
ry79418
06-04-2007, 01:35 PM
lol idk
does NO ONE know how to use underscores when needed???
onEnterFrame=function(){
if(Key.isDown(Key.UP)){
game.map._y+=speed
game.player._rotation=0
}
else if(Key.isDown(Key.DOWN)){
game.map._y-=speed
game.player._rotation=180
}
else if(Key.isDown(Key.LEFT)){
game.map._x+=speed
game.player._rotation=270
}
else if(Key.isDown(Key.RIGHT)){
game.map._x-=speed
game.player._rotation=90
}
else if(Key.isDown(Key.UP) && Key.isDown(Key.RIGHT)){
game.player._rotation=45
}
else if(Key.isDown(Key.UP) && Key.isDown(Key.LEFT)){
game.player._rotation=315
}
else if(Key.isDown(Key.DOWN) && Key.isDown(Key.RIGHT)){
game.player._rotation=135
}
else if(Key.isDown(Key.DOWN) && Key.isDown(Key.LEFT)){
game.player._rotation=225
}
}
Insane Knux
06-04-2007, 02:56 PM
does NO ONE know how to use underscores when needed???
xD
I just added else if.
Those many if's mess things up.
xD
I just added else if.
Those many if's mess things up.
Well they work fine for 4 direction but when you have 8 directiosn then you need else if statements.
*waits for some experts to rant on about not needing any if statements or else ifs*
ry79418
06-04-2007, 06:53 PM
ya truimagz would probably have a different way
Thrash
06-05-2007, 12:23 AM
thanks for the help guys!
Soon to be flash master
06-05-2007, 03:34 AM
The best way to get help here is to start a fight over the correct method, everyone will then chip in there 2 cents. XD
Thrash
06-05-2007, 03:55 AM
lol thanks for the hint
EDIT:
ok now i have another problem lol.
its a hittest problem. i want the player to stop when he hits a wall.
here is the code i have:
onEnterFrame=function(){
if(Key.isDown(Key.UP)){
if(game.map.limits.hitTest(game.player)) {
game.map._y-=0
}else{ game.map._y+=speed }
}
if(Key.isDown(Key.DOWN)){
if(game.map.limits.hitTest(game.player)) {
game.map._y+=0
}else{ game.map._y-=speed }
}
if(Key.isDown(Key.LEFT)){
if(game.map.limits.hitTest(game.player)) {
game.map._x-=0
}else{ game.map._x+=speed }
}
if(Key.isDown(Key.RIGHT)){
if(game.map.limits.hitTest(game.player)) {
game.map._x+=0
}else{ game.map._x-=speed }
}
}
the only problem is normally the player travels at 5pixels per frame (that sounds so weird lol) so by the time the script finds him touching the wall and stops him, hes already embedded himself 5 pixels into the wall and is unable to get out (i think because it still thinks hes touching cos he is and therefore it makes him travel at 0 pixels per frame.)
I tried to make it so if he hit he was pushed backwards the same speed (so when he touches and embeds himself 5 pixels in, he gets pushed in the opposite direction the same speed (5 pixels))
the only problem is that its extreamyly easy to get through the wall.
Also i was wondering how you could make it work with multiple walls (it only work with one atm). I tryed to use part of a code i got when i asked about a mouse avoider wall problem, but it didnt work.
you can use the code above, but if you need the fla file, find it here:
http://www.freewebs.com/yousmelllikecabbage/untitledRpg%5Fv1.fla
arkhan
06-05-2007, 07:38 AM
have 2 vars hold the last valid values of the player position, like lastx lasty, so you move, and if you hit a wall, you put him back at where he was on the frame before, where you are sure he didnt hit anything...and if he doesnt hit the walls, you save the x and y values in the lastx lasty as the last valid values.
truimagz
06-05-2007, 11:34 AM
ok, here is how you do it bro.
You need to check if your player is going to hit the walls before he moves, and if he will dont allow him to move. This will fix your problem easily.
CODE:
if (Key.isDown(Key.RIGHT)){
if(!game.map.limits.hitTest(game.player._x + speed)) {
game.player._x += speed
}
}
Seriously though I would put this hit Detection in your collision class, when you create your wall you should add it to the collision array, then you can pass this array to your player control class in your for loop when looking for collision.
Thrash
06-06-2007, 04:23 AM
Ok i tryed that code but it doesent seem to work. this is what i have, I cant seem to find anything wrong with it:
onEnterFrame=function(){
if(Key.isDown(Key.UP)){
if(!game.map.limits.hitTest(game.player_y + speed)) {
game.map._y += speed
}
}
if(Key.isDown(Key.RIGHT)){
if(!game.map.limits.hitTest(game.player_y + speed)) {
game.map._y -= speed
}
}
if(Key.isDown(Key.RIGHT)){
if(!game.map.limits.hitTest(game.player_x + speed)) {
game.map._x += speed
}
}
if(Key.isDown(Key.RIGHT)){
if(!game.map.limits.hitTest(game.player_x + speed)) {
game.map._x -= speed
}
}
}
all it does is when i start it, pressing the up key goes up, the right key makes you go down and the left and down keys do nothing.
I changed the bit here:
if(Key.isDown(Key.RIGHT)){
if(!game.map.limits.hitTest(game.player_x + speed)) {
game.map._x -= speed
}
}
because its ment to move the map under the player, not the player over the map. Would that matter???
---
Seriously though I would put this hit Detection in your collision class, when you create your wall you should add it to the collision array, then you can pass this array to your player control class in your for loop when looking for collision.
this probly sounds stupid, but i know nothing bout classes arrays. Is this the reason why i cant gte the script to work.
Ill try and find something to read about arrays now.
Your using +speed everytime even when you should use -speed.
onEnterFrame=function(){
if(Key.isDown(Key.UP)){
if(!game.map.limits.hitTest(game.player_y + speed)) {
game.map._y += speed
}
}
if(Key.isDown(Key.RIGHT)){
if(!game.map.limits.hitTest(game.player_y - speed)) {
//Check to see if the walls are hitting the spot to the ABOVE not bellow so you need -speed not +speed
game.map._y -= speed
}
}
if(Key.isDown(Key.RIGHT)){
if(!game.map.limits.hitTest(game.player_x + speed)) {
game.map._x += speed
}
}
if(Key.isDown(Key.RIGHT)){
if(!game.map.limits.hitTest(game.player_x - speed)) {
//Check to see if the walls are hitting the spot to the LEFT not right so you need -speed not +speed
game.map._x -= speed
}
}
}
Easy way to remember is when moving right check if something is to the right, when moving left check to see if something is to the left ect.
Thrash
06-06-2007, 05:37 AM
it still doesent work.
heres a link to the .fla file, is there anything wrong in it???
dont mind all the black bits round the edge, tahts so if someone maximises it somehow they cant see extra map and cheat.
Link:
http://www.freewebs.com/yousmelllikecabbage/untitledRpg%5Fv2.fla
i gtg now watch prison break ill be back tommorow try sort it out now
ry79418
06-06-2007, 12:40 PM
omg learn to edit CODE!!!!!!!!!!!!!!!
change the + to - if your having the opposite direction
truimagz
06-07-2007, 02:20 AM
WRONG
jesus fricking christ,
At leasyt copy paste my code
your problme is not the - or + is should be + , HEAR ME NOW IT SHOULD BE PLUS
Your problem is that your doing game.player_x when it should be game.player._x............your forgetting the .
I swear to got if we dont get some more educated members im out of here, I love you JJcoria, but just like when playing games, if you play on easy mode alll the time your skills start to deteriate.
I feel like all I do is fix noobie problems and haven't learned anything here yet.
We need educated people around these parts posting interestng ideas and concepts
Thrash
06-07-2007, 03:47 AM
sorry if im pissing you off. I dont want to do that or anything. sorry
i added the dots and it still doesent work
*pictures you smashing your head into the wall in frustration*
BUT DONT WORRY!
I just relised somehow that the problem was that all the things were if(Key.isDown(Key.RIGHT)){ except for the first one.
lol im such an idiot. sorry about bothering you.
thanks for all your help
TRUIMAGZ FTW!!!
EDIT:
just thought i let you all know, i got it working!!!
here is what i finished with:
onEnterFrame=function(){
if(Key.isDown(Key.UP)){
if(! game.map.limit.hitTest(game.player._x, (game.player._y + speed), true)) {
game.map._y += speed
}
}
if(Key.isDown(Key.DOWN)){
if(! game.map.limit.hitTest(game.player._y, (game.player._y + speed), true)) {
game.map._y -= speed
}
}
if(Key.isDown(Key.LEFT)){
if(! game.map.limit.hitTest((game.player._x + speed), game.player._y, true)) {
game.map._x += speed
}
}
if(Key.isDown(Key.RIGHT)){
if(! game.map.limit.hitTest((game.player._x + speed), game.player._y, true)) {
game.map._x -= speed
}
}
}
Thanks for all your help guys!!!
IM SO HAPPY! :)
lol
vBulletin® v3.7.3, Copyright ©2000-2010, Jelsoft Enterprises Ltd.