【问题标题】:How do you make a static sprite be a child of another sprite in cocos2D while using SpaceManager使用 SpaceManager 时如何使静态精灵成为 cocos2D 中另一个精灵的子对象
【发布时间】:2010-04-22 15:49:10
【问题描述】:

我有两个静态 (STATIC_MASS) SpaceManager 精灵。一个是另一个的孩子 - 我的意思是一种建立另一种,但是虽然孩子的图像出现在正确的位置,但孩子似乎并不存在于花栗鼠物理引擎中,就像我一样会期望。就我而言,我有一个篮板(矩形精灵)和一个箍(圆形精灵)。因为我可能想移动篮板,所以我想将篮筐连接到篮板上,以便篮筐自动与篮板一起移动。

在这里,我们看到一个带有箍的旋转篮板。它在屏幕上看起来不错,但其他物体只会从篮板上反弹,但会直接穿过篮筐(从术语上讲是不好的)。物理引擎中似乎不存在什么我的孩子精灵?

  // Add Backboard
  cpShape *shapeRect = [smgr addRectAt:cpvWinCenter mass:STATIC_MASS width:200 height:10 rotation:0.0f ];// We're upgrading this 
  cpCCSprite * cccrsRect = [cpCCSprite spriteWithShape:shapeRect file:@"rect_200x10.png"];
  [self addChild:cccrsRect];

  // Spin the static backboard: http://stackoverflow.com/questions/2691589/how-do-you-make-a-sprite-rotate-in-cocos2d-while-using-spacemanager
  // Make static object update moves in chipmunk
  // Since Backboard is static, and since we're going to move it, it needs to know about spacemanager so its position gets updated inside chipmunk.
  // Setting this would make the smgr recalculate all static shapes positions every step
  //    cccrsRect.integrationDt = smgr.constantDt; 
  //    cccrsRect.spaceManager = smgr;
  // Alternative method: smgr.rehashStaticEveryStep = YES;
  smgr.rehashStaticEveryStep = YES;

  // Spin the backboard
  [cccrsRect runAction:[CCRepeatForever actionWithAction:
                         [CCSequence actions:
                          [CCRotateTo actionWithDuration:2 angle:180],
                          [CCRotateTo actionWithDuration:2 angle:360],
                          nil]

                         ]];


  // Add the hoop
  cpShape *shapeHoop = [smgr addCircleAt:ccp(100,-45) mass:STATIC_MASS radius: 50 ];
  cpCCSprite * cccrsHoop = [cpCCSprite spriteWithShape:shapeHoop file:@"hoop_100x100.png"];
  [cccrsRect addChild:cccrsHoop];

上面的代码有点乱。箍的图像显示在板子旁边,这就是我想要的,如果我检测到碰撞,碰撞只发生在屏幕的左下角。奇怪的是,即使我检测到碰撞,我的对象实际上似乎并没有发生碰撞,它只是穿过它而不是从它反弹。

注意:SpaceManager 是一个使用 cocos2D-iphone 的工具包

【问题讨论】:

  • 标签说明:这应该有一个'spacemanager'标签

标签: iphone cocos2d-iphone chipmunk


【解决方案1】:

问题是父子关系在物理引擎中没有意义。您可以将多个形状附加到同一个主体,这样当您移动一个主体时,它会移动多个形状。

【讨论】:

    【解决方案2】:

    对此的简单回答是,这实际上是不可能的。花栗鼠喜欢一切都在世界坐标中,当你开始在 cocos2d 中创建父子关系时,你正在设置坐标系的严重不匹配......

    但是...我确实尝试过解决这个问题但没有取得多大成功(我相信问题是旋转)查看 SpaceManager.m 中的函数“eachShapeAsChildren”。如果你设置 SpaceManager 的 iterateFunc 属性指向这个函数,你可以看到会发生什么......

    在某个时候,我可能会再试一次;事实上,我已经感觉更有灵感了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多