Jump to content


Photo

Making objects randomly spawn


  • Please log in to reply
3 replies to this topic

#1 Fratrino

Fratrino

    GMC Member

  • GMC Member
  • 136 posts
  • Version:Unknown

Posted 17 July 2011 - 01:03 AM

I'm making a game where your army full out charges an enemy trench, how can I create an object that spawns other objects from random positions behind the spawner?
  • 0

#2 epicCreations

epicCreations

    Cool As Ace

  • New Member
  • 1241 posts

Posted 17 July 2011 - 01:25 AM

var chance; chance = 2; //probability is 1/chance
if (!floor(random(chance)) //if it happened
{
     var amt_min,amt_max; //minimum and maximum to spawn
     amt_min = 3;
     amt_max = 10;
     amt = irandom_range(amt_min,amt_max); //amount it will spawn
     for (i=amt_min;i<amt;i+=1)
          instance_create(x-16,y,obj_enemy); //will spawn 16 pixels left of spawner
}

That should work. I don't know the viewpoint though so I'm unsure of how it is supposed to spawn
  • 0

#3 Unsung Hero

Unsung Hero

    GMC Member

  • New Member
  • 286 posts

Posted 17 July 2011 - 01:37 AM

Err...I'm not sure if this'll work but...

Oh yeah, I should explain something first.
The spawner object is the object that creates the other objects.
The spawning object is the object that is being created.
//In create event; this is just to initialize the variable
ID=0
//In the step event
//# stands for any number you decide you want; I usually use 30 or 40 if it occurs occasionally
//obj_1 is just the name of the object you want spawned; replace with the name of the spawning object
if floor(random(#))=0
ID=instance_create(random(room_width), random(room_height), obj_1)

To prevent it from spawning in front of the spawner we'll use this:
//In create event of the spawning object
//If you don't want it to spawn to the right of the spawner use this
if x>obj_spawner.x
instance_destroy()
//If you don't want it to spawn to the left of the spawner use this
if x<obj_spawner.x
instance_destroy()

Edited by Unsung Hero, 17 July 2011 - 01:39 AM.

  • 0

#4 Fratrino

Fratrino

    GMC Member

  • GMC Member
  • 136 posts
  • Version:Unknown

Posted 17 July 2011 - 05:30 AM

i need them to spawn in random places behind the spawner, not directly from the spawner

thanks unsung

Edited by Fratrino, 17 July 2011 - 05:40 AM.

  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users