# Key press in increments?

**URL:** <https://forum.kirupa.com/t/key-press-in-increments/38768>\
**Category:** Uncategorized\
**Created:** [December 29, 2003, 5:48pm UTC](https://forum.kirupa.com/t/key-press-in-increments/38768 "2003-12-29T17:48:29Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![lunatic](https://avatars.discourse-cdn.com/v4/letter/l/73ab20/32.png) [@lunatic](https://forum.kirupa.com/u/lunatic)\
**Post date:** [December 29, 2003, 5:48pm UTC](https://forum.kirupa.com/t/key-press-in-increments/38768/1 "2003-12-29T17:48:29Z")

</div>

Happy Holidays Kirupians!

Back from a quick xmas break with this question:

WHY AM I NOT OUT SNOWBOARDING?! THERE ARE 20 NEW INCHES OF POWDER ON THE MOUNTAIN! ARGH! WORK SUCKS!

But to a more forum-appropriate question:

I have this prototype function:  
[AS]  
MovieClip.prototype.moveRight = function() {  
// when it enters the frame  
this.onEnterFrame = function() {  
oldx = newx;  
// if timelineMC.\_x less than or equal to howFarRight  
if (timeline.\_x\<=howFarRight) {  
// delete the onEnterFrame so it can’t scroll  
delete this.onEnterFrame;  
} else {  
// else keep scrolling it  
\_global.scrolling = true;  
timeline.\_x -= speed;  
newx = timeline.\_x;  
\_global.timex = newx-oldx;  
}  
};  
};  
[/AS]

and call it this way:

[AS]  
left.onPress = moveLeft;  
right.onPress = moveRight;  
[/AS]

Now, I would like to also be able to use the left and right arrow keys to accomplish the same thing but I’m not sure how to script it since prototypes aren’t really my thing and since I used MCs instead of buttons (so I can’t say “on (release, keyPress\<LEFT\>)” etc.).

I tried using this:  
[AS]  
this.onKeyDown = function() {  
if (Key.getCode() == Key.LEFT) {  
moveLeft();  
}  
if (Key.getCode() == Key.RIGHT) {  
moveRight();  
}  
};  
Key.addListener(this);  
[/AS]

which works great except that it scrolls the whole thing all at once. Is it possible to put some kind of increment in there so that on a key press it only moves a certain amout of pixels?

Thanks for any help!

---

<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:** [December 29, 2003, 7:15pm UTC](https://forum.kirupa.com/t/key-press-in-increments/38768/2 "2003-12-29T19:15:14Z")

</div>

```auto
var speed = 10;
var howFarRight = 400;
var howFarLeft = 100;
this.onKeyDown = function() {
var direction = Key.isDown(39)-Key.isDown(37);
var x = this._x+direction*speed;
this._x = Math.min(Math.max(howFarLeft, x), howFarRight);
};
Key.addListener(this);

```

?? 😛

---

<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:** [December 29, 2003, 7:47pm UTC](https://forum.kirupa.com/t/key-press-in-increments/38768/3 "2003-12-29T19:47:10Z")

</div>

Wow! That shifted the entire screen to the left no matter which key I pressed!

A cool effect, though not entirely what I had in mind 😉

Could you explain your code? Maybe I can modify it?

thanks kode!

---

<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:** [December 29, 2003, 9:22pm UTC](https://forum.kirupa.com/t/key-press-in-increments/38768/4 "2003-12-29T21:22:58Z")

</div>

```auto
MovieClip.prototype.addMovement = function() {
	var speed = 10;
	var howFarRight = 400;
	var howFarLeft = 100;
	this.onKeyDown = function() {
		var direction = Key.isDown(39) - Key.isDown(37);
		var x = this._x + direction * speed;
		this._x = Math.min(Math.max(howFarLeft, x), howFarRight);
	};
	Key.addListener(this);
};
myMC.addMovement();

```

---

<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:** [December 29, 2003, 9:31pm UTC](https://forum.kirupa.com/t/key-press-in-increments/38768/5 "2003-12-29T21:31:48Z")

</div>

Howdy 😃

This works a little better . . .

I already have the variables of speed, howFarRight, and howFarLeft defined for the scroll buttons I am using. So I removed those from the code you wrote but left everything else. However, when either the right OR left arrow key is pressed it just jumps to the value of howFarRight. So I see the right side of myMC but it’s not just moving in increments.

Since I have MCs that act as buttons for scrolling and when you click them they just move the MC a little bit at a time then why can’t I achieve the same effect with a keystroke? Doesn’t make sense to me, especially since if it was a button and not a symbol I could say

[AS]  
on (press, keyPress\<LEFT\>, release, rollOut)  
[/AS]

etc.? :h:

---

<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:** [December 29, 2003, 9:32pm UTC](https://forum.kirupa.com/t/key-press-in-increments/38768/6 "2003-12-29T21:32:04Z")

</div>

In that case… it would be better is you pass the variables as parameters to the method, so that you can easily use different values for each movie clip. 😉

```auto
MovieClip.prototype.addMovement = function(speed, howFarRight, howFarLeft) {
this.onKeyDown = function() {
var direction = Key.isDown(39)-Key.isDown(37);
var x = this._x+direction*speed;
this._x = Math.min(Math.max(howFarLeft, x), howFarRight);
};
Key.addListener(this);
};
my_mc.addMovement(10, 400, 100);

```

And… which part of the code did you not understand, lunatic? 🙂

---

<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:** [December 29, 2003, 9:40pm UTC](https://forum.kirupa.com/t/key-press-in-increments/38768/7 "2003-12-29T21:40:40Z")

</div>

Well, the whole Math min max stuff I only sort of have half a brain wrapped around.

I really only have one movie clip that I am trying to scroll (at least for now) so the variables like speed and howFarLeft/Right won’t change. It’s a big long timeline with a mask. I followed the kirupa tutorial on scrolling an MC and it works great. Another Kirupian helped me to modify it so I could add some other functionality to it (having a little bar ease over it on mouseover + other stuff).

Now I would just like the user to be able to use the left and right arrow keys as substitutes (but not in place of) the scroll buttons (which are really mcs). I thought it would be something simple like onPress = Key.LEFT = moveLeft (the prototype function) but I guess it’s way more complicated!

It’s not a necessity to the functionality of what I’m doing but my boss would like to see it if it’s possible.

Thanks for all your help everyone!

---

<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:** [December 29, 2003, 9:48pm UTC](https://forum.kirupa.com/t/key-press-in-increments/38768/8 "2003-12-29T21:48:56Z")

</div>

It’s quite simple… those methods will keep the number between _howFarLeft_ and _howFarRight_; so you’ll always have a number greater than or equal to _howFarLeft_, and less than or equal to _howFarRight_. 😉

…Got 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:** [December 29, 2003, 9:53pm UTC](https://forum.kirupa.com/t/key-press-in-increments/38768/9 "2003-12-29T21:53:36Z")

</div>

I guess so. But how do you know which pair to get the min of and which pair to get the max of? And why do you subtract right key press from left key press? I don’t really get that either.
