Results 1 to 6 of 6

Thread: Keypress Question

  1. #1
    Join Date
    Feb 2008
    Posts
    90
    Thanks
    3
    Thanked 2 Times in 2 Posts

    Default Keypress Question

    im creating a game in which once the user presses space bar it calls the following code:
    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

  2. #2
    Join Date
    Jun 2008
    Posts
    589
    Thanks
    13
    Thanked 54 Times in 54 Posts
    Blog Entries
    1

    Default

    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:

    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;
    }
    }
    Last edited by magicyte; 10-28-2008 at 10:20 PM.

  3. #3
    Join Date
    Feb 2008
    Posts
    90
    Thanks
    3
    Thanked 2 Times in 2 Posts

    Default

    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

  4. #4
    Join Date
    Jun 2008
    Posts
    589
    Thanks
    13
    Thanked 54 Times in 54 Posts
    Blog Entries
    1

    Default

    WHOOPS!! This = flash?? Sorry. I was coding in JavaScript. Thought you had a jQuery statement in there...

    In JavaScript: (sorry)

    Sorry. Mistake. Fixed code:

    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

  5. #5
    Join Date
    Feb 2008
    Posts
    90
    Thanks
    3
    Thanked 2 Times in 2 Posts

    Default

    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?

  6. #6
    Join Date
    Mar 2007
    Location
    Currently: New York/Philadelphia
    Posts
    2,735
    Thanks
    3
    Thanked 519 Times in 507 Posts

    Default

    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.

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

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •