Object moving problem

hello,
well i’m trying to get this object to move on the press of a key (keeps moving if you hold it down) and stop when you release it, but all i’m gettin, is when you press it, it moves on forever and never stops…i’ve tried reading this tutorial:

http://www.kirupa.com/developer/actionscript/xymove.htm

but the code is very confusing, not the actual code itself but the letters and stuff so i dont’ really understand it. Would anyone know another place with a similar tutorial? thx a lot

I think there is an example provided when you installed flash mx in your computer similar to what you are trying to do.

if you want something really easy… :stuck_out_tongue:

myMovieClip.onKeyDown = function() {
	if (Key.isDown(37)) this._x--; // left
	if (Key.isDown(38)) this._y--; // up
	if (Key.isDown(39)) this._x++; // right
	if (Key.isDown(40)) this._y++; // down
}
Key.addListener(myMovieClip);

or

myMovieClip.onEnterFrame = function() {
	if (Key.isDown(37)) this._x--;
	if (Key.isDown(38)) this._y--;
	if (Key.isDown(39)) this._x++;
	if (Key.isDown(40)) this._y++;
}

:wink:

lairusi - i couldn’t manage to find the example you were talking about? is it in the tutorials section of the flashmx help?

and kax thx for the codes, i dont’ really get hte first one but i think i may get the second. the numbers in brackets are like the numbers for each key right? How did you manage to find them out?

and x-- means to -1 from the previous coordinate right? corect me if i’m wrong…thx for the code again though :slight_smile:

ah and i just checked the tutorials in flash about the decrement, and i realized there was a pre and post. I understand that the post decrement returns the original value, but how would you know when to use a pre decrement and a post decrement? Wouldn’t it be better if the code you gave me used a pre decrement so it would -1 from the new number each time?

Heh it’d be great if you could explain that to me, if you understand it, but thx anwyaays

the numbers in brackets are like the numbers for each key right? How did you manage to find them out?

k = new Object();
k.onKeyDown = function() {
	trace(Key.getCode());
}
Key.addListener(k);

just kidding :stuck_out_tongue:

you can find them in the flash help files.

ah and i just checked the tutorials in flash about the decrement, and i realized there was a pre and post. I understand that the post decrement returns the original value, but how would you know when to use a pre decrement and a post decrement? Wouldn’t it be better if the code you gave me used a pre decrement so it would -1 from the new number each time?

honestly… i see no difference in this case. the _x/_y is increased/decreased when you press the key :-\

ah yea i see now thx :slight_smile:

no problem =)

heh sorry, another question jsut came to me…is it possible to vary the increment/decrement? because i want to speed up the object…is this possible!?

ah nvm i got it to work…i did:

this._x -= 5

thx though:)

oh!! i thought you wanted something like this… :stuck_out_tongue:

MovieClip.prototype.keyMovement = function(smax) {
	this.onEnterFrame = function() {
		if (Key.isDown(37) && this.xs>-smax) this.xs--;
		if (Key.isDown(39) && this.xs<smax) this.xs++;
		if (Key.isDown(38) && this.ys>-smax) this.ys--;
		if (Key.isDown(40) && this.ys<smax) this.ys++;
		this.xs *= .94;
		this.ys *= .94;
		this._x += this.xs;
		this._y += this.ys;
	}
}
myMovieClip.keyMovement(10);

heh…looks good, but its too advanced for me at the moment :slight_smile: it’d take too much explainin for me to get that, just gonna stick with the stuff i understand first :slight_smile:

ok then. good luck :wink: =)