How can I enable and disable an Object randomly?

Hey there!

I have to deliver an assignment this week that is basically a Shoot that Spaceship (little spaceship on top and a cannon to shoot it). One of the parts im struggling with is to create a Shield around the Spaceship that is randomly enabled and disabled. How would I code this last part? I can't come up with anything to randomly turn it on and off. Any guidelines are deeply appreciated!

Thanks in advance! <3

Answers

  • Have a flag holding the current state.

    Toggle that state if random(n) is less than one within draw(). Remember that draw runs as a loop, 60 times a second. So choose n so that there's a decent looking delay between changes - too low and it'll flicker, too high and it won't change enough.

  • Answer ✓

    More convincingly you'd have a delay counter. Set the counter to a random number, say between 60 and 120. Decrement it each frame. When it reaches 0 then toggle the shield state and choose another random delay. This gives you more control, but is more code.

Sign In or Register to comment.