Using sprite sheets in xcode

前端 未结 4 742
一向
一向 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 14:02

    I did a fix for getting sprites from top to bottom, left to right.

    Also added a totalFrames so if you don't have a sprite with all frames, you can set the total frames so the array only contains the sprites, no blank sprites.

    //
    //  SpriteSheet.swift
    //
    
    import SpriteKit
    
    class SpriteSheet {
        let texture: SKTexture
        let rows: Int
        let columns: Int
        var margin: CGFloat=0
        var spacing: CGFloat=0
        var frameSize: CGSize {
            return CGSize(width: (self.texture.size().width-(self.margin*2+self.spacing*CGFloat(self.columns-1)))/CGFloat(self.columns),
                      height: (self.texture.size().height-(self.margin*2+self.spacing*CGFloat(self.rows-1)))/CGFloat(self.rows))
        }
    
        init(texture: SKTexture, rows: Int, columns: Int, spacing: CGFloat, margin: CGFloat) {
            self.texture=texture
            self.rows=rows
            self.columns=columns
            self.spacing=spacing
            self.margin=margin
    
        }
    
        convenience init(texture: SKTexture, rows: Int, columns: Int) {
            self.init(texture: texture, rows: rows, columns: columns, spacing: 0, margin: 0)
        }
    
        func textureForColumn(column: Int, row: Int)->SKTexture? {
            if !(0...self.rows ~= row && 0...self.columns ~= column) {
                return nil
            }
            var textureRect = CGRect(x: self.margin + CGFloat(column) * (self.frameSize.width+self.spacing)-self.spacing, y: self.margin + CGFloat(self.rows - row - 1) * (self.frameSize.height+self.spacing)-self.spacing,
                               width: self.frameSize.width,
                               height: self.frameSize.height)
    
            textureRect = CGRect(x: textureRect.origin.x/self.texture.size().width, y: textureRect.origin.y/self.texture.size().height,
                           width: textureRect.size.width/self.texture.size().width, height: textureRect.size.height/self.texture.size().height)
            return SKTexture(rect: textureRect, in: self.texture)
        }
    }
    

    To call, just add the animation like this:

    fileprivate let framesOpening: [SKTexture] = {
        let totalFrames = 28
        let rows = 6
        let columns = 5
        let sheet = SpriteSheet(texture: SKTexture(imageNamed: "GETREADY"), rows: rows, columns: columns, spacing: 0, margin: 0)
        var frames = [SKTexture]()
        var count = 0
    
        for row in (0..

    and call it

        let sprite1 = SKSpriteNode(texture: framesOpening.first)
        let openingAnimation: SKAction = SKAction.animate(with: framesOpening, timePerFrame: 0.2, resize: false, restore: true)
        sprite1.position = CGPoint(x: frame.midX, y: frame.midY)
        sprite1.zPosition = 2000
        self.addChild(sprite1)
        sprite1.run(SKAction.repeatForever(openingAnimation))
    

提交回复
热议问题