首页 文章

在Sprite Kit iOS中使用一个图像精灵表

提问于
浏览
11

我有一个精灵表,上面有我的精灵的动画图片 . How would I make put these images in to a SKTextureAtlas (so I can animate in an SKAction) without splitting up the individual files? 我之前见过并做过这个,但没有使用SpriteKit和/或SKTextures .

Is this even possible without splitting up the files? 最糟糕的情况:我需要将工作表分开制作图像 .

在过去,我只使用精灵大小和总精灵数来制作一个方法,然后将代码中的图像分开,然后进行动画制作 .

在此先感谢您的帮助!

附:我是SpriteKit的新手,但不是游戏编程或iOS

2 回答

  • 3

    感谢@ LearnCocos2D指点我使用 textureWithRect:inTexture: 我在自定义 Entity : SKSpriteNode 类中创建了一个自定义初始化方法,这将允许使用包含所有图像的精灵表 . 我试图包含一些注释来解释代码 . 我有这个版本扫描x,因为我的动画是水平的 . 只能使用调用的方法更改Y.不在此版本的方法中 .

    一定要把它放在界面中!

    alloc-initing的示例:

    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;
    }
    
  • 12

    您需要将每个动画帧作为单独的图像 . 然后,您可以使用SKAction为图像设置动画,可选择使用SKTextureAtlas .

    所以是的,除非你编写一个自定义动画处理程序来手动更改动画精灵的纹理矩形,否则你必须拆分现有的工作表 . 您可以使用textureWithRect:inTexture:从spritesheet纹理创建较小的纹理 .

相关问题