开发者

I want to move,animate and rotate object at the same time

Here is the code:

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
开发者_JAVA技巧        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);

I want to rotate,animate and move ball but i am confused,How to apply all things at the same time!?


Using Spawn action to play many action at the same time. id spawAction = [CCSpawn actions:moveAction, rotateAction, nil]; [aSprite runAction:spawAction];


For example, how to apply box2d position and rotation to 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));

By the way, CCBox2D is helpful with cocos2d and Box2d.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜