Ios 俄罗斯方块旋转
我还有一个问题。我有SKSpriteNode aktualniBlock,下面是初始化代码:Ios 俄罗斯方块旋转,ios,sprite-kit,Ios,Sprite Kit,我还有一个问题。我有SKSpriteNode aktualniBlock,下面是初始化代码: -(SKSpriteNode *)createBlock { SKSpriteNode *blok = [[SKSpriteNode alloc]init]; SKSpriteNode *square = [SKSpriteNode spriteNodeWithImageNamed:@"Red Square"]; square.position = CGPointMake(45,
-(SKSpriteNode *)createBlock
{
SKSpriteNode *blok = [[SKSpriteNode alloc]init];
SKSpriteNode *square = [SKSpriteNode spriteNodeWithImageNamed:@"Red Square"];
square.position = CGPointMake(45, 0);
[blok addChild:square];
square = [SKSpriteNode spriteNodeWithImageNamed:@"Red Square"];
square.position = CGPointMake(15, 0);
[blok addChild:square];
square = [SKSpriteNode spriteNodeWithImageNamed:@"Red Square"];
square.position = CGPointMake(30, 0);
[blok addChild:square];
square = [SKSpriteNode spriteNodeWithImageNamed:@"Red Square"];
square.position = CGPointMake(0, 0);
[blok addChild:square];
square = [SKSpriteNode spriteNodeWithImageNamed:@"Red Square"];
square.position = CGPointMake(45, 15);
[blok addChild:square];
CGRect rect = [blok calculateAccumulatedFrame];
blok.size = CGSizeMake(rect.size.width, rect.size.height);
return blok;
}
当我点击它时,它会旋转90度:
-(void)tap:(UIGestureRecognizer *)gestureRecognizer
{
[aktualniBlok setZRotation:aktualniBlok.zRotation+1.571];
}
但旋转点不在块的中间,而是在块的左下角。我怎样才能把它弄到街区中间。我尝试了anchorPoint,但无效。谢谢您的帮助。您需要为
blok
节点设置anchorPoint
属性
调用CalculateAccumeratedFrame
后设置此行:
CGRect rect = [blok calculateAccumulatedFrame];
blok.size = CGSizeMake(rect.size.width, rect.size.height);
blok.anchorPoint = CGPointMake(0.5, 0.5);
您可以阅读有关anchorPoint
属性的信息
此外,为了设置旋转,使用
M_PI_2
而不是1.571
将为您提供更精确的值。默认情况下,SKSprites的锚点是其中心点
[aktualniBlok setZRotation:aktualniBlok.zRotation+M_PI_2];
代码执行您需要的操作(更改相对于其中心的旋转),但如果您想要更改每个红方块相对于其中心点的旋转,您应该尝试
[square setZRotation:square.zRotation+M_PI_2];
你的俄罗斯方块没有在左下角旋转。它在(0,0)处围绕块的中心旋转。要围绕工件中心旋转,请从每个块的x位置减去22.5。我修改了你的代码,使工件围绕中心旋转
- (SKSpriteNode *) createBlock
{
SKSpriteNode *blok = [[SKSpriteNode alloc]init];
SKSpriteNode *square;
for (int i=0;i<4;i++) {
CGFloat x = i*15-22.5;
square = [SKSpriteNode spriteNodeWithImageNamed:@"Red Square"];
square.position = CGPointMake(x, 0);
[blok addChild:square];
}
square = [SKSpriteNode spriteNodeWithImageNamed:@"Red Square"];
square.position = CGPointMake(22.5, 15);
[blok addChild:square];
CGRect rect = [blok calculateAccumulatedFrame];
blok.size = CGSizeMake(rect.size.width, rect.size.height);
return blok;
}
-(SKSpriteNode*)创建块
{
SKSpriteNode*blok=[[SKSpriteNode alloc]init];
斯普林泰诺广场;
对于(inti=0;我在哪里设置了aktualniBlok?aktualniBlok=[self createBlock];红色方块的大小是多少?保持零件的锚点为(0,0)我设置了。我设置了锚点(0.5,0.5),但它仍然像锚点(0,0)一样旋转。即使我手动将所有blok的孩子的锚点设置为(0.5,0.5)。我尝试过。不起作用。我将尝试不同的方法,并以“旧”方式旋转。谢谢。您也可以通过相对于中心点设置红色正方形的位置来设置红色正方形的不同位置。