ForumsProgramming ForumSprite with Movement code

52 15753
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
gaboloth
offline
gaboloth
1,612 posts
Peasant

Aaaah I got ninjad! And yes, I forgot to say about the registration point

beech
offline
beech
107 posts
Nomad

LMAO! stealthMode(true);

bad gab, bad! (jk) - you should steer away from using _root, there are lots of reasons, but the main one is that if you load the file into another, like a preloader or a third party service API like Mochi, then _root will point at the 'stage' of the loader, and your code will mostly likely 'break'

instead use: _parent when necessary, or _parent._parent._parent - if you have that many levels to reach back

it isn't 'wrong' just a best practice tip :P

Midmenj
offline
Midmenj
216 posts
Nomad

can you explain in detail how I find the half height hahaha

beech
offline
beech
107 posts
Nomad

you can use:

hero._height / 2

or just use a 'hardcoded' value - as in the number of pixels needed

Midmenj
offline
Midmenj
216 posts
Nomad

and where do i put that in the code?

beech
offline
beech
107 posts
Nomad

well M - it really depends on the structure you are using - you can see in the example that i have added 15px to the y values as needed - so you'll need it in your hitTest, and in assignments to 'where' to set the player

so, you can use a 'number' or you can use a property - for instance:

var offsetY:Number = 15;
OR
var offsetY:Number = hero._height / 2;

AND/OR
if( ground.hitTest( hero._x, hero._y+offsetY, true ) ) {}
if( ground.hitTest( hero._x, hero._y+15, true ) ) {}

see how it works

gaboloth
offline
gaboloth
1,612 posts
Peasant

Uh? Does _parent just get the nearest level? Cuz in that case is amazing, I wish I knew it before. _root looked so cool, you keep destroying my coding idols

PS how does that ground.hitTest works? Usually when I try to do that it says me "the property is missing the static attribute" or something like that. Did you set a static property in some way?

plasmafish
offline
plasmafish
252 posts
Nomad

Is this basically what you're trying to do?

Simple platform game

On this file the character is behind the ground, an error on my part.

beech
offline
beech
107 posts
Nomad

@plasmafish - yes it seems the OP is building a basic platformer, but the questions in this thread are related to refining the systems needed to do so and learning about them and i have previously posted an example fla and it's code - you're example could clearly benefit from some of the discussion here, and does not assist the OP because you have not provided any code, thoughts or theory, only a compiled swf file.


@gab - sorry bud LOL - yeah the use of root can really cause issue, but it's commonplace when first getting going and is found all over in tutorials even - so no worries

_parent) sort of, when you create a MC, and then place another MC 'inside' the first, it is known as a 'child' of the 'arent' clip - each time you do so, it becomes a child of the clip you place it into. so you can do this with many levels, for instance:

lets build a rocket, on the rocket's timeline it has nose, body, wings, engine - all clips - the engine's timeline has a cone, and flames. now to call from the main timeline to get to the flames to 'turn them on' we'd use the path: rocket.engine.flames - you can see by the syntax that the flames are 3 'levels' deep - the flames are a 'child' of the 'engine' the engine is a child of rocket, and rocket is ultimately a child of the main timeline or stage. so to transverse the display stack in the 'other' direction, from the flames instance to the stage we have to iterate through as many levels as needed to get there, store a ref to the stage or use a short-cut like root (but still bad to do so) so if we have a call that in the flames instance that needs to get to the stage, one way to do it is to use:

_parent(engine)._parent(rocket)._parent(stage).method();

(without the parenthesis of course)

there are other more efficient ways of doing this though, especially within a Class structured framework - but this is the core principle of parenting hierarchy.

beech
offline
beech
107 posts
Nomad

gab - about the hitTest issue: not certain unless i was view specific code - statics are a hot topic of Class structures and Deisgn Patterns these days, as opposed to using the getter/setter mindset (i prefer statics atm)

it's possible that you'd missed an instance name and we're running into a 1120 null object reference, or were calling the method outside the scope of an instance (like from 'inside' another instance, that does not have a reference to the 'name' of the other) - or perhaps you had missed one of the parameters when using a 'shapeflag' based hitTest, which requires three arguments

in as2 outside a class file, on a timeline the property declarations are 'local' to the scope in which they are created, as is the case for any property declared within another Object (including methods, or 'functions'

Midmenj
offline
Midmenj
216 posts
Nomad

hahaha ok i got him to be ontop of the platform but I still need the sprites to work D: I almost got it too hahaha thank you for all the help!

gaboloth
offline
gaboloth
1,612 posts
Peasant

I'll try to explain that better. When I try to refer to a thing in a class I have to make _parent or _root or whatever, but I saw that you just wrote ground, so I thought you were referring to a class, but in as2 I can never do it, it says me the static thing. So, first, was you referring to the class or the instance?

plasmafish
offline
plasmafish
252 posts
Nomad

Beech, I didn't want to give an FLA because I don't know what hes aiming for. I'm just assuming that at this speed, it will take him years to make a half working game.

beech
offline
beech
107 posts
Nomad

... so you were just trying to show off?

beech
offline
beech
107 posts
Nomad

gab - in this case it was targeting the instance, since it was local in our scope of the example from the timeline.

Showing 31-45 of 52