# Movement using keyboard keys tute

**URL:** https://forum.kirupa.com/t/movement-using-keyboard-keys-tute/7349
**Category:** Uncategorized
**Created:** [October 29, 2002, 4:28am UTC](https://forum.kirupa.com/t/movement-using-keyboard-keys-tute/7349 "2002-10-29T04:28:26Z")
**Posts on this page:** 17
**Page:** 1

<div class="post-metadata">

### Author: ![dr\_ew](https://avatars.discourse-cdn.com/v4/letter/d/c89c15/32.png) [@dr\_ew](https://forum.kirupa.com/u/dr_ew)
#### Post date: [October 29, 2002, 4:28am UTC](https://forum.kirupa.com/t/movement-using-keyboard-keys-tute/7349/1 "2002-10-29T04:28:26Z")

</div>

Hi,

First, this site and the tutorials are great, thanks.

I am building a site for school in which i am using keyboard keys to control a character on screen. The character is only viewed from the side, and only moves left and right. In the tutorial, the bug changes direction using \_root.bug.\_rotation = ###; Because it is a side view, the rotation does not suit my needs. (I need the character to flip horizontally when changing directions, not rotate, so it stays upright…)  
Anyone able to suggest anything that might help me out? I am a bit of a flash noob, so forgive me if this is an ignorant question.  
Thanks!

---

<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: [October 29, 2002, 4:46am UTC](https://forum.kirupa.com/t/movement-using-keyboard-keys-tute/7349/2 "2002-10-29T04:46:18Z")

</div>

Make the bug 2 keyframes.

Keyframe 1 having him facing one direction, and keyframe 2 having him face another. Then when the arrow button is pressed, add this code…

\_root.bug.gotoAndStop(2);

and for the other way…

\_root.bug.gotoAndStop(1);

---

<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: [October 29, 2002, 5:44am UTC](https://forum.kirupa.com/t/movement-using-keyboard-keys-tute/7349/3 "2002-10-29T05:44:46Z")

</div>

Hi, thanks for your reply lostinbeta.

I made the changes, but now when i test the movie, the character flips back and forth; the movieclip keeps looping. I tried adding a stop, but then the character still will only face one way.  
How can i fix it? Thanks 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: [October 29, 2002, 5:46am UTC](https://forum.kirupa.com/t/movement-using-keyboard-keys-tute/7349/4 "2002-10-29T05:46:22Z")

</div>

Put a stop on both frames.

That way it will stop on frame one, which will be the default original position.

And you will of course need a stop action on frame 2 as well.

So just put a stop(); action on both frames and it should work.

---

<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: [October 29, 2002, 6:38am UTC](https://forum.kirupa.com/t/movement-using-keyboard-keys-tute/7349/5 "2002-10-29T06:38:40Z")

</div>

Hi  
i tried adding the other stop, but now it is stuck facing one direction only. I just want to make sure I have done things correctly, so here is what i’ve done:  
On the main timeline, i have 1 keyframe, with the button in it. the action for the button is:

on (keyPress “\<Left\>”) {  
currentX = this.\_x;  
this.\_x = currentX - 4;  
\_root.bug.gotoAndStop(1);

}  
on (keyPress “\<Right\>”) {  
currentX = this.\_x;  
this.\_x = currentX + 4;  
\_root.bug.gotoAndStop(2);

}  
Inside the button is the movie clip of the character, it has 2 keyframes, one for each direction, and each frame has a stop action on it.  
What am i doing wrong? Thanks a lot for helping me, i really appreciate it.

---

<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: [October 29, 2002, 6:42am UTC](https://forum.kirupa.com/t/movement-using-keyboard-keys-tute/7349/6 "2002-10-29T06:42:02Z")

</div>

o ya and the \<Left\> and \<Right\> are in there too…I forgot to put them in in my previous post.

---

<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: [October 29, 2002, 6:49am UTC](https://forum.kirupa.com/t/movement-using-keyboard-keys-tute/7349/7 "2002-10-29T06:49:04Z")

</div>

oic…thats left and right in brackets…

---

<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: [October 29, 2002, 6:54am UTC](https://forum.kirupa.com/t/movement-using-keyboard-keys-tute/7349/8 "2002-10-29T06:54:35Z")

</div>

Assuming you are putting this code on a Movie Clip symbol and that it starts off racing the right (facing right in frame 1 and left in frame 2)… this should work (untested)…

```php
on (keyPress "<Right>") {
	this.onEnterFrame = function() {
		this._x += 5;
		_root.bug.gotoAndStop(1);
		if (this._x>=100) {
			this._x = 100;
		}
		delete this.onEnterFrame;
	};
}
on (keyPress "<Left>") {
	this.onEnterFrame = function() {
		this._x -= 5;
		_root.bug.gotoAndStop(2);
		if (this._x<=10) {
			this._x = 10;
		}
		delete this.onEnterFrame;
	};
}

```

The if statements are in there to constrain it to an area. In this case it can’t go past 100px on the right or 10px on the left. It will just stop instead.

[edit] About time I got that thing to show right [/edit]

---

<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: [October 29, 2002, 7:11am UTC](https://forum.kirupa.com/t/movement-using-keyboard-keys-tute/7349/9 "2002-10-29T07:11:44Z")

</div>

It still is stuck facing one direction only. Sorry to keep buggin…

---

<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: [October 29, 2002, 7:14am UTC](https://forum.kirupa.com/t/movement-using-keyboard-keys-tute/7349/10 "2002-10-29T07:14:15Z")

</div>

You have a stop action on both frame 1 and frame 2 of your bug movie clip?

Frame 1 has a stop() action and the bug facing your right.

Frame 2 has a stop() action and the bug facing your left.

I just tested my code and it worked fine for me.

Actually, I can even edit that code just a tad and replace \_root.bug with “this” Since you will put this code right on the bug movie clip…

```php
on (keyPress "<Right>") {
    this.onEnterFrame = function() {
        this._x += 5;
        this.gotoAndStop(1);
        if (this._x>=100) {
            this._x = 100;
        }
        delete this.onEnterFrame;
    };
}
on (keyPress "<Left>") {
    this.onEnterFrame = function() {
        this._x -= 5;
        this.gotoAndStop(2);
        if (this._x<=10) {
            this._x = 10;
        }
        delete this.onEnterFrame;
    };
}

```

---

<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: [October 29, 2002, 6:24pm UTC](https://forum.kirupa.com/t/movement-using-keyboard-keys-tute/7349/11 "2002-10-29T18:24:43Z")

</div>

I posted a file for you to look at.

It works great for me and I used the code I gave you.

---

<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: [October 30, 2002, 4:13am UTC](https://forum.kirupa.com/t/movement-using-keyboard-keys-tute/7349/12 "2002-10-30T04:13:09Z")

</div>

Ok, ya i got it to work! Thanks so much for your help, much appreciated.  
I liked your site, great photoshop work.  
🙂

---

<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: [October 30, 2002, 4:16am UTC](https://forum.kirupa.com/t/movement-using-keyboard-keys-tute/7349/13 "2002-10-30T04:16:26Z")

</div>

I am glad you got it to work 🙂

Thanks for the compliments on my site and my Photoshop work =)

---

<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: [November 4, 2002, 7:29am UTC](https://forum.kirupa.com/t/movement-using-keyboard-keys-tute/7349/14 "2002-11-04T07:29:12Z")

</div>

Ok, I have another somewhat related question, so I thought I’d just add to this post.

Is it possible to use onKeyPress to make button objects work?

For instance, with the littleman .fla, if the character was moved over another button symbol, pressing the space bar on it would play another Scene. (an action placed on the button…)

I’ve been reading a lot, but I haven’t been able to figure out how this could be done. Any Ideas?

Thanks 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: [November 4, 2002, 7:41am UTC](https://forum.kirupa.com/t/movement-using-keyboard-keys-tute/7349/15 "2002-11-04T07:41:37Z")

</div>

No clue, but I have seen it done at [www.tweened.com](http://www.tweened.com).

My guess is something like…

```auto

if (this._x >= 50 && this._x<=100){
  //keypresscode;
}

```

This code was pulled out of the air. Totally untested, but it should work like… if theobject is between 50 and 100, and you press this key, it should do this…

Not possitive… in theory I suppose it should work :-\

---

<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: [November 4, 2002, 8:26am UTC](https://forum.kirupa.com/t/movement-using-keyboard-keys-tute/7349/16 "2002-11-04T08:26:59Z")

</div>

Ok, that makes sense, thanks!

I guess making the onkeypress interact with buttons in order to change scene is not really necessary. I tried adding a gotoAndPlay (“Scene”, 1) after the ‘if’ condition, but that didn’t seem to work for me. (surprise, surprise, hehe.) Anyways, thanks for the quick reply. I’m going to plug away at it a bit more, maybe I’ll find the answer trial and error stylez. If you have any suggestions, that would be sweet.

O ya that [tweened.com](http://tweened.com) site was cool. This project is trying to emulate [www.kerb.co.uk](http://www.kerb.co.uk)  
(…in a simplified “drawn super crappy, doesn’t even compare at all” kinda way…)

thanks lots 🙂

---

<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: [November 4, 2002, 8:30am UTC](https://forum.kirupa.com/t/movement-using-keyboard-keys-tute/7349/17 "2002-11-04T08:30:28Z")

</div>

WOW, THAT SITE IS AWESOME!!!

The cat totally rules.

BTW: Never get down on yourself 🙂 Your skills are always more than what you think.

I don’t have any suggestions right now. There was a guy named flex that used to post in this forum, but he dropped off the face of the earth :-\ He was excellent at game coding and things like this.

You can see his stuff at [http://flex.funkyolive.com](http://flex.funkyolive.com)
