Cocos2d-knowledge about the genie

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.