我有一个 sprite 表,其中包含我的 sprite 的动画图像。 我如何在不拆分单个文件的情况下将这些图像放入 SKTextureAtlas(这样我就可以在 SKAction 中制作动画)?我以前见过并做过这个,但是不使用 SpriteKit 和/或 SKTextures。
如果不拆分文件,这甚至可能吗? 最坏的情况:我需要将工作表中的图像分开。
在过去,我只是制作了一个方法,其中包含 Sprite 大小和 Sprite 总数,这些方法在代码中将图像分开,然后进行动画处理。
在此先感谢您的帮助!
附言我是 SpriteKit 的新手,但不是游戏编程或 iOS 的新手
最佳答案
感谢@LearnCocos2D 指导我使用 textureWithRect:inTexture:
我在自定义 Entity : SKSpriteNode
类中创建了一个自定义初始化方法,它允许使用一张包含所有图像的 Sprite 表。我试图包含一些注释来解释代码。我让这个版本扫描 x,因为我的动画是水平的。 Y 只能通过调用的方法进行更改。不在这个版本的方法中。
一定要把它放在界面中!
分配初始化时的例子:
Entity *cookie = [[Entity alloc] initWithSpriteSheetNamed:@"cookie_sheet" withinNode:map sourceRect:CGRectMake(0, 0, 32, 32) andNumberOfSprites:6];
实体中的方法:SKSpriteNode
- (id) initWithSpriteSheetNamed: (NSString *) spriteSheet withinNode: (SKSpriteNode *) scene sourceRect: (CGRect) source andNumberOfSprites: (int) numberOfSprites {
// @param numberOfSprites - the number of sprite images to the left
// @param scene - I add my sprite to a map node. Change it to a SKScene
// if [self addChild:] is used.
NSMutableArray *mAnimatingFrames = [NSMutableArray array];
SKTexture *ssTexture = [SKTexture textureWithImageNamed:spriteSheet];
// Makes the sprite (ssTexture) stay pixelated:
ssTexture.filteringMode = SKTextureFilteringNearest;
float sx = source.origin.x;
float sy = source.origin.y;
float sWidth = source.size.width;
float sHeight = source.size.height;
// IMPORTANT: textureWithRect: uses 1 as 100% of the sprite.
// This is why division from the original sprite is necessary.
// Also why sx is incremented by a fraction.
for (int i = 0; i < numberOfSprites; i++) {
CGRect cutter = CGRectMake(sx, sy/ssTexture.size.width, sWidth/ssTexture.size.width, sHeight/ssTexture.size.height);
SKTexture *temp = [SKTexture textureWithRect:cutter inTexture:ssTexture];
[mAnimatingFrames addObject:temp];
sx+=sWidth/ssTexture.size.width;
}
self = [Entity spriteNodeWithTexture:mAnimatingFrames[0]];
animatingFrames = mAnimatingFrames;
[scene addChild:self];
return self;
}
关于ios - 在 Sprite Kit iOS 中使用一张图片 Sprite 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20271812/