PDA

View Full Version : How to stop someone holding down key


danb
04-05-2005, 04:21 PM
Hi,

I have a keyboard-bashing game, which uses this code to do the work:


onClipEvent (keyDown) {
bounce = false;
xscale = Number(xscale*1.008);
yscale = Number(yscale*0.992);
}
onClipEvent (keyUp) {
bounce = false;
xscale = Number(100);
yscale = Number(100);
}
onClipEvent (keyDown) {
intCount++;
_level1.intTotalCount++;

if (intCount==1) {
loadMovieNum("sound2.swf",2);
loaded=math.round(_level1.getBytesLoaded());
if (loaded<1) {
loadMovieNum("countdown.swf",1);
}
}

if ((intCount==1)||(intCount==4)||(intCount==10)||(in tCount==26)||(intCount==38)||(intCount==44)||(intC ount==52)) {
singer.Play();
}

setProperty(singer.mouth,_xscale,((intCount/0.8)+65));
setProperty(singer.mouth,_yscale,((intCount/0.8)+65));

if (intCount==22){
loadMovieNum("sound2.swf",3);
}
if (intCount==28){
loadMovieNum("sound2.swf",4);
}
if (intCount==38){
loadMovieNum("sound2.swf",5);
}

if (intCount==32) {
singer.eyes.Play();
loadMovieNum("sound3.swf",6);
}

if (intCount>=24) {
setProperty(singer.eyes,_xscale,((intCount/0.8)+65));
setProperty(singer.mouth,_yscale,((intCount/0.8)+65));
}

if (intCount>=20) {
setProperty (singer, _xscale, (((intCount-20)/12)+30));
}

if (intCount==52) {
unloadMovieNum(2);
unloadMovieNum(3);
unloadMovieNum(4);
unloadMovieNum(5);
unloadMovieNum(6);
_global.intHelmets++;
_root.play();
}
}



It can probably be shortened and optimised, but I'm not worried about that now.

I basically need to stop someone from holding the key down which is obviously cheating!

I've found this code posted elsewhere on the forum, but my lack of actionscript knowlege means that I have no idea how to apply it:


controls = new Object()
controls.onKeyDown = function() {
if (Key.isDown(Key.SPACE) == true && spaceDown == false) {
moving = true
player.gotoAndPlay(5)
spaceDown = true
}
}
controls.onKeyUp = function() {
if (Key.isDown(Key.SPACE) == false && spaceDown == true) {
spaceDown = false
}
}
Key.addListener(controls)


Can anyone show me how to combine the two? I'm really crap when it comes to actionscript, so the simpler the better!

Thanks very much...

ski_la
04-05-2005, 07:42 PM
Use the onClipEvent(keyUp) event to make sure that you don't do anything unless the user releases the key.

e.g.

onClipEvent (keyUp) {

//do stuff here instead of in onClipEvent(keyDown).
intCount++;
_level1.intTotalCount++;
//optimisation - use a switch instead of an if statement
switch(intCount) {
case 1:
loadMovieNum("sound2.swf",2);
loaded=math.round(_level1.getBytesLoaded());
if (loaded<1) {
loadMovieNum("countdown.swf",1);
}
break;
case 2:
//etc
break;
default:
//do nothing
break;
}
}

danb
04-05-2005, 07:56 PM
Ahhhhhh, must think outside of the box next time! Swapped the key-up and key-downs and it now works a treat. Thanks for the tips regarding using switch as opposed to ifs - I've just been in a desparate rush to get it working before I start optimising the coding (which even with my limited skills can be improved!)

Thanks again.

everlong
04-28-2005, 05:27 PM
I'm also having the same issue which can not be solved by doing this. My program uses key.isDown instead. any suggestions?

Barn
04-29-2005, 03:50 AM
Hit the user over the head with a rubber mallet.

Geez, read the thread -- you need to change your event so that it does NOT continue to perform an action when the key is down.

everlong
04-29-2005, 04:55 PM
but it would not work the same way. this would not validly check for the key because when a key is up there is no key.isDown. You cannot go
onClipEvent(keyUp) {
if (key.isDown(65)) {
blah blah blah
}
}
Therefore the solution posted above would not work. Or am I completely wrong?

Barn
04-29-2005, 05:32 PM
If you want an action, upon an event, to only happen once, but you are using an event that continuously polls a state, you have to set up your own script that ensures that the action occurs only once, which is precisely what is demonstrated, above (just with a different triggering event).

everlong
04-29-2005, 05:39 PM
That's not quite the intention. I want the user to have to press the button individually everytime he wishes to hit the opponent rather than be able to hold the key and rack in the points.

Barn
04-29-2005, 06:33 PM
Exactly. Precisely. That's what I just said. You are using an event that does a constant state poll for performing an action one time, so you must limit the number of times the action is performed with your own script, as explained above.

tg
04-30-2005, 08:16 AM
if you do the code above, (keyup). then a cheater can hold the key down as long as they want, and nothing will happen.
they must release the key to get a result, if they want to repeat that result, then they must press and release again, and again, and again....

if you do it on press, they can hold the key down, if you do it on release, then they must press and release for you event to trigger. thus circumventing the cheat.

tg
04-30-2005, 08:18 AM
ahh. i see gibberish has provided the code you need in another thread. heh. it looks un-surprisingly alot like the code above

Fraise
12-17-2008, 09:02 AM
if you do the code above, (keyup). then a cheater can hold the key down as long as they want, and nothing will happen.
they must release the key to get a result, if they want to repeat that result, then they must press and release again, and again, and again....

if you do it on press, they can hold the key down, if you do it on release, then they must press and release for you event to trigger. thus circumventing the cheat.

here is a diagram mental digram

user pushes key.
nothing happens.
user releases key.
points are scored.

simple.

now get a code that does that... the one previously said should work fine.