The Armor Games website will be down for maintenance on Monday 10/7/2024
starting at 10:00 AM Pacific time. We apologize for the inconvenience.
The Armor Games website will be down for maintenance on Monday 10/7/2024
starting at 10:00 AM Pacific time. We apologize for the inconvenience.
52 | 15718 |
Hello, I have a working code with working sprites. But I'm trying to figure out how to make him stop moving when i don't hold down the key.
Here is the code:
onClipEvent (load) {
walk = 4;
}
onClipEvent (enterFrame) {
if (Key.isDown(Key.RIGHT)) {
gotoAndStop(1);
_x += walk;
} else if (Key.isDown(Key.LEFT)) {
gotoAndStop(2);
_x -= walk;
} else {
gotoAndStop(1);
}
}
Just to recap I want the character to stop moving in the direction he was going.
Thank you
} else if (Key.isDown(Key.LEFT)) {
gotoAndStop(2);
_x -= walk;
Can I just say that your structure is bad for holding left and right at the same time? Yep. Heres what I would try first;
onClipEvent (load) {
walk = 4;
dir = 0;
currentFrame = 1;
}
onClipEvent (enterFrame) {
dir = Key.isDown(Key.RIGHT) - Key.isDown(Key.LEFT); //will be 1,0 or -1
if(dir==1){ //if trying to move right
gotoAndStop(2); //go to frame containing spriteRight
spriteRight.play(); //make the sprite start/continue animation
}else if(dir==-1){ //if trying to move left
gotoAndStop(1); //go to frame containing spriteLeft
spriteLeft.play(); //make the sprite start/continue animation
}else{ //if not trying to move
if(currentFrame==1){ //if seeing spriteLeft
spriteLeft.stop(); //stop spriteLeft animation
}else if(currentFrame==2){ //if seeing sprightRight
spriteRight.stop(); //stopspriteRight animation
}
}
Replace spriteLeft and spriteRight with the instance names of your sprites. Tell me if that works or not.
oh wait, what the heck I'm saying? that's as2 too. when I'm tired I come up with dumb conclusions.
The code I gave isn't complete, it should be easy to complete yourself if you're not a copy paster but here is a more complete version anyway;
onClipEvent(load){
walk = 4;
dir = 0;
currentFrame = 1;
}
onClipEvent(enterFrame){
dir = Key.isDown(Key.RIGHT) - Key.isDown(Key.LEFT); //will be 1,0 or -1
if(dir==1){ //if trying to move right
gotoAndStop(2); //go to frame containing spriteRight
spriteRight.play(); //make the sprite start/continue animation
currentFrame = 2;
_x += walk;
}else if(dir==-1){ //if trying to move left
gotoAndStop(1); //go to frame containing spriteLeft
spriteLeft.play(); //make the sprite start/continue animation
currentFrame = 1;
_x -= walk'
}else{ //if not trying to move
if(currentFrame==1){ //if seeing spriteLeft
spriteLeft.stop(); //stop spriteLeft animation
}else if(currentFrame==2){ //if seeing sprightRight
spriteRight.stop(); //stopspriteRight animation
}
}
}
thank you driejen, your code worked for me. I had to do a little tweaking thought. Btw you forgot the ; after walk for the _x-=walk;
i would strongly encourage *not* to use 'object attachment' in your code and place code on the timeline or within class files.
i understand that this is working, but another method would be to break the if/else statement and use a series of conditions:
if( Key.isDown(Key.RIGHT) ) {}
if( Key.isDown(Key.LEFT) ) {}
if( !Key.isDown(Key.RIGHT) && !Key.isDown(Key.LEFT) ) {}
yea, that was because it was as3 at first, cuz i always use external files and when I saw onclip event my poor tired brain associated it to as3's events.
I only gave code for an object because thats what he intends to do judging from the code he gave first, but yeah it can get very messy that way when you are dealing with lots of objects so it's better not to do that.
Also I use else often when the following code isn't necessary when the first condition is met, I can see how it might get confusing but I'm perfectly fine with it and I don't get confused that way but that's just me.
The thing with timeline coding is it gets really really long, as3 is much better because you can separate code into classes dunno if you can do that with as2.
one can use classes in AS2, but it's a bit more complicated actually, since the system was particularly intended for that type of work at the time (although was making that move) today, the system is based on a Class/Event model, which is much more powerful.
nothing wrong with the code above driejen - if/else is completely valid. although i often find now days that one runs into problems in 'capturing' all of the possible contingencies in this manner, because when you say: if(this) - else if(that) -else(other) you may have 'missed' the if(this & that) and it'll fall through to 'other' when that wasn't the intent - and part of the issue the OP has in this case
one other quick note: the use of booleans in that manner (as integers) works in 2, but will not in 3 as it is a stricter syntax - unless you use data type conversions: 'coercion' (implicit) or 'cast' (explicit) - as in:
int(true) == 1;
int(false) == 0;
I have another code I need help with haha It's a jump code and it's not working haha. I want it to have gravity and stuff as well. Here is the code
onClipEvent(enterFrame){
if(!_root.ground.hitTest(this._x, this._y, true) &&!jumping){
_y+=10
}
if(_root.ground.hitTest(this._x, this._y, true) && falling){
falling=false;
jumping=false;
jump=12
}
if (key.isDown(key.UP)){
jumping=true;
}
if (jump<0)
falling=true;
}
if(jumping){
this._y-=jump;
jump-=.9;
}
I also want to be able to use a sprite for the jumping if someone can help me out with that.
is this code on the same clip as the previous? if so, you should not run 'two' enterframe events - just wondering about this first
I think you should set a jumpspeed var, subtract it from the y value, and decrease it a little in enterframe while he is in the air. this way the value will become negative and he will begin falling without a falling state. if you want to use a different animation for the faling youst play it if jumpspeed is negative.
right g - just like in 'real life' a gravity factor is good to give realism to the action - one way is to decrement the value on every frame by a factor, as in:
speedY *= 0.98;
You must be logged in to post a reply!