cocos2d(CCSprite绑定不规则刚体与精灵一起移动)(2)
<喎?http://www.2cto.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+Mi6z9cq8u6++q8Hpus241czlvLC80L7fPC9wPgo8cD48cHJlIGNsYXNzPQ=="brush:java;">#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; } } }
效果图:
相关新闻>>
- 发表评论
-
- 最新评论 更多>>