gpt4 book ai didi

iOS Sprite Kit - SKSpriteNode 的 .centerRect 属性不起作用

转载 作者:可可西里 更新时间:2023-11-01 04:15:38 27 4
gpt4 key购买 nike

我在浏览 Apple 的 SpriteKit 文档时发现了一个非常有用的功能,我可以在编写 UI 时使用它。问题是我无法让它工作。

请查看此页面并向下滚动到“调整 Sprite 大小”- Apple Docs

我从字面上复制了图像尺寸并使用了相同的代码以防我做错了什么。但我总是以拉伸(stretch)的图像结束,而不是保持相同比例的正确“端盖”。

我指的是这段代码:

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);

最佳答案

如果您调整 sprites 的 .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;

关于iOS Sprite Kit - SKSpriteNode 的 .centerRect 属性不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19715128/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com