1
\$\begingroup\$

I am making a zombie game. I want to have it so zombies randomly spawn. My code for this is:

instance_create(
  obj_player.x + irandom_range(-300, 300),
  obj_player.y + irandom_range(-300, 300), 
  obj_zom_1 )  

The problem with this is that zombies will sometimes spawn really close to the player. I want to have it so zombies spawn randomly but to NOT spawn on the visible screen, because seeing zombies materialize out of nowhere looks silly.

How would I do that?

\$\endgroup\$

2 Answers 2

3
\$\begingroup\$

I don't know the expected limits of your coordinates so I made them constant-like (i.e. screenLeftX). My snippet chooses a random edge of the screen and then a random value between the appropriate coordinate limits of that edge.

var positionAlongEdge = 0;
var which = irandom_range(1,4);
if (which == 1) //top edge
{
  positionAlongEdge = irandom_range(screenLeftX, screenRightX);
  instance_create(positionAlongEdge, screenTopY, obj_zom_1);
  //If the shape of zombies causes them to appear on-screen (at the edge),
  //you can push them further beyond the edge:
  //instance_create(positionAlongEdge, screenTopY - 10, obj_zom_1);
  //or
  //instance_create(positionAlongEdge, screenTopY - zombieHeight, obj_zom_1);
}
else if (which == 2) //bottom edge
{
  positionAlongEdge = irandom_range(screenLeftX, screenRightX);
  instance_create(positionAlongEdge, screenBottomY, obj_zom_1);
}
else if (which == 3) //left edge
{
  positionAlongEdge = irandom_range(screenTopY, screenBottomY);
  instance_create(screenLeftX, positionAlongEdge, obj_zom_1);
}
else if (which == 4) //right edge
{
  positionAlongEdge = irandom_range(screenTopY, screenBottomY);
  instance_create(screenRightX, positionAlongEdge, obj_zom_1);
}
\$\endgroup\$
5
  • \$\begingroup\$ Please explain your code and your train of thoughts. \$\endgroup\$
    – Vaillancourt
    Mar 12, 2016 at 14:03
  • \$\begingroup\$ this is not uniform, you should take into account the size of those areas \$\endgroup\$
    – Sopel
    Mar 12, 2016 at 14:14
  • \$\begingroup\$ It's giving me an error for the first line: " int where; " it says assignment operator expected. what? \$\endgroup\$ Mar 13, 2016 at 1:36
  • \$\begingroup\$ It still says assignment operator expected for " int positionAlongEdge; " but the error goes away if I put " = " between them (that messes it up though) so what do I do? \$\endgroup\$ Mar 13, 2016 at 2:03
  • \$\begingroup\$ @DeathSwagga, sorry again; I've never actually used game maker. It seems variables are just var typed. You may be forced to initialize it as well. I've edited the code. \$\endgroup\$
    – Jon
    Mar 13, 2016 at 3:22
0
\$\begingroup\$

I would have done more simply:

with obj_player //or in the right place in "step"
{
  var xx=random_range(0,room_width);
  var yy=random_range(0,room_height);
  while point_distance(xx,yy,x,y)<300
  {
    var xx=random_range(0,room_width);
    var yy=random_range(0,room_height);
  }
  instance_create(xx, yy, obj_zom_1);
}
\$\endgroup\$
3
  • \$\begingroup\$ This code made the zombies only appear on screen, which is kinda the opposite to what I was aiming to achieve. \$\endgroup\$ Mar 13, 2016 at 1:34
  • \$\begingroup\$ @DeathSwagga, he meant while distance < 300. This would try again until the point is at least 300 units away, but would still spawn zombies on-screen near the corners. \$\endgroup\$
    – Jon
    Mar 13, 2016 at 3:31
  • \$\begingroup\$ @Jon Thank you very much corrected, I mixed up the sign. Yes, I have proposed the code for the "circle" around the player, and not a square. \$\endgroup\$
    – Kosta
    Mar 13, 2016 at 19:05

You must log in to answer this question.

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