PDA

View Full Version : object moving problem...



blah-de-blah
April 8th, 2003, 07:43 AM
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

lairusi
April 8th, 2003, 08:40 AM
I think there is an example provided when you installed flash mx in your computer similar to what you are trying to do.

kode
April 8th, 2003, 09:14 AM
if you want something really easy... :P

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++;
}
;)

blah-de-blah
April 8th, 2003, 10:17 AM
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 :)

blah-de-blah
April 8th, 2003, 10:21 AM
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

kode
April 8th, 2003, 10:42 AM
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 :P

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 :-\

blah-de-blah
April 8th, 2003, 10:45 AM
ah yea i see now thx :)

kode
April 8th, 2003, 10:46 AM
no problem =)

blah-de-blah
April 8th, 2003, 10:54 AM
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!?

blah-de-blah
April 8th, 2003, 11:05 AM
ah nvm i got it to work...i did:

this._x -= 5

thx though:)

kode
April 8th, 2003, 11:07 AM
oh!! i thought you wanted something like this... :P

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);

blah-de-blah
April 8th, 2003, 11:09 AM
heh...looks good, but its too advanced for me at the moment :) it'd take too much explainin for me to get that, just gonna stick with the stuff i understand first :)

kode
April 8th, 2003, 11:11 AM
ok then. good luck ;) =)