# Object moving problem

**URL:** <https://forum.kirupa.com/t/object-moving-problem/17698>\
**Category:** Uncategorized\
**Created:** [April 8, 2003, 11:43am UTC](https://forum.kirupa.com/t/object-moving-problem/17698 "2003-04-08T11:43:19Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![blah\_de\_blah](https://avatars.discourse-cdn.com/v4/letter/b/a8b319/32.png) [@blah\_de\_blah](https://forum.kirupa.com/u/blah_de_blah)\
**Post date:** [April 8, 2003, 11:43am UTC](https://forum.kirupa.com/t/object-moving-problem/17698/1 "2003-04-08T11:43:19Z")

</div>

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](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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [April 8, 2003, 12:40pm UTC](https://forum.kirupa.com/t/object-moving-problem/17698/2 "2003-04-08T12:40:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [April 8, 2003, 1:14pm UTC](https://forum.kirupa.com/t/object-moving-problem/17698/3 "2003-04-08T13:14:55Z")

</div>

if you want something really easy… 😛

```auto
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

```auto
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++;
}

```

😉

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [April 8, 2003, 2:17pm UTC](https://forum.kirupa.com/t/object-moving-problem/17698/4 "2003-04-08T14:17:10Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [April 8, 2003, 2:21pm UTC](https://forum.kirupa.com/t/object-moving-problem/17698/5 "2003-04-08T14:21:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [April 8, 2003, 2:42pm UTC](https://forum.kirupa.com/t/object-moving-problem/17698/6 "2003-04-08T14:42:49Z")

</div>

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

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

```

just kidding 😛

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [April 8, 2003, 2:45pm UTC](https://forum.kirupa.com/t/object-moving-problem/17698/7 "2003-04-08T14:45:46Z")

</div>

ah yea i see now thx 🙂

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [April 8, 2003, 2:46pm UTC](https://forum.kirupa.com/t/object-moving-problem/17698/8 "2003-04-08T14:46:26Z")

</div>

no problem =)

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [April 8, 2003, 2:54pm UTC](https://forum.kirupa.com/t/object-moving-problem/17698/9 "2003-04-08T14:54:50Z")

</div>

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!?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [April 8, 2003, 3:05pm UTC](https://forum.kirupa.com/t/object-moving-problem/17698/10 "2003-04-08T15:05:14Z")

</div>

ah nvm i got it to work…i did:

this.\_x -= 5

thx though:)

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [April 8, 2003, 3:07pm UTC](https://forum.kirupa.com/t/object-moving-problem/17698/11 "2003-04-08T15:07:20Z")

</div>

oh!! i thought you wanted something like this… 😛

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

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [April 8, 2003, 3:09pm UTC](https://forum.kirupa.com/t/object-moving-problem/17698/12 "2003-04-08T15:09:19Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [April 8, 2003, 3:11pm UTC](https://forum.kirupa.com/t/object-moving-problem/17698/13 "2003-04-08T15:11:39Z")

</div>

ok then. good luck 😉 =)
