我想同时移动、动画和旋转对象

发布于 2024-11-17 05:35:33 字数 1055 浏览 4 评论 0原文

这是代码:

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

我想要旋转、动画和移动球,但我很困惑,如何同时应用所有东西!?

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
        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!?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

删除→记忆 2024-11-24 05:35:33

使用 Spawn 动作同时进行多个动作。
id spawAction = [CCSpawn 动作:moveAction、rotateAction、nil];
[aSprite runAction:spawAction];

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

氛圍 2024-11-24 05:35:33

例如,如何将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和盒子2d。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文