1
\$\begingroup\$

I am making a game, where two zombies will spawn after every three moves my character makes. The problem is that I have no idea how to do this.

I have my character moving correctly, in each key press event, so I want the game to count each time one of these events are triggered, and perform the action after every third trigger. Perhaps I need a global variable/counter that can count these events?

How do I make instances of enemies spawn after every three keypress movements?

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Hi Toby, and welcome to Game Dev Stack Exchange. Check out our tour to see how we work. Please tag your Game Maker questions with [game-maker]; we prefer people do not put tags in titles. Also, when you reach 20 rep, you'll be able to join us in Game Development Chat. \$\endgroup\$ Commented Jun 12, 2017 at 12:19

2 Answers 2

2
\$\begingroup\$

Here is some pseudocode:

If keypress do
    KeyPressCounter++
End
If KeyPressCounter%3 == 0 do
    Action ()
End

This preforms a modulo operation which will detect numbers divisible by 3.

\$\endgroup\$
4
  • \$\begingroup\$ This doesn't appear to give any guidance on how to do this in game maker. \$\endgroup\$ Commented Jun 12, 2017 at 12:19
  • \$\begingroup\$ StackExchange is not here to code for you, this pseudocode has valid logic and can be applied in GML. I would recommend @DeaSTL to extend his explaination to help OP understand why this works. \$\endgroup\$ Commented Jun 13, 2017 at 16:47
  • \$\begingroup\$ Besides, i've never used game maker but i've used other libraries. \$\endgroup\$
    – DeaSTL
    Commented Jun 14, 2017 at 16:15
  • \$\begingroup\$ Simpler: if keypresscounter++%3 :) \$\endgroup\$
    – Charanor
    Commented Jun 15, 2017 at 11:18
0
\$\begingroup\$

If you want to create this type of event, I suggest this :

If your character moves with the directional keys, then create four Keypress event : one for each key (Up, Down, Right, Left). Create a variable in Create event, and increment this variable in the four event previously created. Then, in the Step event, check if the variable is equal at 3. If yes, do your action and set the variable at 0. If not, do nothing.

Here what it looks in code :

Create event :

number_key_pressed=0;

Key press left event :

number_key_pressed+=1;

Key press right event :

number_key_pressed+=1;

Key press up event :

number_key_pressed+=1;

Key press down event :

number_key_pressed+=1;

Step event :

if(number_key_pressed==3)
{action();
number_key_pressed=0;}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .