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.

ForumsProgramming ForumSprite with Movement code

52 15718
Midmenj
offline
Midmenj
216 posts
Nomad

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

  • 52 Replies
Midmenj
offline
Midmenj
216 posts
Nomad

Also, both sprites are 12 frames

gaboloth
offline
gaboloth
1,612 posts
Peasant

} else if (Key.isDown(Key.LEFT)) {
gotoAndStop(2);
_x -= walk;

That else is bad. This way going right is stronger than going left, for example if you are holding left and press right it will change direction, but if you're holding right and you press left it won't react.
For the stopping problem, I'm not sure. What's the problem? When you press a key it sticks to the command?
driejen
offline
driejen
486 posts
Nomad

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.

gaboloth
offline
gaboloth
1,612 posts
Peasant

oh wait, what the heck I'm saying? that's as2 too. when I'm tired I come up with dumb conclusions.

driejen
offline
driejen
486 posts
Nomad

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
}
}
}

Midmenj
offline
Midmenj
216 posts
Nomad

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;

beech
offline
beech
107 posts
Nomad

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) ) {}

gaboloth
offline
gaboloth
1,612 posts
Peasant

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.

driejen
offline
driejen
486 posts
Nomad

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.

beech
offline
beech
107 posts
Nomad

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;

beech
offline
beech
107 posts
Nomad

LOL @gaboloth

Midmenj
offline
Midmenj
216 posts
Nomad

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.

beech
offline
beech
107 posts
Nomad

is this code on the same clip as the previous? if so, you should not run 'two' enterframe events - just wondering about this first

gaboloth
offline
gaboloth
1,612 posts
Peasant

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.

beech
offline
beech
107 posts
Nomad

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;

Showing 1-15 of 52