Looping Clouds – spawning, destroying, adjusting variables

F

Foss

Guest
Hey! I'd like to have cloud shadows that move across the screen, destroying themselves once they move off screen, and then re-spawning.

I have obj_cloud where I define the properties:

image_speed = 0;
image_index = irandom(3);
image_alpha = 0.1 * random(0.3);
image_xscale = 1 + random(0.2);
image_yscale = 1 + random(0.2);
speed = 0.1 + random(10);
direction = 360;
Then I have a separate obj_game with the Create event:

number_of_clouds = 0
maximum_clouds = 10
… and the Step event:

//Spawn a cloud in a random position off-screen
if number_of_clouds < maximum_clouds {
instance_create_layer(-500, 0 + random(1000), "layer_cloud", obj_cloud);
number_of_clouds += 1;
}


//Destroy a cloud if it moves off screen
with (obj_cloud) {
if x>room_width+600 {
instance_destroy();
number_of_clouds -= 1;
}
}​

The code is obviously erroneous. That line, 'number_of_clouds -=1' seems to be what's breaking it. In my head, this should subtract 1 from the 'number_of_clouds' variable so that the spawn code can create another cloud.

Without that line, the clouds spawn and destroy themselves correctly. Just need to get the process repeating!
 

Electros

Member
Perhaps try handling in the cloud step event:

Code:
if x>room_width+600 {
with (obj_game) {
number_of_clouds -= 1;
}
instance_destroy();
}
Particle systems are pretty good at handling these kinds of things also!
 

JackTurbo

Member
Rather than making each cloud shadow an object, you'd probably better using a single object and just drawing sprites for each cloud in that.
 
F

Foss

Guest
It … It works. Perfectly. Wow, thanks!

So what was wrong with my conception/formulation of the code? I guess it's because I was using the 'with' statement to refer to the separate obj_cloud, but hadn't set the 'number_of_clouds' variable within that object?

Yeah, I considered using particles. But I've had performance trouble with cloud particles in the past. Also, I think the designers I work with will want to use something more stylised (and perhaps animated!)

Thanks again for this!
 
F

Foss

Guest
Rather than making each cloud shadow an object, you'd probably better using a single object and just drawing sprites for each cloud in that.
That'd probably be neater too, code-wise, wouldn't it? Then I'd be able to keep everything within one object. Thanks to Electros, I've got it working! But I might try and clean the code up anyway, just as a matter of getting my brain better used to programming.
 

JackTurbo

Member
My personal rule of thumb is: if a sprite doesn't need to interact with anything, then I try not to make it an object.

Realistically what you've got is fine though.
 

Electros

Member
Yeah, it would error as the variable number_of_clouds was not present within obj_cloud - but you wouldn't want it contained there anyway, as you rightly wanted to control that from a single position (obj_game).

Don't forget, with(obj_cloud) will influence all active instances of obj_cloud, so any following code in brackets would impact all your clouds.
 
F

Foss

Guest
Yeah, it would error as the variable number_of_clouds was not present within obj_cloud - but you wouldn't want it contained there anyway, as you rightly wanted to control that from a single position (obj_game).

Don't forget, with(obj_cloud) will influence all active instances of obj_cloud, so any following code in brackets would impact all your clouds.
Got it. I think. I clearly need to practice this kind of thing more!
 
Top