Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sprite kit 如何从SpriteKit中已旋转的另一个纹理创建旋转纹理_Sprite Kit_Xcode6_Texture Atlas - Fatal编程技术网

Sprite kit 如何从SpriteKit中已旋转的另一个纹理创建旋转纹理

Sprite kit 如何从SpriteKit中已旋转的另一个纹理创建旋转纹理,sprite-kit,xcode6,texture-atlas,Sprite Kit,Xcode6,Texture Atlas,我正在从一个SKTexture创建一个精灵,它又是从一个atlas加载的另一个纹理创建的,如下所示: SKTexture *textureFromAtlas = [SKTexture textureWithImageNamed:@“MyImage.png”]; CGRect myRect = textureFromAtlas.textureRect; myRect.size.with *= 0.5; myRect.size.height *= 0.5; SKTexture *newTexture

我正在从一个
SKTexture
创建一个精灵,它又是从一个atlas加载的另一个纹理创建的,如下所示:

SKTexture *textureFromAtlas = [SKTexture textureWithImageNamed:@“MyImage.png”];
CGRect myRect = textureFromAtlas.textureRect;
myRect.size.with *= 0.5;
myRect.size.height *= 0.5;
SKTexture *newTexture = [SKTexture textureWithRect:myRect inTexture:textureFromAtlas];
SKSpriteNode * MySprite = [SKSpriteNode spriteNodeWithTexture:newTexture];
textureFromAtlas
恰好被旋转,因为在应用程序包中的文件夹Images.atlasc中,Images.1.png显示MyImage.png旋转,并且图像。plist将该子图像的键textureRotated设置为是

如何创建
MySprite
,使其具有正确的旋转

(A)正是人们所期待的,因为SpriteKit具有自动旋转功能; (B)是实际结果,即从上面的代码中得到的结果

编辑:

我必须添加一些代码来获得MyImage.png的大小,但要查看atlas中包含的已旋转的大小,以查看它是否已旋转,然后计算一个新的已旋转的
myRect
以用于
mySrpite

CGRect textureRect = textureFromAtlas.textureRect;
CGSize atlasSize = [[SKTexture textureWithRect:CGRectMake(0, 0, 1, 1)
                                     inTexture:textureFromAtlas] size];
CGSize sizeInAtlas = CGSizeMake(textureRect.size.width * atlasSize.width,
                                textureRect.size.height * atlasSize.height);
myRect = CGRectMake(myRect.origin.x,
                    myRect.origin.y+myRect.size.height-sizeInAtlas.height/atlasSize.height);
SKTexture *rotatedTexture = [SKTexture textureWithRect:myRect
                                             inTexture:textureFromAtlas];
mySprite = [SKSpriteNode spriteWithTexture: rotatedTexture];
mySprite.zRotation = M_PI_2;

这将有效地将矩形“移动”到image.1.png中旋转图像的左上角,从而
mySprite
将成为(A)中的矩形,但是这仅在MyImage.png完全不透明或没有透明边框时才有效,因为SpriteKit对那些具有透明度的图像进行了一些优化,并且纹理比atlas中的实际帧小

不幸的是,当使用来自SKTextureAtlas的纹理时,textureWithRect:inTexture似乎存在错误。这是有意义的,因为您正在尝试从精灵贴图创建精灵贴图,我可以看到这会导致一些性能问题

为了得到你想要的结果,我认为它有两种不同的工作方式

第一个选项是将MyImage移出atlas文件夹(或使用asset catelog)并使用textureWithRect:inTexture。这是理想的,如果图像已经创建为精灵表,你不想把它切碎。这是一个不错的解决方案,但使用第二个选项可能会得到更好的结果

SKTexture *textureFromAtlas = [SKTexture textureWithImageNamed:@“MyImage.png”];
CGRect myRect = CGRectMake(0,.5,.5,.5);
SKTexture *newTexture = [SKTexture textureWithRect:myRect inTexture:textureFromAtlas];
SKSpriteNode * MySprite = [SKSpriteNode spriteNodeWithTexture:newTexture];
第二个选项是将图像切碎,然后将这些图像添加到images.atlas。如果您希望在一张sprite表(atlas)上同时渲染其他纹理,则此选项更为理想。SpriteKit将负责在运行时将它们作为一个图像放在一起

红色/绿色将是切碎的图像,然后再进行组合,而粉色/绿色只是像以前一样的一个图像

然后,您将能够轻松地使用以下代码:

SKTexture *textureFromAtlas = [SKTexture textureWithImageNamed:@"MyImage1.png"];
SKSpriteNode *mySprite = [SKSpriteNode spriteNodeWithTexture:textureFromAtlas];

这两个选项都不需要担心旋转。即使.atlas在您取出图像时旋转它,它也不会旋转。

我已经用您建议的代码编写了一个示例,但它不起作用,可能我遗漏了什么。当你说atlasc时,我认为mySprite仅为正常图像的25%(即GameSecene.m第14行中的0),我认为c是一个打字错误。你在用texturepacker吗?@rraallvv我更新了我的答案。我验证了plist与苹果为旋转图像动态创建的相同。我还验证了它是否适用于地图集以外的图像。我用这些新发现更新了我的答案。嗯。。。你的方法似乎更简单,但我不能让它工作,请看一看,基本上是一样的,但对于使用单独文件的地图集,我认为这肯定是一个错误bug@rraallvv我更新了我的答案,以便更好地展示解决问题的两种方法。我还用两个不同的选项以我正在解释的格式更新了您的示例。"