z-order for map elements and sprites in cocos2d

Nibbie
Posts: 2
Joined: 2009.04
Post: #1
I used to dabble in game development back in the 90s, when you still entered mode 13h, created a palette of colors and drew pixels directly to the video memory so the answer to the following questions might be a simple "OpenGL doesn't work like that, rtfm" and I'll accept it. I also do a lot of Flash development, so I'm familiar with systems where you have objects and no real control over how or when they're drawn other than setting coordinates and depth. I just want to make sure I understand how this stuff works.
That said, I've started working on my first iPhone game and after looking around a bit decided to use cocos2d.

What I want to do is create a map that is basically one, maybe two layers of tiles which will represent terrain (easy, using the tilemap in cocos2d), and then use sprites for other scenery elements like trees and rocks, etc. The reason I want to use sprites for these instead of tiles is that I want to control them a bit - like pushing rocks, burning trees and stuff like that. Think of something along the lines of A Link to the Past, only with a more interactive and destructible world.

If I was building my own engine to draw the game screen using my old methods, I would draw the ground tiles first and as the other sprites moved around the screen they would be put into linked lists or hash tables representing their y coordinate and drawn from top to bottom, creating a sense of depth. It seems that cocos2d handles all drawing automagically behind the scenes and wants you to change a sprite's depth using the z coordinate.

Is the best way to approach this in cocos2d to sort my sprites on the y coordinate and assign z orders to all of the sprites each frame? This will work, but is it the best solution? It seems like it might be costly compared to the solution I'm used to and not having control over exactly when something gets drawn makes me feel a bit like I'm sitting in the passenger seat.

The problem gets complicated further when I think about implementing something like dynamic waving grass, where I probably don't need actual sprites to represent each blade and would rather draw them procedurally between drawing rows of sprites.

What do you guys think, is there something in cocos2d that I haven't discovered yet, or is using the z order of sprites the way to go? Is there any way with cocos2d to inject some drawing into the process or should I be looking at using opengl directly for something this involved. I will learn opengl if needed, but I'd really rather not re-invent animated sprites, menus, scenes and all the other stuff that cocos2d brings to the table.

Thanks for any input you guys can give, I really appreciate it.
Quote this message in a reply
Nibbie
Posts: 2
Joined: 2009.04
Post: #2
You only need to re-order the z-depth of "nodes" that have an altered Y-position. This isn't too hard and shouldn't be a problem for Cocos2D unless you have a lot of nodes to be re-ordered.

When your sprite is moved enough to warrant a z-depth change, call the [parentNode reorderChild:sprite z:(something based on Y)]. It's costly in that the code is removing and reinserting the sprite in the parentNode's array.

--Leto
Quote this message in a reply
Nibbie
Posts: 2
Joined: 2009.04
Post: #3
That's what I thought, I just wanted to make sure I'm not missing anything. I will follow that path and see how it goes.

Thanks!
Quote this message in a reply
Nibbie
Posts: 2
Joined: 2009.04
Post: #4
Actually, one nice tip I just picked up from the Cocos2d-iphone google group is that if you subclass your sprite/node and use, say, MoveTo actions on it you can override the setPosition() method which is regularly called by the action and perform reordering as needed.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Cocos2d: Reuse sprites oskob 0 3,583 Aug 29, 2009 08:16 AM
Last Post: oskob
  Order of operations in Objective-C if statement johncmurphy 2 3,264 Aug 2, 2009 02:40 PM
Last Post: akb825
  conditionally displaying interface elements aerospaceman 2 2,723 Jun 25, 2009 12:20 PM
Last Post: aerospaceman
  Isometric Alpha Draw Order. superflat 1 2,958 Feb 4, 2009 06:43 AM
Last Post: ThemsAllTook
  Custom menus, buttons and UI elements technocrat9000 3 4,160 Nov 11, 2008 02:49 PM
Last Post: longjumper