Cloud system recommendations?


(Nate ) #1

Hey guys! I set up a nice little cloud system for my game earlier today, and I was just wondering if I could get some constructive criticism, and if you think this method would be good for a final game, or if there are much better more efficient ways of doing so.

I will list all the code I have created for my scrolling clouds.

The Cloud.as file:

package entities {

import net.flashpunk.Entity;
import net.flashpunk.graphics.Image;
import net.flashpunk.FP;

public class theCloud extends Entity {
	
	[Embed(source = '../assets/cloud.png')]
	
	protected const CLOUD:Class;
	
	public function theCloud(posX:int,posY:int) {
		graphic=new Image(CLOUD);
		setHitbox(80,120);
		type="cloud"; 
		x=posX*40;
		y = posY * 40;
		
		//graphic.scrollX = .5;
		
		
		
	}
	override public function update():void
	{
		x -= FP.elapsed * 20;
		
		trace("x value of the cloud imave: " + x);
		if (x <= -125)
		{
			x = 2560;
		}
		
		
	}
}}

In the World.as

for (var t:int = 0; t < 10; t++)
		{
		add(new theCloud(FP.rand(64), FP.rand(7)+1));  
		}

Like I said everything runs fine, I was just wondering if there was a better way of doing this. Thanks guys! I tried making it a backdrop, but that went horribly wrong and I had a nice LARGE never ending cloud scrolling… lol


(Zachary Lewis) #2

Instead of using add(), I’d suggest using create(). Additionally, when a cloud goes offscreen, it’s a good idea to get rid of it. You can use

world.recycle(this);

inside of theCloud once it is offscreen to get rid of it (so it doesn’t drift on forever and ever).

Other than that, if it looks good, rock on!


(Nate ) #3

Okay I will look into the create method.

The reason I am not removing/recycling the clouds is because I am basically spawning 10 or 11 of them, at anywhere randomly on my X axis, then if then drift off out of sight, which they are 120 pixels in width, so to be safe I used -125 in my code. But once they are not visible, I send their X value to the opposite side of the screen, so they start all over again drifting to the left.

Do you think this is going to be resource consuming to the point where I should write it differently?

Thanks Zach for your input! :smiley:


(Joseph Sweeney) #4

The method you suggest wouldn’t take any more resources, but the code would probably be cleaner if you create and recycle each cloud. Plus, by treating each cloud as a new, unique cloud, you can implement other effects more easily - for example, changing y position, cycling cloud attributes based on time on a per-cloud basis, or spawning “special” clouds to use interactively would all be much easier to add down the road using create / recycle.


(reopucino) #5

@zachwlewis what different from create and add??? or recyle and remove??? did create and remove good for garbage collection??


(Zachary Lewis) #6

Recycled Entities

For an Entity that will be created and disposed often, recycling is the way to go. It reuses an old Entity of the same class (if it exists) so more memory doesn’t need to be allocated.

  • create() — Returns a new Entity, or a stored recycled Entity if one exists.
  • recycle() — Removes the Entity from the World at the end of the frame and recycles it. The recycled Entity can then be fetched again by calling the create() function.

Disposed Entities

For an Entity that isn’t created often and needs specialized constructor parameters (like loading a map), traditional entity adding and destroying is the way to go.

  • add() — Adds the Entity to the World at the end of the frame.
  • remove() — Removes the Entity from the World at the end of the frame.

(Ultima2876) #7

I prefer recycling in all cases, as you can very easily add a setup(blah: Number, blah2: String) function instead of using the constructor to pass in arguments. Even maps have to allocate memory each time they’re created, so you still get a small benefit from recycling them!

The only exception is if your game is insane and using over 500mb of memory. In that case, you will have to economise somewhere. That said, if you only ever have one of our example maps in existence at a time, recycling it wouldn’t actually do any harm either - plus it’s very good practice to get into the habit of recycling EVERYTHING :smiley: