# Moveing with KEYS

**URL:** <https://forum.kirupa.com/t/moveing-with-keys/16678>\
**Category:** Uncategorized\
**Created:** [March 27, 2003, 12:09pm UTC](https://forum.kirupa.com/t/moveing-with-keys/16678 "2003-03-27T12:09:42Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![FlashSwimmer](https://avatars.discourse-cdn.com/v4/letter/f/35a633/32.png) [@FlashSwimmer](https://forum.kirupa.com/u/FlashSwimmer)\
**Post date:** [March 27, 2003, 12:09pm UTC](https://forum.kirupa.com/t/moveing-with-keys/16678/1 "2003-03-27T12:09:42Z")

</div>

hi guys,  
i have tow quesation…?  
first one :  
i’m working in big map… i make a buttons to move it right, left, up and down. it works great. tow buttons for zoom in and out and it works too…  
i need to make it move with 45 degree when i press up and right… how can i do it, but i don’t want to rotate it.

second:  
how can i make zoom with selection… i mean when i press mouse and draw rectangle in somewhere it makes zoom for this area.

thanx

---

<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:** [March 28, 2003, 7:05am UTC](https://forum.kirupa.com/t/moveing-with-keys/16678/2 "2003-03-28T07:05:28Z")

</div>

nobody can HELP me… ☹

---

<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:** [March 28, 2003, 10:57am UTC](https://forum.kirupa.com/t/moveing-with-keys/16678/3 "2003-03-28T10:57:18Z")

</div>

Concerning your first question, it depends on your code, really. When I have to do things like that, I usually use temporary variables to store the position change. Something like that:

```auto
function listenForKeys(){
  if (Key.isDown(Key.LEFT)) yourClip.x=1;
  if (Key.isDown(Key.RIGHT)) yourClip.x=-1;
  if (Key.isDown(Key.UP)) yourClip.y=1;
  if (Key.isDown(Key.DOWN)) yourClip.y=-1;
}

function updatePosition(){
  yourClip._x+=yourClip.x;
  yourClip._y+=yourClip.y;
}

this.onEnterFrame=function(){
  listenForKeys();
  updatePosition();
}

```

Concerning the second, I have no idea :trout:

---

<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:** [March 28, 2003, 11:47am UTC](https://forum.kirupa.com/t/moveing-with-keys/16678/4 "2003-03-28T11:47:07Z")

</div>

what about tow keys in the same times… (-:  
i mean when you play game and want to move you ship to 45 drgree… so you will press Right and UP… :nerd:

How can i do it?  
Concerning this in the first … 🙂

thanx

---

<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:** [March 28, 2003, 11:55am UTC](https://forum.kirupa.com/t/moveing-with-keys/16678/5 "2003-03-28T11:55:37Z")

</div>

That case is handled, Flashswimmer. Look at the listenForKeys function 🙂 But there will be a problem if you press left AND right for instance…

---

<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:** [March 28, 2003, 3:21pm UTC](https://forum.kirupa.com/t/moveing-with-keys/16678/6 "2003-03-28T15:21:46Z")

</div>

Can’t you just do…

[AS]if(Key.isDown(Key.RIGHT) && Key.isDown(Key.UP)) {  
yourClip.x=1;  
yourClip.y=1;  
}[/AS]

??

---

<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:** [March 28, 2003, 3:30pm UTC](https://forum.kirupa.com/t/moveing-with-keys/16678/7 "2003-03-28T15:30:05Z")

</div>

I tried this, and it seemed to work…  
[AS]speed = 1;  
moveClip = new Object();  
moveClip.onKeyDown = function() {  
if (Key.isDown(Key.LEFT)) {  
yourClip.\_x += speed;  
}  
if (Key.isDown(Key.RIGHT)) {  
yourClip.\_x += -speed;  
}  
if (Key.isDown(Key.UP)) {  
yourClip.\_y += speed;  
}  
if (Key.isDown(Key.DOWN)) {  
yourClip.\_y += -speed;  
}  
};  
Key.addListener(moveClip);[/AS]

---

<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:** [March 28, 2003, 3:39pm UTC](https://forum.kirupa.com/t/moveing-with-keys/16678/8 "2003-03-28T15:39:23Z")

</div>

Why make something simple when you can obfuscate it? :sure:

No, actually, what I wrote can (could?) be useful if you had to test the position to be sure you’re not out of bounds. Man, I just can’t explain myself today :crazy: 😛

---

<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:** [March 28, 2003, 3:40pm UTC](https://forum.kirupa.com/t/moveing-with-keys/16678/9 "2003-03-28T15:40:47Z")

</div>

[AS]moveClip = new Object();  
moveClip.onKeyDown = function() {  
Key.isDown(Key.LEFT) ? yourClip.\_x += speed : null;  
Key.isDown(Key.RIGHT) ? yourClip.\_x += -speed : null;  
Key.isDown(Key.UP) ? yourClip.\_y += speed : null;  
Key.isDown(Key.DOWN) ? yourClip.\_y += -speed : null;  
};  
Key.addListener(moveClip);[/AS]

😛

Yeah, your method could be useful in some situations indeed, I wasn’t putting your method down, just offering alternative solutions.

---

<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:** [March 28, 2003, 3:50pm UTC](https://forum.kirupa.com/t/moveing-with-keys/16678/10 "2003-03-28T15:50:51Z")

</div>

Ok, how about with a little easing?

[AS]easeSpeed = 4;  
slideSpeed = 10;  
MovieClip.prototype.easeTo = function(xDir, yDir) {  
endX = this.\_x+xDir;  
endY = this.\_y+yDir;  
this.onEnterFrame = function() {  
this.\_x += (endX-this.\_x)/easeSpeed;  
this.\_y += (endY-this.\_y)/easeSpeed;  
};  
};  
\_root.onEnterFrame = function() {  
Key.isDown(Key.LEFT) ? yourClip.easeTo(slideSpeed, 0) : null;  
Key.isDown(Key.RIGHT) ? yourClip.easeTo(-slideSpeed, 0) : null;  
Key.isDown(Key.UP) ? yourClip.easeTo(0, slideSpeed) : null;  
Key.isDown(Key.DOWN) ? yourClip.easeTo(0, -slideSpeed) : null;  
Key.isDown(Key.RIGHT) && Key.isDown(Key.UP) ? yourClip.easeTo(-slideSpeed, slideSpeed) : null;  
Key.isDown(Key.LEFT) && Key.isDown(Key.UP) ? yourClip.easeTo(slideSpeed, slideSpeed) : null;  
Key.isDown(Key.RIGHT) && Key.isDown(Key.DOWN) ? yourClip.easeTo(-slideSpeed, -slideSpeed) : null;  
Key.isDown(Key.LEFT) && Key.isDown(Key.DOWN) ? yourClip.easeTo(slideSpeed, -slideSpeed) : null;  
};[/AS]

Man… I am sooo bored. I gotta watch my dog today (he just had surgery yesterday) and all he is doing is sleeping!!! Which is normal, but still, sheesh, I need SOMETHING to do here.

---

<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:** [March 28, 2003, 3:54pm UTC](https://forum.kirupa.com/t/moveing-with-keys/16678/11 "2003-03-28T15:54:22Z")

</div>

How about that scripting battle? 😛

---

<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:** [March 28, 2003, 3:56pm UTC](https://forum.kirupa.com/t/moveing-with-keys/16678/12 "2003-03-28T15:56:25Z")

</div>

You can also check the levitated update 🙂

---

<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:** [March 28, 2003, 3:58pm UTC](https://forum.kirupa.com/t/moveing-with-keys/16678/13 "2003-03-28T15:58:06Z")

</div>

Ermmm :-\

I dunno yet, I have NO clues as towhat to make, and my scripting ability with just AS isn’t too hot, especially when trying to keep it withing a certain amount of lines.

You know my code efficiency and optimization sucks 😛

---

<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:** [March 28, 2003, 4:02pm UTC](https://forum.kirupa.com/t/moveing-with-keys/16678/14 "2003-03-28T16:02:05Z")

</div>

Just checked Jared Tarbells new stuff. Almost wish I hadn’t, now I am just more jealous of him again… :hangover:

I like the walking critters thing, I always wanted to learn how to do that in AS. Any idea on where to learn? Or is it going to be one of those “try and see” type of deals?

---

<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:** [March 28, 2003, 4:03pm UTC](https://forum.kirupa.com/t/moveing-with-keys/16678/15 "2003-03-28T16:03:56Z")

</div>

You’re too modest, as always, and concerning levitated, it’s open source, so take a shovel and dig, Lost!!

---

<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:** [March 28, 2003, 4:12pm UTC](https://forum.kirupa.com/t/moveing-with-keys/16678/16 "2003-03-28T16:12:09Z")

</div>

LOL, it is Jared Tarbell though! His code is about as readable as Latin converted to Mandarin. Not to mention this particular experiment is OOP :hangover: \<-- OOP does that to me.

But he does define everything into a bunch of tiny little functions, which makes it a bit easier to follow.

---

<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:** [March 28, 2003, 4:47pm UTC](https://forum.kirupa.com/t/moveing-with-keys/16678/17 "2003-03-28T16:47:46Z")

</div>

hi man… this is my code  
[AS]  
speed = 5;  
\_root.onEnterFrame = function() {  
if (Key.isDown(Key.RIGHT)) {  
\_root.big\_city.\_x += speed;  
} else if (Key.isDown(Key.LEFT)) {  
\_root.big\_city.\_x -= speed;  
} else if (Key.isDown(Key.UP)) {  
\_root.big\_city.\_y -= speed;  
} else if (Key.isDown(Key.DOWN)) {  
\_root.big\_city.\_y += speed;  
}else if (Key.isDown(79)) {  
\_root.fullmap.map.text\_all.\_visible =0  
//\_root.logo.\_visible =0  
}else if (Key.isDown(67)) {  
\_root.fullmap.map.text\_all.\_visible =1  
//\_root.logo.\_visible =1  
}else if (Key.isDown(90)) {  
\_root.bag\_city.\_xscale +=10  
\_root.bag\_city.\_yscale +=10  
}else if (Key.isDown(88)) {  
\_root.bag\_city.\_xscale -=10  
\_root.bag\_city.\_yscale -=10  
}else if (Key.isDown(76)) {  
\_root.logo.\_visible =0  
}else if (Key.isDown(71)) {  
\_root.logo.\_visible =1  
}  
};  
[/AS]

it works good but when i tired you code (&&) i don’t know why it didn’t work.  
i didn’t know how to do it with function… i thought about function but i failed… ☹  
but now it works… thanx

---

<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:** [March 28, 2003, 4:53pm UTC](https://forum.kirupa.com/t/moveing-with-keys/16678/18 "2003-03-28T16:53:14Z")

</div>

Well so far thers a ton of different methods of this in this very thread.

But I think the problem here lies in the else if statements.

See, an else if says that if this is false then check this and if this is false then check the one after this. Now, when it finally reaches an if statement that is true, it stops running.

So unless you press both keys at the exact same time, the if statments will stop at the first button pressed and it won’t include the second button.

---

<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:** [March 28, 2003, 4:58pm UTC](https://forum.kirupa.com/t/moveing-with-keys/16678/19 "2003-03-28T16:58:55Z")

</div>

so that was my problem… :hangover:  
i got the idea now lostinbeta… 🙂  
thanx to you and POM 😊

---

<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:** [March 28, 2003, 5:07pm UTC](https://forum.kirupa.com/t/moveing-with-keys/16678/20 "2003-03-28T17:07:40Z")

</div>

Glad you got it fixed 🙂

I added to my easing code to make it have easing zoom in capabilities too…

[AS]easeSpeed = 4;  
slideSpeed = 10;  
scaleBy = 10;  
MovieClip.prototype.easeTo = function(xDir, yDir) {  
endX = this.\_x+xDir;  
endY = this.\_y+yDir;  
this.onEnterFrame = function() {  
this.\_x += (endX-this.\_x)/easeSpeed;  
this.\_y += (endY-this.\_y)/easeSpeed;  
};  
};  
MovieClip.prototype.scaleEase = function(newSize) {  
mySize = this.\_xscale+newSize;  
this.onEnterFrame = function() {  
this.\_xscale = this.\_yscale += (mySize-this.\_xscale)/easeSpeed;  
};  
};  
\_root.onEnterFrame = function() {  
Key.isDown(Key.LEFT) ? yourClip.easeTo(slideSpeed, 0) : null;  
Key.isDown(Key.RIGHT) ? yourClip.easeTo(-slideSpeed, 0) : null;  
Key.isDown(Key.UP) ? yourClip.easeTo(0, slideSpeed) : null;  
Key.isDown(Key.DOWN) ? yourClip.easeTo(0, -slideSpeed) : null;  
Key.isDown(Key.RIGHT) && Key.isDown(Key.UP) ? yourClip.easeTo(-slideSpeed, slideSpeed) : null;  
Key.isDown(Key.LEFT) && Key.isDown(Key.UP) ? yourClip.easeTo(slideSpeed, slideSpeed) : null;  
Key.isDown(Key.RIGHT) && Key.isDown(Key.DOWN) ? yourClip.easeTo(-slideSpeed, -slideSpeed) : null;  
Key.isDown(Key.LEFT) && Key.isDown(Key.DOWN) ? yourClip.easeTo(slideSpeed, -slideSpeed) : null;  
Key.isDown(90) ? yourClip.scaleEase(scaleBy) : null;  
Key.isDown(88) ? yourClip.scaleEase(-scaleBy) : null;  
};[/AS]

(this assumes your map is a perfect square though)

[Next page](https://forum.kirupa.com/t/moveing-with-keys/16678.md?page=2)
