Log in

View Full Version : Keypress Question



SChaput
10-28-2008, 09:57 PM
im creating a game in which once the user presses space bar it calls the following code:


on(keyPress "<Space>"){
_root.top1.play();
_root.bottom1.play();
totalNum=200;

// custom actions begin
_root.count = _root.count+1;
if (_root.count <= totalNum) {
duplicateMovieClip("_root.clip", "clip"+_root.count,_root.count);
_root["clip"+_root.count]._y = random(350)+50;
}
// custom actions end
}

I want this code to be called only once. However, the point of the game is to have keep something afloat by pressing the spacebar many many times a session. How do i make it so this section of code is only called once, instead of every time the user presses the spacebar.
Thank you

magicyte
10-28-2008, 10:03 PM
Simply make a variable in which some value is false. After the user presses the space bar, execute code that signals the user has pressed space. Also, place an if statement in your code. Exampe:


var pressSpace = false;
on(keyPress "<Space>"){
if(!pressSpace) {
_root.top1.play();
_root.bottom1.play();
totalNum=200;

// custom actions begin
_root.count = _root.count+1;
if (_root.count <= totalNum) {
duplicateMovieClip("_root.clip", "clip"+_root.count,_root.count);
_root["clip"+_root.count]._y = random(350)+50;
}
// custom actions end
pressSpace = !pressSpace;
}
}

SChaput
10-28-2008, 10:09 PM
When i put that code in i get an error, "Statement must appear within on(onCLipEvent) handler - var pressSpace=False

This code is on a movieclip.
Sorry if im not following something easy.
Thank you for your help

magicyte
10-28-2008, 10:20 PM
WHOOPS!! This = flash?? Sorry. I was coding in JavaScript. Thought you had a jQuery statement in there...

In JavaScript: (sorry)

Sorry. Mistake. Fixed code:


var pressSpace = false;
on(keyPress "<Space>"){
if(!pressSpace) {
_root.top1.play();
_root.bottom1.play();
totalNum=200;

// custom actions begin
_root.count = _root.count+1;
if (_root.count <= totalNum) {
duplicateMovieClip("_root.clip", "clip"+_root.count,_root.count);
_root["clip"+_root.count]._y = random(350)+50;
}
// custom actions end
pressSpace = !pressSpace;
}
}

Forgot bang (!).

-magicyte

SChaput
10-28-2008, 10:22 PM
I changed the code and put in the new section. However i am still getting the same, "Must appear within OnClip Event handler." Is it bad to have this part of code on a movieclip?

Medyman
10-29-2008, 12:35 AM
Is it bad to have this part of code on a movieclip?
Yup!

Well, not really bad. But you're getting outside the scope of the onClip Event handler. You should move the code to the timeline. Otherwise, magicyte's logic is right (didn't check his code).

In simpler terms, here is the logic in pseudo code.


var presssed= false;
function spaceBar() {
if(clicked == false) {
// Perform the functions!
pressed = true;
}
else {
trace("You already pressed the spacebar!");
}
}

You would of course need to assign the function to the event. You've probably seen this before, but here (http://gotoandlearn.com/play?id=21) is a tutorial on using ActionScript's key methods. Using the methods of this tutorial will help you reduce a lot of code in the long run.