Create Objects Only When They Show Up In Camera


(Zouhair Elamrani Abou Elassad) #1

Hi again, i’ve been working on my FlashPunk game for days now, i have created multiple objects, the idea is that i have a player (a bird) and i have to go through different levels that contains other objects (Animated Enemies, Rocks … etc), i was thinking while the player is the only object i’m moving with my keyboard and mouse, the other objects are being created and waiting to show up in stage (When the camera arrives), i was wondering is there a way to not create object unless they show up in camera or something like that !


The Best Way To Handle A Game With Multiple Animated Objects!
(Darrin) #2

If you take a look at my Subby and the Fishes Game. Everything is moving but the player. (Technically it is moving up and down but pretend it is stationary). Everything else is moving from right to left, including fishes, rocks. buoy, etc. Typically you want to create objects off screen then have them move onto the screen. So in the case of Subby, I create everything at about x=850, then have them move -X every frame. Once they get past -0 +image.width I remove them.

http://www.playfulminds.com/subby.html

If you are doing a big map, larger than the screen size, then you want to create everything on your map and just have the camera follow the player. Put something like this in your player.update(). This is a click to move.

		override public function update():void
	{
	
		//bounds check
		if (x < 1) {x = 1;}
		if (y < 1) { y = 1; }
		if (x  > 1024-width) { x = 1024 - width; }
		if (y > 768 - height) { y = 768 - height; }			
	
		// mouse movement
		if (Input.mousePressed)
		{
			bMoving = true;
			point.x = Input.mouseX + FP.camera.x;;
			point.y = Input.mouseY + FP.camera.y;;
			
		}
		
		if (bMoving) {
			moveTowards(point.x, point.y, speed, null, false);				
			
			// The camera follows player. halfWidth adjust for the sprite  

			FP.camera.x = this.x - FP.halfWidth + halfWidth;
			FP.camera.y = this.y - FP.halfHeight + halfHeight;
			
		}


	}

(Zouhair Elamrani Abou Elassad) #3

I see, what i’m doing is that i’m moving the camera with the player, i guess i simply have to create the objects when the camera arrives at a certain position