Spaceship and Hyperspeed
Create a new layer 1 and lock layer 0, so we don't mess up with the starfields.
Insert a keyboard object and the player's spaceship:
Give the spaceship the behavior 8Direction with the parameter Set angle to No.
We allow it to move in all directions, but it won't turn toward any angle.
The spaceship can travel around, but it needs more speed..
Your task: you might as well show the spaceship ist boundaries and it should not be able to leave the window! Hint: use four empty sprites with Solid behavior at the windows limits and when the ship collides with them, it stops its movement. In the .capx you'll see how, but do try it for yourself.
In the Event sheet, we'll insert a keyboard event, so first insert a keyboard object in the game and then:
Keyboard -> On key pressed -> H with the following actions:
Stars_back -> Set (bullet) speed to 150
Stars_front -> Set (bullet) speed to 200
When you run the game and at some point you press the 'H' key, the spaceship will travel at Hyperspeed.
There are two ways to stop Hyperspeed movement.
a) Use the same key or another key to stop Hyperspeed travelling.
b) Give the Hyperspeed a limited time to travel
Since a) is fairly easy to acomplish, let's take a look at alternative b).
We will introduce a global variable called HS_Time.
Within the above created keyboard event, introduce the following action:
System -> Set Value -> HS-Time to time+5
This will set the variable to the current game time + five (5) seconds.
Create a new event with the following condition:
System -> On every tick
Insert a sub-event containing the condition:
System -> Compare variable -> HS_Time -> Greater or equal -> time
and insert the bullet speed actions to this condition (there are now no Bullet speed actions in the keyboard event!).
We need to introduce the slow movement, so after the HS_Time > time comparison condition, insert an Else condition containing the following bullet speeds:
Stars_back -> Set (bullet) speed to 10
Stars_front -> Set (bullet) speed to 20
Your code should look like this:
Testing will lead you to the desired results: Press on the 'H' key and the ship will travel for 5 seconds at Hyperspeed.
Stop endless hyperspeed!
Pressing continuously on the 'H' key during Hyperspeed will raise the time of Hyperspeed travelling, but only 5 seconds are allowed.
Since there is no actions for the keyboard object (like "ignore key input") we'll insert a global variable called HS_chk. This variable is checked whether the ship is running at Hyperspeed or not.
In the keyboard event, insert the action
System -> Set HS_chk to 1
At the same time, these actions can only start if HS_chk is set to 0, so add a new condition under the On 'H' released condition:
System -> Compare variable HS_chk -> 0
Good this means, "if the player presses 'H' key and we are not in Hyperspeed mode (HS_chk=0), then add 5 seconds to the HS_Time variable and set Hyperspeed mode (HS_chk=1)".
In the System -> Every tick condition, the change into Hyperspeed is done while HS_Time > time and Hyperspeed mode is on (HS_chk=1). At some point, the time variable will be larger than HS_Time, so we'll slow down the ship and set the Hyperspeed mode to 0 (HS_chk=0). See the following code how I did it:
Congrats and have fun with your game programming!