2

I'm using Flash CS6 with AS2.

I'm in a Game Design class here in High School and we have learned some basic coding. I am currently trying to add a "speed boost" feature when you press the shift button. You're supposed to be able to press it and that would give you a 5 second speed boost. After those 5 seconds, you'd revert back to normal speed. These are the variables I have made:

    speed = 6;
    boost = 16;
    boost_timer = 0;

I've set my speed to 6, and I called 16 (the ASCII code for the Shift Key) "boost". I've also added in a timer for the boos to count to the 5 seconds when I call for it in the main code. Here that part of the code:

    if(Key.isDown(boost))
    {
        speed = 0;
        boost_timer++;
        speed = 12
    }

-

    if(boost_timer >= 5)
    {
        boost_timer = 0;
        speed = 6;
    }

Now, what I'm trying to do here is make it so that when I press "boost", it will set my "boost_timer" to 0 (which would only matter if and when it's not already at 0). Then, it will start increasing my "boost_timer" and set my "speed" to double the original speed.

In the second piece of code, I make it so that once it reaches 5, it should set the timer back to 0. However, because "boost_timer++" had already been set, even with it getting set back to 0, it would still be increasing. But, even when it gets to 5, it will simply restart the timer and continue resetting the speed to 6. The next time I press the "boost" button, it should set the "boost_timer" back to 0 and re-do the whole thing.

I am planning on adding some sort of power up function for later that will restrict the amount of times you can use the boost, but for now, I would first like to make sure that my game is actually capable of using the boost multiple times.

The actual problem I'm having with the coding I have so far is that whenever I now press the "boost" button, it does, in fact, increase the speed. However, it stays at the boosted speed and never goes back on its own. And for some reason, whenever I press the "boost" button again while already boosted, it reverts back to original speed. It's as if the "boost" button is working as an activate and deactivate button for the boost, but I have no idea why and what part of my code is causing this.

Thank you very much for any help you can give!

Sam Miller
  • 23,808
  • 4
  • 67
  • 87
Joshweiser
  • 21
  • 3

1 Answers1

0

Found an answer for you from newgrounds:

If you just want to test if the key isnt pressed at the time, just use:
if(Key.isDown(Key.UP) == false)
or
if(!Key.isDown(Key.UP))

however, if you want to test for the exact point the key is lifted only, you have to use a latch sysetm:

if(Key.isDown(Key.UP)){
    latch = true;
}
if(!Key.isDown(Key.UP) && latch){
    latch = false;
    // put your actions here
}

you can see that it will only be that !Key.isDown(Key.UP) && latch once after the key is pressed, so it will perform the actions,then will wait until the key is pressed then released again to do the actions again.

And you shall probably run some function at the point where key is lifted up, to reduce speed and store speed boost leftover values.

also this latch variable shall be set to false on every frame at the point where no more functions precedes, so after event program enters next frame with its value being false


Umh! There is event listener for this already made: adobeMX

Community
  • 1
  • 1
animaacija
  • 170
  • 9
  • 25