PDA

View Full Version : Is it possible to limit a key press??



V Kyrie
April 23rd, 2002, 12:14 AM
u see, i've done a key press and an animation leading to it... but, is there any way to limit it so dat it can be only be pressed for a given number of times...? after which when pressed, the animation will not be executed.....

upuaut8
April 23rd, 2002, 12:20 AM
easy enough, I think.

add to the button's actions a counter like so
on(release){
if(buttonInaction<10){
//any other actions go here
buttonInaction++;
}
}

this should count each time the button is pressed and when it reaches 10 it will stop working..

upuaut8
April 23rd, 2002, 08:52 PM
thanks for your reply. but i have no idea where to fit it in. here's my code >>>

if (Key.isDown(Key.UP) && !_root.aGrid[_root.aPlayerPos[0] -1][_root.aPlayerPos[1]]) {
_root.aPlayerPos[0] --;
gotoAndPlay ("lbUp");
} else if (Key.isDown(Key.DOWN) && !_root.aGrid[_root.aPlayerPos[0] +1][_root.aPlayerPos[1]]) {
_root.aPlayerPos[0] ++;
gotoAndPlay ("lbDown");
} else if (Key.isDown(Key.LEFT) && !_root.aGrid[_root.aPlayerPos[0] ][_root.aPlayerPos[1]-1]) {
_root.aPlayerPos[1]--;
gotoAndPlay ("lbLeft");
} else if (Key.isDown(Key.RIGHT) && !_root.aGrid[_root.aPlayerPos[0] ][_root.aPlayerPos[1]+1]) {
_root.aPlayerPos[1]++;
gotoAndPlay ("lbRight");
} else if (Key.getCode() == 97) {
gotoAndPlay ("throw 1");
} else if (Key.getCode() == 98) {
gotoAndPlay ("throw 2");
} else if (Key.getCode() == 99) {
gotoAndPlay ("throw 3");
} else if (Key.getCode() == 100) {
gotoAndPlay ("throw 4");
} else if (Key.getCode() == 102) {
gotoAndPlay ("throw 6");
} else if (Key.getCode() == 103) {
gotoAndPlay ("throw 7");
} else if (Key.getCode() == 104) {
gotoAndPlay ("throw 8");
} else if (Key.getCode() == 105) {
gotoAndPlay ("throw 9");
} else {
gotoAndPlay (_currentframe-1);
}

as you can see, when a key is pressed an animation is played. Now i want to limit this >>
} else if (Key.getCode() == 97) {
gotoAndPlay ("throw 1");

thanks....

upuaut8
April 23rd, 2002, 09:46 PM
I edited the post to get rid of those pesky Emoticons.. they apear all in the code if you don't turn them off.

I'll think about this. I think I'm going to need to print this out to decypher it. :)

Aaron Burgess
April 24th, 2002, 12:29 AM
Without fully going through your code you can do this (sample taken from your script.

if (Key.isDown(Key.UP) && !_root.aGrid[_root.aPlayerPos[0] -1][_root.aPlayerPos[1]] && upKey < 10) {
upKey += 1;
_root.aPlayerPos[0] --;
gotoAndPlay ("lbUp");
} else if (Key.isDown(Key.DOWN) && !_root.aGrid[_root.aPlayerPos[0] +1][_root.aPlayerPos[1]] && downKey < 10) {
downKey += 1;
_root.aPlayerPos[0] ++;
gotoAndPlay ("lbDown");

It may not be exactly what you're after. If your looking to only have the user to press the keyboard 10 times no matter what key is pressed, then what about...

if (pressKey < 10) {
pressKey += 1;
insert all your other code in here
}

Still knowing me, I;m probably wrong. :)


(edited to remove Emoticons)

V Kyrie
April 24th, 2002, 01:13 AM
i'll be waiting upuaut8...... thanks.....

hmm, thanks aaron... i've looked through and tried, but i have no idea why it doesn't work....

and yeah, i was thinking of limiting d keypress regardless of any key..... but how?

upuaut8
April 24th, 2002, 07:02 PM
Sorry.. been a little distracted trying to make changes on my guestbook and such. I haven't forgotten about you.

Aaron Burgess
April 29th, 2002, 06:00 PM
I forgot all about this question until yesterday. Sorry.

This code is based on the concept that the source you have given above is inside a movieclip.

To limit a key press, the way I solved the puzzle required 2 movieclips. One the tester, and one that does the action.

The test clip I placed outside of the stage and called it testaction The actionscript for that clips is

onClipEvent (keyDown) {
pressKey ++;
}

The movieclip with your code in it I called doaction It's code is

onClipEvent (enterFrame) {
if (oldpressKey < _root.testaction.pressKey && oldpressKey < 11) {
oldpressKey = _root.testaction.pressKey;
// insert your required code here.
}
}

All you have to do then is change 11 to whatever limit you want in key presses. To quickly test that this code worked I just added the line

_x += 10;

under oldkeypress = _root.testaction.pressKey; which made doaction clip move everytime until I hit my key press limit of 10.

Let me know if this works.

V Kyrie
April 30th, 2002, 04:10 AM
thankx aaron..... i juz read your tip and i'm gonna try it out soon..... i'll get back to you if it works...

Aaron Burgess
April 30th, 2002, 08:34 PM
Here's the cleaned up code. My brain is not working at all. Only need one movie clip, not two. God knows what I was thinking.

onClipEvent (keyDown) {
pressKey ++;
}
onClipEvent (enterFrame) {
if (oldpressKey < pressKey && oldpressKey < 10) {
oldpressKey = pressKey;
// _x +=10; this is the line to test it, or where to insert your code
}
}

Sorry.