我正在阅读Apple的SpriteKit文档,并且遇到了一个非常有用的功能,我可以在编写UI时使用它。问题是我无法让它发挥作用。
请参阅此页并向下滚动到“调整精灵大小” - Apple Docs
我已经完全复制了图像尺寸并使用相同的代码,因为我做错了什么。但我总是看到一个看起来很紧张的图像而不是正确的“端盖”保持相同的比例。
我指的是这段代码:
SKSpriteNode *button = [SKSpriteNode spriteWithImageNamed:@"stretchable_button.png"];
button.centerRect = CGRectMake(12.0/28.0,12.0/28.0,4.0/28.0,4.0/28.0);
我做错了什么?有没有我错过的一步?
编辑:
这是我一直在使用的代码。我删除了我的按钮类并尝试将其与SKSPriteNode一起使用,但问题仍然存在。我也改变了图像,以确保它不是那样的。使用的图像是正常尺寸的32x32。
SKSpriteNode *button = [SKSpriteNode spriteNodeWithImageNamed:@"Button.png"];
[self addChild:button];
button.position = ccp(200, 200);
button.size = CGSizeMake(128, 64);
button.centerRect = CGRectMake(9/32, 9/32, 14/32, 14/32);
答案 0 :(得分:10)
如果调整sprite .scale属性,则.centerRect属性的工作方式如下所示。
尝试:
SKTexture *texture = [SKTexture textureWithImageNamed:@"Button.png"];
SKSpriteNode *button = [[SKSpriteNode alloc] initWithTexture:texture];
button.centerRect = CGRectMake(9/32, 9/32, 14/32, 14/32);
[self addChild:button];
button.xScale = 128.0/texture.size.width;
button.yScale = 64.0/texture.size.height;
答案 1 :(得分:2)
9/32
是整数除法,因此传递给CGRectMake
的结果为零。同样是其他三个参数。如果您使用浮点文字(如您引用的示例),则可能会获得更好的结果。
答案 2 :(得分:2)
这是一个如何运作的更新。顺便说一句,我的图像大小宽度是48像素,高度是52像素,但这根本不重要。可以使用任何图像:
SKSpriteNode *button = [SKSpriteNode spriteNodeWithImageNamed:@"Button.png"];
//(x, y, width, height). First two values are the four corners of the image that you DON'T want touched/resized (They will just be moved).
//The second two values represent how much of images width & height you want cut out & used as stretching material. Cut out happens from the center of the image.
button.centerRect = CGRectMake(20/button1.frame.size.width, 20/button1.frame.size.height, 5/button1.frame.size.width, 15/button1.frame.size.height);
button.position = CGPointMake(self.frame.size.width/2, self.frame.size.height/2); //Positions sprite in the middle of the screen.
button.xScale = 4; //Resizes width (This is all I needed).
//button.yScale = 2; //Resizes height (Commented out because I didn't need this. You can uncomment if the button needs to be higher).
[self addChild:button];
请阅读本文档中的“调整精灵大小”一节:https://developer.apple.com/library/ios/documentation/GraphicsAnimation/Conceptual/SpriteKit_PG/Sprites/Sprites.html#//apple_ref/doc/uid/TP40013043-CH9-SW10
'图2-4可拉伸的按钮纹理'演示了(x,y,width,height)的工作原理。
答案 3 :(得分:1)
基于rwr的答案,这里是SKSpriteNode的工作init方法。我在自己的游戏中使用它。基本上你在输出图像周围制作了10px的插图。然后像这样称呼它:
[[HudBoxScalable alloc] initWithTexture:[atlas textureNamed:@"hud_box_9grid.png"] inset:10 size:CGSizeMake(300, 100) delegate:(id<HudBoxDelegate>)clickedObject];
-(id) initWithTexture:(SKTexture *)texture inset:(float)inset size:(CGSize)size {
if (self=[super initWithTexture:texture]) {
self.centerRect = CGRectMake(inset/texture.size.width,inset/texture.size.height,(texture.size.width-inset*2)/texture.size.width,(texture.size.height-inset*2)/texture.size.height);
self.xScale = size.width/texture.size.width;
self.yScale = size.height/texture.size.height;
}
return self;
}
答案 4 :(得分:-2)
是的,我注意到在我检查了NSLOG后,我发布并在发布后不久修复了它。
但是根据这篇文章,'。centterRect'属性被破坏了,不起作用。
(我也搜索了Apple开发者论坛,发现有3个帖子引用了同样的问题。苹果还没有解决这个问题。