Author Topic: Priority of dynamic sprites  (Read 5062 times)

Tanja

  • Clever
  • Fanatic
  • ***
  • Posts: 606
    • View Profile
Priority of dynamic sprites
« on: 2008-03-10, 09:56:16 AM »
i added a thundercloud to my game. it's based on durnurds ShootInRange sprite. it works very well, shooting thunderbolts in all directions. only one thing bothers me: the bolts are always created over the cloud. but i want them to appear behind it. can i affect their priority somehow?

durnurd

  • Lead Lemming
  • Expert
  • Fanatic
  • *****
  • Posts: 1234
  • Games completed so far: 0
    • MSN Messenger - durnurd@hotmail.com
    • View Profile
    • Find My Ed
Re: Priority of dynamic sprites
« Reply #1 on: 2008-03-10, 12:58:24 PM »
Unfortunately, dynamic sprites are all drawn after all static sprites.  Several options present themselves:

1. Make the cloud a dynamic sprite.  Every time you add a lightning bolt, delete the cloud and re-add it to the end of the list to be drawn on top
2. Make the lightning bolts static.  Move/Activate a lightning bolt, then deactivate it when it's done.  Look at the sample project to see how it's done with floating "100 points" images for the score.  Then just make sure the priority is set correctly between all the bolts and all the clouds.
3. Move dynamic sprite processing to the beginning in the layer draw function.  (Or something.  Can't look now.  Maybe fiddle with InjectSprites)
Edward Dassmesser

Tanja

  • Clever
  • Fanatic
  • ***
  • Posts: 606
    • View Profile
Re: Priority of dynamic sprites
« Reply #2 on: 2008-03-10, 02:00:44 PM »
whoa, this is cool, it works!
i took nr. 2. the thundercloud ist somewhat cool now. if the player is in range, it takes a little while, then it flashes three times (becomes brighter), and then a bolt is shot. the direction and velocity is random. after one shot it deactivates and adds a new one. so the cloud is over the player and the bolts. cool. i am happy.  :nerd:

durnurd

  • Lead Lemming
  • Expert
  • Fanatic
  • *****
  • Posts: 1234
  • Games completed so far: 0
    • MSN Messenger - durnurd@hotmail.com
    • View Profile
    • Find My Ed
Re: Priority of dynamic sprites
« Reply #3 on: 2008-03-10, 03:17:59 PM »
So did you use the second option, I assume?
Edward Dassmesser

Tanja

  • Clever
  • Fanatic
  • ***
  • Posts: 606
    • View Profile
Re: Priority of dynamic sprites
« Reply #4 on: 2008-03-10, 04:11:37 PM »
yup. oh, no wait.
the first one. (it seemed to difficult to me to manage dozens of static bolts)
i place a placeholder onto the map. at game start these add the real thunderclouds. the clouds add a new cloud after a shot and deactivate themselves then.

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: Priority of dynamic sprites
« Reply #5 on: 2008-03-10, 05:52:22 PM »
Generally, having a static list is better for performance.  Games often try to allocate most/all their objects ahead of time and then never allocate or free memory at runtime.  Also, activating a series of static sprites could be very similar to creating dynamic sprites -- it shouldn't be hard to manage (unless you mean at design time).  Look at TileActivateSprite for an example.  It is very similar to TileAddSprite, but activates the next inactive static sprite instead of creating a new sprite.  You might be able to do something similar with your lightning bolts.

It's no big deal really, but if you use static sprites you have more control over the order of the sprites, and the number that can be activated at once.  You also have more control over other properties of the sprites when they are activated -- it's sometimes hard to set properties a newly added dynamic sprite's properties.