【问题标题】:Creating CCSprites and using them as a rope创建 CCSprite 并将它们用作绳索
【发布时间】:2011-04-26 16:08:18
【问题描述】:

我的屏幕上有一个轮胎,我想将一根绳子连接到轮胎顶部和屏幕顶部。我已经使用花栗鼠+spaceManager 将物理融入到我的游戏中,所以我需要了解这根绳子如何对物理做出反应。我只需要它在被撞击时与轮胎一起来回移动。到目前为止,我已经使用 cpConstraintNode 绘制了一条线以将其用作绳索,但是我所看到和研究的所有内容都无法将 CCSprite 附加到约束。所以我的问题是,我将如何制作这条绳索并让它在移动时的反应与轮胎相同?这是我对约束所做的代码:我正在使用 cocos2d 和花栗鼠 + spaceManger

//The "rope"
        cpVect a1 = cpv(0,30);      //Local coordinates of tire
        cpVect a2 = cpv(70,320);    //World coordinates (staticBody is at (0,0))

        //calculate the length of the rope
        float max = cpvdist(cpBodyLocal2World(upper->body, a1), a2);
        cpConstraint *rope = [game.spaceManager addSlideToBody:upper->body fromBody:game.spaceManager.staticBody toBodyAnchor:a1 fromBodyAnchor:a2 minLength:1 maxLength:max];

        cpConstraintNode *ropeNode = [cpConstraintNode nodeWithConstraint:rope];
        ropeNode.color = ccBLUE;

【问题讨论】:

    标签: cocos2d-iphone chipmunk


    【解决方案1】:

    post 提供了一种使用 Verlet 集成在 Cocos2D 中绘制绳索的好方法。

    唯一的缺点是该示例使用了 Box2D。但是代码可以移植到花栗鼠。

    【讨论】:

      【解决方案2】:

      如果您想使用 github 上的 VRope 项目来集成 VRope 和花栗鼠,我已经创建了一个新的分支来实现这一点。您可以在以下位置找到它:

      VRope Chipmunk Branch

      使用它的一个例子是:

      创作

      pinPointJoint = 
          cpSlideJointNew(body,
                          body2,
                          body.anchorPoint,
                          body2.anchorPoint,
                          minimumLength,
                          maximumLength);
      
      cpSpaceAddConstraint(space, pinPointJoint);
      
      rope = [[VRope alloc] init:pinPointJoint batchNode:ropeBatchNode];
      

      更新

      [rope update:delta];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-03
        • 1970-01-01
        • 2021-03-23
        • 1970-01-01
        • 2014-01-04
        相关资源
        最近更新 更多