Move CCSprite around another CCSprite Circle Shape

送分小仙女□ 提交于 2019-12-25 04:19:30

问题


What I am trying to accomplish is this:

We have CCSprite Circle A and CCSprite Circle B.

Move Circle B around Circle A. I already tried to create a CCNode and attach the circle B to it. In this case it works perfectly but the position is constant also. I need to move the circle around A and update the position. I will have more objects on the screen and I will check if B intersect some other objects, but for that case I need to update the position while rotating. Much appreciate your help guys. I am using Cocos2D v3.0


回答1:


Maybe something like this? Put this in your update method. I used this code in my box2d project for orbit. Change it for your needs.

    b2Vec2 center = bodyA->GetPosition();
    int smoothness = 1000;
    int radius = 100;

    for (int i = 0; i < smoothness; i++) {
        float angle = (i / smoothness) * 360 * DEGTORAD;

        b2Vec2 pos( sinf(angle), cosf(angle));
        b2Vec2 newposition = center + radius * pos;

        bodyB->SetTransform(newposition, bodyB->GetAngle());

 }



回答2:


Put the anchor point of B in the position of the anchor point of A and then rotate the B by 360 in animation.



来源:https://stackoverflow.com/questions/24754969/move-ccsprite-around-another-ccsprite-circle-shape

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!