• cocos2d(CCSprite绑定不规则刚体与精灵一起移动)


    对于不规则的精灵我们可以借助PhysicsEditor来制作shape ,

    对于地图可以使用Tiled软件制作瓷砖地图。

    今天主要记录一下如何把CCSprite与不规则刚体进行绑定,然后一起移动

    //初始化玩家

    1.加载shape文件,在init方法中添加:

    //加载shape文件
            [[GB2ShapeCache sharedShapeCache] addShapesWithFile:@"physicShape.plist"];
    .plist文件内容大体如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    
    <!-- created with http://www.physicseditor.de -->
    
    <plist version="1.0">
    	<dict>
            <key>metadata</key>
            <dict>
                <key>format</key>
                <integer>1</integer>
                <key>ptm_ratio</key>
                <real>32</real>
            </dict>
            <key>bodies</key>
    		<dict>
    
    			<key>Player1</key>
    			<dict>
    				<key>anchorpoint</key>
                    <string>{ 0.0000,0.0000 }</string>
    				<key>fixtures</key>
    				<array>
    
    					<dict>
                            <key>density</key> <real>2</real>
                            <key>friction</key> <real>0</real>
                            <key>restitution</key> <real>0</real>
                            <key>filter_categoryBits</key> <integer>1</integer>
                            <key>filter_groupIndex</key> <integer>0</integer>
                            <key>filter_maskBits</key> <integer>65535</integer>
                            <key>isSensor</key> <false/>
                            <key>id</key> <string></string>
                            <key>fixture_type</key> <string>POLYGON</string>
    
    						<key>polygons</key>
    						<array>
    							<array>	
    								<string>{ 1.000,0.000 }</string>	
    								<string>{ 32.000,0.000 }</string>	
    								<string>{ 27.000,32.000 }</string>	
    								<string>{ 7.000,32.000 }</string>
    							</array>
    						</array>
                        </dict>
    				</array>
    			</dict>
    			<key>RetroCoin</key>
    			<dict>
    				<key>anchorpoint</key>
                    <string>{ 0.0000,0.0000 }</string>
    				<key>fixtures</key>
    				<array>
    
    					<dict>
                            <key>density</key> <real>2</real>
                            <key>friction</key> <real>0</real>
                            <key>restitution</key> <real>0</real>
                            <key>filter_categoryBits</key> <integer>1</integer>
                            <key>filter_groupIndex</key> <integer>0</integer>
                            <key>filter_maskBits</key> <integer>65535</integer>
                            <key>isSensor</key> <false/>
                            <key>id</key> <string></string>
                            <key>fixture_type</key> <string>POLYGON</string>
    
    						<key>polygons</key>
    						<array>
    							<array>	
    								<string>{ 4.000,3.000 }</string>	
    								<string>{ 17.000,0.000 }</string>	
    								<string>{ 28.000,3.000 }</string>	
    								<string>{ 28.000,28.000 }</string>	
    								<string>{ 16.000,32.000 }</string>	
    								<string>{ 4.000,28.000 }</string>
    							</array>
    						</array>
                        </dict>
    				</array>
    			</dict>
    		</dict>
    	</dict>
    </plist>
    




    2.初始化精灵和刚体及夹具

    #pragma mark 初始化玩家
    -(void)initPlayer{
        //创建对象层 获得对象的产生点
        CCTMXObjectGroup *objects=[_gameMap objectGroupNamed:@"objects"];
        NSMutableDictionary *spawnPoint=[objects objectNamed:@"StartPoint"];
         //获得出生点坐标
        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;   //设置描点为0,0  否则与刚体不能重合
        b2BodyDef playerBodyDef;           //定义刚体结构体的定义
        [self addChild:_player];
        
        playerBodyDef.type=b2_dynamicBody;
        playerBodyDef.fixedRotation=true;   //不会旋转
        playerBodyDef.position.Set(x/PTM_RATIO, y/PTM_RATIO);
        
        _playBody=world->CreateBody(&playerBodyDef);
        _playBody->SetUserData(_player);
        [[GB2ShapeCache sharedShapeCache] addFixturesToBody:_playBody forShapeName:@"Player1"];  //为刚体设置夹具
       
    }
    

    3.在update函数中添加更新位置方法

    for(b2Body *b = world->GetBodyList(); b; b=b->GetNext()) {
            
            
            if (b->GetUserData() != NULL) {
                CCSprite *sprite = (CCSprite *)b->GetUserData(); //获得精灵对象
                if (sprite != nil) {
                    b2Vec2 bodyPos = b->GetPosition();  //获得刚体的位置
                    CGPoint pos = CGPointMake(bodyPos.x * PTM_RATIO, bodyPos.y * PTM_RATIO);  //变换为坐标
                    float32 rotation = -1 * CC_RADIANS_TO_DEGREES(b->GetAngle());
                    sprite.position = pos;
                    sprite.rotation = rotation;
                }
            }
        }
    

    效果图:



    转载请注明:版权所有http://1.wildcat.sinaapp.com/



  • 相关阅读:
    程序员修炼之道:从小工到专家
    2020.12.16收获
    2020.12.15收获
    2020.12.14收获
    2020.12.13收获
    Android学习第二天——对Android的简单了解
    Java学习12.18
    考试加分项
    Java学习12.17
    Java建议
  • 原文地址:https://www.cnblogs.com/lixingle/p/3707688.html
Copyright © 2020-2023  润新知