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

Yes they are in the same clip haha. It's weird the way i have it set up. It has like 4 different layers inside one. So what do you think I should do?

beech
offline
beech
107 posts
Nomad

layers are fine

but use a 'single' onClipEvent(enterframe) to handle all conditions

more soon - building a quick sample

Midmenj
offline
Midmenj
216 posts
Nomad

Ok thanks. Btw, I have never programmed before so explanations would help. Thanks for all the help.

beech
offline
beech
107 posts
Nomad

alrighty - sorry i didn't get this yesterday - had a busy day

so i have place comments in the code for your review, these should explain the procedure - but you'll likely still have questions.

rem: this is *one* way to do it - there are many others, and this is a very basic algorithm to accomplish what your looking for

HERE is the file

(note: this is a CS5 file, in AS2 - if you have a lesser version i may need to save this is a different format for you - let me know)

beech
offline
beech
107 posts
Nomad

PS - i went with gaboloth's decrement method in this example - as it fits the procedure better, than when using other more 'fancy' methodologies

PSS - also, you're welcome

gaboloth
offline
gaboloth
1,612 posts
Peasant

does ! mean false? I'd google it but google does not accept punctuation

also, what is xscale? a sort of facing right or left?

and another thing, why did you used that rate x thing instead of modifying directly the xspeed?

beech
offline
beech
107 posts
Nomad

boolean operator) yes when using a Boolean you can 'ask' if it evaluates to false by prefacing it with a ! so if you say: if(!false) <- this evaluates to a 'true' condition because your asking for the opposite of the boolean

additionally one can use the != operator as in: if( x != 0 ) <- this says: "if x does not equal 0"

further, you can 'reverse' a boolean by using:
myBoolean = !myBoolean;
in this case regardless of the value of the boolean currently, it will 'change' the assignment to the opposite of the current value - this is very handy in situations like a 'mute' button where you wish to toggle the value

_xscale (AS2) scaleX (AS3) - yes, you can use scaling to 'flip' an instances transform property to a postive or a negative value, in addition to just simply scaling it to a percentage - so in this case i'm simply using the scale property to make it change from 100 to -100 (in as2 values, in as3 values are base on 0-1)

rateX) i simply used a property declaration there, so that it is simple to change the rate of movement by changing the property value - if speedX was a constant, then you would have to 'set' the x property directly, and this messes with the integration step - this is a 'clearer' way of adjusting the speed of the character movement

additionally, if one was to set up a more 'fluid' acceleration step you could increment the speedX value by 1 using the rateX value as a 'maximum' so that is slowly(relatively) increases it's speed until reaching the limit - much like the way that decrementing the y speed cause the impression of a gravitational force.

beech
offline
beech
107 posts
Nomad

PS - you can find a list of operators HERE in the docs

gaboloth
offline
gaboloth
1,612 posts
Peasant

rateX) i simply used a property declaration there, so that it is simple to change the rate of movement by changing the property value - if speedX was a constant, then you would have to 'set' the x property directly, and this messes with the integration step - this is a 'clearer' way of adjusting the speed of the character movement

well, to be frank I dont use the integration step, I just do
if (key.isdown(key.RIGHT))
{
_x = _x + xspeed
}
lame huh?

no seriously, I don't get that your way is better.
beech
offline
beech
107 posts
Nomad

LOL - no not lame - just straight up

but you can eliminate the additional _x by using:

_x += xspeed;

the reason why, is only that it is more 'flexible' and just easier to change - for instance there are several areas that use the speed property - if it is a constant it cannot be adjusted without making the change to several instances or conditions - with this approach, you simply change the 'rate' prop and everything still works without having to adjust a properties or conditions in the code - when you have a large project that consists of several thousand lines, to go back through and adjust everything is a pain in the...

also, in effect you are integrating the property _x on every frame, but directly - what i have in the example is designed to 1) adjust the value of the speed (in this case it's a straight up change or similar to an impulse force) and 2) then use the adjusted property to affect the position of the instance.

imagine that you have multiple physical objects, when they collide, forces are applied to each body, and there also may be several forces of different vectors, first you sum the force vectors, then apply them all to the body - with a straight up method it's likely that the system won't handle this correctly.

driejen
offline
driejen
486 posts
Nomad

I use xspeed a lot, mostly for consistency but it is very useful. Like you can make realistic free fall by having a yspeed variable, you can simulate friction using xspeed. For example, I had a helicopter that wouldn't instantly stop when I let go of left or right, but would deccelerate and continue to travel a little further even though I have let go of the arrow keys. This would also be useful for simulating icy floor in platformers where you might want the character to continue 'sliding' on the ice for some time after arrow keys are released.

gaboloth
offline
gaboloth
1,612 posts
Peasant

Hey, you're right! I realized that I used that too when I made a thing that decelerates! And I realized also that I made a total crap that time...

And about _x += xspeed, yeah, I knew about it, but I'm not used to things like that or xspeed ++ or however it's written, I prefer to just go with the usual _x = _x + xspeed o_o

Midmenj
offline
Midmenj
216 posts
Nomad

Ok so he jumps! now how to play the animation of him jumping when i jump? also he's half way in the "ground" how do i fix that? hahaha

beech
offline
beech
107 posts
Nomad

you're getting there Midmenj - i'd guess that the registration point is at center, and you're hitTest is using the reg point, rather than adding the half-height

also make sure that when you find the ground that you then 'set' the instance at the contact manifold - eg. the y point of the ground + the half-height (if reg is at center)

----
remember guys - "xspeed" is just a name, it can be anything you would like like xs, xsdp, x2, xreallylongpropertynamedblah... lol

gaboloth
offline
gaboloth
1,612 posts
Peasant

If you are using the decreasing value way, it's like
If (_root.hero.hitTest(_root.ground))
{
If (decreasingValue > 0)
{
Gotoandstop(yourJumpingFrame)
}
Else
{
Gotoandstop(yourFallingFrame)
}
}
I would do it this way, I dunno if beech is going to pwn me ;D

Showing 16-30 of 52