|
#1
|
|||||||||
|
|||||||||
|
I want to make a movieclip called "runningleft" play when the the LEFT key is held down; vice versa. I have a code that does that well, except if you change directions without releasing both buttons he does a moon walk. It's something really obvious that I'm missing.
Help? ![]() So far: Code:
onClipEvent (enterFrame) {
if (Key.isDown(Key.LEFT)) {
if(idle == true){
gotoAndPlay("runningleft");
idle = false;
}
} else {
if (Key.isDown(Key.RIGHT)) {
if(idle == true){
idle = false;
gotoAndPlay("runningright");
}
} else {
idle = true; gotoAndPlay("standing");
}
}
}
__________________
Last edited by Bigmutha; 07-02-2008 at 03:09 AM. Reason: typo |
|
#2
|
|||||||||
|
|||||||||
|
This Is For The Game We're Making! so it is important
![]() just providing some motivation! (and a cookie to bribe (: we also need a cookie smily)
__________________
![]() (thanks nate) |
|
#3
|
|||||||||
|
|||||||||
|
Original
Code:
onClipEvent (enterFrame) {
if (Key.isDown(Key.LEFT)) {
if(idle == true){
gotoAndPlay("runningleft");
idle = false;
}
} else {
if (Key.isDown(Key.RIGHT)) {
if(idle == true){
idle = false;
gotoAndPlay("runningright");
}
} else {
idle = true; gotoAndPlay("standing");
}
}
}
Code:
onClipEvent (load) {
idle = true;
}
onClipEvent (enterFrame) {
Key.isDown(37) && !Key.isDown(39) ? (gotoAndPlay("runningleft"), idle=false) : Key.isDown(39) && !Key.isDown(37) ? (gotoAndPlay("runningright"), idle=false) : (idle=true, gotoAndPlay("standing"));
}
__________________
Last edited by noobjam; 07-02-2008 at 06:40 AM. |
|
#4
|
|||||||||
|
|||||||||
|
Quote:
You have a very unique way of scripting also. ![]() I've never used the "?" and ":" functions. Could you explain what they do?
__________________
Last edited by Bigmutha; 07-02-2008 at 06:43 AM. |
|
#5
|
|||||||||
|
|||||||||
|
Your mistake was you were letting people press both at the same time. I added a check that would only let them move right if the left key wasn't down and vice versa. If they were both down or both not, it would be idle.
Code:
condition ? statement : statement; It's just an if statement that needs an else. Very short way of doing; Code:
If(condition){
statement
} else {
statement
};
__________________
|
|
#6
|
|||||||||
|
|||||||||
|
Hmmm, noobjam? When I try it on my game, it doesn't play the running animation, it starts to play, but the next frame it starts all over. So he just shakes. It worked on yours because it wasn't a full animation, just a pic.
I'll check this in the morning noobjam, I have to go to bed, and my mom remembered to shut down her laptop tonight. ![]()
__________________
Last edited by Bigmutha; 07-02-2008 at 07:00 AM. |
|
#7
|
|||||||||
|
|||||||||
|
Code:
onClipEvent (enterFrame) {
idle == true ? Key.isDown(37) && !Key.isDown(39) ? (gotoAndPlay("runningleft"), idle=false) : Key.isDown(39) && !Key.isDown(37) ? (gotoAndPlay("runningright"), idle=false) : null : null;
(!Key.isDown(37) && !Key.isDown(39)) || (Key.isDown(37) && Key.isDown(39)) ? (idle=true, gotoAndPlay("standing")) : null;
}
__________________
|
|
#8
|
|||||||||
|
|||||||||
|
Looks like the problem is that if you never release an arrow button idle never becomes true so it won't get as far as the gotoAndPlay commands.
Here's my solution, it works similar to noobjams but is probably a bit easier to follow: Code:
onClipEvent (load) {
idle = true;
}
onClipEvent (enterFrame) {
if ((Key.isDown(Key.LEFT) && Key.isDown(Key.RIGHT)) || (!Key.isDown(Key.LEFT) && !Key.isDown(Key.RIGHT))) {
// If both keys are up or down he is standing
idle = true; gotoAndPlay("standing");
} else if (Key.isDown(Key.LEFT) && idle == true) {
// Otherwise if the left key is down run left
gotoAndPlay("runningleft");
idle = false;
} else if (Key.isDown(Key.RIGHT) && idle == true) {
// Otherwise if the right key is down run right
gotoAndPlay("runningright");
idle = false;
}
}
![]()
__________________
|
|
#9
|
|||||||||
|
|||||||||
|
Haha, Gaz comes along and shows the nice way of coding. I just crackdown and make it like 4 lines long.
I just picked up a few tricks along the way (You never really see it used or know it's used. but using binary and bitwise operators is a neat little trick) Of course, I would never put script ON a MovieClip anymore. Timelines only.
__________________
|
|
#10
|
|||||||||
|
|||||||||
|
I want to break the habit but it is convenient. I like everything neatly filed away in an on(load) and on(enterFrame) + use it all over the place. Damn you AS3!!!
__________________
|
| Sponsored Links |
|
|
![]() |
| Tags |
| as2, flash, walking |
| Thread Tools | |
| Display Modes | |
|
|