以下是代码:
ball = [CCSprite spriteWithFile:@"Ball.png" rect:CGRectMake(0, 0, 52, 52)];
ball.position = ccp(300, 300);
ball.tag = 1;
[self addChild:ball z:-1];
// Create ball body
//b2BodyDef ballBodyDef;
ballBodyDef.type = b2_dynamicBody;
ballBodyDef.position.Set(100/PTM_RATIO, 300/PTM_RATIO);
ballBodyDef.userData = ball;
b2Body * ballBody = _world->CreateBody(&ballBodyDef);
// Create circle shape
b2CircleShape circle;
circle.m_radius = 20.0/PTM_RATIO;
// Create shape definition and add to body
b2FixtureDef ballShapeDef;
ballShapeDef.shape = &circle;
ballShapeDef.density = -2.5f;
ballShapeDef.friction = 0.01f;
ballShapeDef.restitution = 1.0f;
_ballFixture = ballBody->CreateFixture(&ballShapeDef);
b2Vec2 force = b2Vec2(5, 7);
ballBody->ApplyLinearImpulse(force, ballBodyDef.position);
我想旋转,动画和移动球,但我很困惑,如何同时应用所有的东西!?
答案 0 :(得分:1)
例如,如何将box2d位置和旋转应用于CCNode,
// Position
b2Vec2 b2pos = ballBody->GetPosition();
CGPoint position = ccpMult(CGPointMake(b2pos.x, b2pos.y), PTM_RATIO);
ball.position = position;
// Rotation
float angle = ballBody->GetAngle();
ball.rotation = -(CC_RADIANS_TO_DEGREES(angle));
顺便说一句,CCBox2D对cocos2d和Box2d很有帮助。
答案 1 :(得分:1)
使用Spawn动作同时播放多个动作。 id spawAction = [CCSpawn actions:moveAction,rotateAction,nil]; [aSprite runAction:spawAction];