Cocos2d (CCSprite binds irregular rigid bodies to move with the genie)

Source: Internet
Author: User

For irregular genie, we can use PhysicsEditor to create shape,

You can use Tiled software to create tile maps for maps.

Today, we mainly record how to bind CCSprite to an irregular Rigid Body and move it together.

// Initialize the player

1. Load the shape file and add the following in the init method:

// Load the shape file [[GB2ShapeCache sharedShapeCache] addShapesWithFile: @ "physicShape. plist"];
The content of the. plist file is as follows:

 
 
 
          
   
    metadata
           
               
    
     format
                
    
     1
                
    
     ptm_ratio
                
    
     32
            
           
   
    bodies
   
   
    
     Player1
    
    
     
      anchorpoint
                     
     
      { 0.0000,0.0000 }
     
     
      fixtures
     
                             
      
       density
       
      
       2
                              
      
       friction
       
      
       0
                              
      
       restitution
       
      
       0
                              
      
       filter_categoryBits
       
      
       1
                              
      
       filter_groupIndex
       
      
       0
                              
      
       filter_maskBits
       
      
       65535
                              
      
       isSensor
       
                              
      
       id
       
                              
      
       fixture_type
       
      
       POLYGON
      
      
       polygons
      
      
       { 1.000,0.000 }
      
      
       { 32.000,0.000 }
      
      
       { 27.000,32.000 }
      
      
       { 7.000,32.000 }
                          
     
    
    
     RetroCoin
    
    
     
      anchorpoint
                     
     
      { 0.0000,0.0000 }
     
     
      fixtures
     
                             
      
       density
       
      
       2
                              
      
       friction
       
      
       0
                              
      
       restitution
       
      
       0
                              
      
       filter_categoryBits
       
      
       1
                              
      
       filter_groupIndex
       
      
       0
                              
      
       filter_maskBits
       
      
       65535
                              
      
       isSensor
       
                              
      
       id
       
                              
      
       fixture_type
       
      
       POLYGON
      
      
       polygons
      
      
       { 4.000,3.000 }
      
      
       { 17.000,0.000 }
      
      
       { 28.000,3.000 }
      
      
       { 28.000,28.000 }
      
      
       { 16.000,32.000 }
      
      
       { 4.000,28.000 }
                          
     
    
   
  
 




<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + Mi6z9cq8u6 ++ q8Hpus241czlvLC80L7fPC9wPgo8cD48cHJlIGNsYXNzPQ = "brush: java;" ># pragma mark initialize player-(void) initPlayer {// create an object Layer to obtain the object generation point: CCTMXObjectGroup * objects = [_ gameMap objectGroupNamed: @ "objects"]; NSMutableDictionary * spawnPoint = [objects objectNamed: @ "StartPoint"]; // obtain the birthpoint coordinate float x = [[spawnPoint valueForKey: @ "x"] floatValue]; float y = [[spawnPoint valueForKey: @ "y"] floatValue]; _ player = [CCSprite spriteWithFile: @ "Player1.png"]; _ player. position = ccp (x, y); _ player. anchorPoint = CGPointZero; // set the stroke to 0 or 0; otherwise, it cannot overlap with the rigid body. b2BodyDef playerBodyDef; // defines the definition of the rigid body structure [self addChild: _ player]; playerBodyDef. type = b2_dynamicBody; playerBodyDef. fixedRotation = true; // playerBodyDef is not rotated. position. set (x/PTM_RATIO, y/PTM_RATIO); _ playBody = world-> CreateBody (& playerBodyDef); _ playBody-> SetUserData (_ player); [[GB2ShapeCache sharedShapeCache] addFixturesToBody: _ playBody forShapeName: @ "Player1"]; // sets a fixture for a rigid body}
3. Add the update position method to the update function.

For (b2Body * B = world-> GetBodyList (); B = B-> GetNext () {if (B-> GetUserData ()! = NULL) {CCSprite * sprite = (CCSprite *) B-> GetUserData (); // get the sprite object if (sprite! = Nil) {b2Vec2 bodyPos = B-> GetPosition (); // obtain the position CGPoint pos = CGPointMake (bodyPos. x * PTM_RATIO, bodyPos. y * PTM_RATIO); // converts it to the coordinate float32 rotation =-1 * CC_RADIANS_TO_DEGREES (B-> GetAngle (); sprite. position = pos; sprite. rotation = rotation ;}}}

:







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.