Performance Considerations
This section is a summary of cocos2d online documentation.
1) if there are less than 25 elves in each frame to be updated, you can directly use the genie
[Python]
Class TLayer (cocos. layer. Layer ):
Is_event_handler = True
Def _ init _ (self ):
Cocos. layer. Layer. _ init _ (self)
World_width, world_height = director. get_window_size ()
Rand_color = [255, 0, 0]
Icolor = 0
For I in range (qty_bils ):
Ball = Ball (world_width * random. random (),
World_height * random. random ()),
Color = rand_color
)
Rand_color [icolor] = random. randint (50,255)
Icolor = (icolor + 1) % len (rand_color)
Self. add (ball)
Self. time = 0.0
Self. schedule (self. update)
Class TLayer (cocos. layer. Layer ):
Is_event_handler = True
Def _ init _ (self ):
Cocos. layer. Layer. _ init _ (self)
World_width, world_height = director. get_window_size ()
Rand_color = [255, 0, 0]
Icolor = 0
For I in range (qty_bils ):
Ball = Ball (world_width * random. random (),
World_height * random. random ()),
Color = rand_color
)
Rand_color [icolor] = random. randint (50,255)
Icolor = (icolor + 1) % len (rand_color)
Self. add (ball)
Self. time = 0.0
Self. schedule (self. update)
2) if there are less than 25-elves in each frame to be updated, add them to the BatchNode first.
[Python]
Class TLayer (cocos. layer. Layer ):
Is_event_handler = True
Def _ init _ (self ):
Cocos. layer. Layer. _ init _ (self)
Batch = cocos. batch. BatchNode ()
Self. add (batch) # we add a batch to the layer...
World_width, world_height = director. get_window_size ()
Rand_color = [255, 0, 0]
Icolor = 0
For I in range (100 ):
Ball = Ball (world_width * random. random (),
World_height * random. random ()),
Color = rand_color
)
Rand_color [icolor] = random. randint (50,255)
Icolor = (icolor + 1) % len (rand_color)
Batch. add (ball) # see? We add sprites to the batch, not the layer
Self. batch = batch
Self. time = 0.0
Self. schedule (self. update)
Class TLayer (cocos. layer. Layer ):
Is_event_handler = True
Def _ init _ (self ):
Cocos. layer. Layer. _ init _ (self)
Batch = cocos. batch. BatchNode ()
Self. add (batch) # we add a batch to the layer...
World_width, world_height = director. get_window_size ()
Rand_color = [255, 0, 0]
Icolor = 0
For I in range (100 ):
Ball = Ball (world_width * random. random (),
World_height * random. random ()),
Color = rand_color
)
Rand_color [icolor] = random. randint (50,255)
Icolor = (icolor + 1) % len (rand_color)
Batch. add (ball) # see? We add sprites to the batch, not the layer
Self. batch = batch
Self. time = 0.0
Self. schedule (self. update)
3) If more than 100 genie need to be updated for each frame, further optimization needs to be considered. You can use the APIS provided in pyglet for optimization.
For a ballpark data point, a 2008 low gamming spec machine (athon 5200, radeon 4650) gives:
Numbils fps
250 98
500 54
750 36
Collision Detection
If you want Sprite to have a collision detection function, you must meet two conditions.
1) You must have a cshape member in the class.
2) cshape must be an instance of cocos. collision_model.CircleShape or cocos. collision_model.w.ectshape.
[Python]
Import cocos. euclid as eu
Import cocos. collision_model as cm
Class CollidableSprite (cocos. sprite. Sprite ):
Def _ init _ (self, image, center_x, center_y, radius ):
Super (ActorSprite, self). _ init _ (image)
Self. position = (center_x, center_y)
Self. cshape = cm. CircleShape (eu. Vector2 (center_x, center_y), radius)
Class ActorModel (object ):
Def _ init _ (self, cx, cy, radius ):
Self. cshape = cm. CircleShape (eu. Vector2 (center_x, center_y), radius)