Key press in increments?

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!

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

?? :stuck_out_tongue:

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

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

thanks kode!

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

Howdy :smiley:

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:

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. :wink:

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? :slight_smile:

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!

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. :wink:

…Got it?

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.