1. 程式人生 > >cocos2d(CCSprite繫結不規則剛體與精靈一起移動)

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;
            }
        }
    }

效果圖: