Using sprite sheets in xcode

前端 未结 4 731
一向
一向 2020-12-14 13:19

I\'m trying to animate my game using a sprite sheet. How would I go about cutting out each sprite from the sprite sheet and using the sprite in xcode? I\'m currently using o

4条回答
  •  情歌与酒
    2020-12-14 13:58

    Since the original question was tagged as Objective-C, here's a rough version of this SpriteSheet class in that language. Hope this is helpful:

    #import "SpriteSheet.h"
    
    @interface SpriteSheet ()
    @property (nonatomic, strong) SKTexture *texture;
    @property (nonatomic)         NSInteger rows;
    @property (nonatomic)         NSInteger cols;
    @property (nonatomic)         CGFloat   margin;
    @property (nonatomic)         CGFloat   spacing;
    @property (nonatomic, getter=frameSize)         CGSize    frameSize;
    @end
    
    
    @implementation SpriteSheet
    
    - (instancetype)initWithTextureName:(NSString *)name rows:(NSInteger)rows cols:(NSInteger)cols margin:(CGFloat)margin spacing:(CGFloat)spacing {
      SKTexture *texture = [SKTexture textureWithImageNamed:name];
      return [self initWithTexture:texture rows:rows cols:cols margin:margin spacing:spacing];
    }
    
    - (instancetype)initWithTexture:(SKTexture *)texture rows:(NSInteger)rows cols:(NSInteger)cols {
      return [self initWithTexture:texture rows:rows cols:cols margin:0 spacing:0];
    }
    
    - (instancetype)initWithTexture:(SKTexture *)texture rows:(NSInteger)rows cols:(NSInteger)cols margin:(CGFloat)margin spacing:(CGFloat)spacing {
      if (self == [super init]) {
        _texture = texture;
        _rows = rows;
        _cols = cols;
        _margin = margin;
        _spacing = spacing;
        _frameSize = [self frameSize];
    
      }
      return self;
    }
    
    - (CGSize)frameSize {
    
      CGSize newSize = CGSizeMake((self.texture.size.width - (self.margin * 2.0 + self.spacing * ((CGFloat)self.cols - 1.0))) / ((CGFloat)self.cols),
                        (self.texture.size.height - ((self.margin * 2.0) + (self.spacing * ((CGFloat)self.rows - 1.0))) / ((CGFloat)self.rows)));
    
      return newSize;
    }
    
    - (SKTexture *)textureForColumn:(NSInteger)column andRow:(NSInteger)row {
    
      if (column >= (self.cols) || row >= (self.rows)) {
        NSLog(@"Asking for row or col greater than spritesheet");
        return nil;
      }
    
      CGRect textureRect = CGRectMake(self.margin + (column * self.frameSize.width + self.spacing) - self.spacing,
                                      self.margin + (row * self.frameSize.width + self.spacing) - self.spacing, // note using width here
                                      self.frameSize.width,
                                      self.frameSize.height);
    
      textureRect = CGRectMake(textureRect.origin.x / self.texture.size.width, textureRect.origin.y / self.texture.size.height, textureRect.size.width / self.texture.size.width, textureRect.size.height/self.texture.size.height);
    
      return [SKTexture textureWithRect:textureRect inTexture:self.texture];
    }
    

提交回复
热议问题