1

我有一个等边三角形的 CCSprite。我想以 60 度的增量旋转三角形以保持其位置。

精灵是 126x110(不是正方形),将精灵旋转属性设置为 60 度增量会改变精灵的位置。我怎样才能让精灵在每次旋转时保持静止?

更多关于这个矩形的中心不是精灵的中心。因此,当需要旋转以使三角形的中心保持居中时,需要进行一些调整。

我想我想出了一个需要批准的长答案..

    // collect points.
    self.point1 = CGPointMake(CGRectGetWidth(self.tile.boundingBox)/2, 0);
    self.point2 = CGPointMake(CGRectGetWidth(self.tile.boundingBox), CGRectGetHeight(self.tile.boundingBox));
    self.point3 = CGPointMake(0, CGRectGetHeight(self.tile.boundingBox));

    // calculcate the mid point.
    float midPointX = floor((self.point1.x + self.point2.x + self.point3.x)/3.0);
    float midPointY = floor((self.point1.y + self.point2.y + self.point3.y)/3.0);

   // stash the center of the triangle
    self.triangleCenter = CGPointMake(midPointX, midPointY);

然后根据旋转找出中心点的新位置。并在那里设置动画。(对不起,屏幕中心是硬编码的,这是一个粗略的测试)。

-(void) rotateToAngleAboutCenter:(float)angle {

        // stash old values.
        CGPoint oldPosition = self.tile.position;
        float oldRotation = self.tile.rotation;

        // reset the rotation
       self.tile.rotation = 0;

       // this is hard coded here currently the center of the screen.
       self.tile.position =  ccp(512, 384);

       // figure out where our center point will be when we are rotated about the center.
       CGPoint point = ccpRotateByAngle(self.triangleCenter, [self.tile anchorPointInPoints],-CC_DEGREES_TO_RADIANS(angle));

      // convert the ppoint to local space.
      point = [self.tile convertToWorldSpace:point];

      point = [self convertToNodeSpace:point];

      // reset the rotation and position.
      self.tile.rotation = oldRotation;
      self.tile.position = oldPosition;

      // animate to new rotation/position.
      CCMoveTo* moveTo = [CCMoveTo actionWithDuration:.25 position:point];
      CCRotateTo* rotateTo = [CCRotateTo actionWithDuration:.25 angle:angle];

      [self.tile runAction:moveTo];
      [self.tile runAction:rotateTo];
}
4

0 回答 0