正三角形の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];
}