试图获得一堆图像以在Swift上触摸屏幕时连续显示

问题描述 投票:1回答:1

[我将不同的图像依次作为shooter,shooter1,shoter2等显示在assets.xcassets文件夹中,但是只要我触摸屏幕,视图中显示的动画/图像就不会改变?

这是我的代码:

 import UIKit
 import SpriteKit

 class ShooterScene: SKScene
 {

     var score = 0
     var enemyCount = 10
     var shooterAnimation = [SKTexture]()

     override func didMove(to view: SKView)
     {
         self.initShooterScene()
     }

     func initShooterScene()
     {
         let shooterAtlas = SKTextureAtlas(named: "shooter") // referencing shooter.atlas
         for index in 1...shooterAtlas.textureNames.count
         {
             let imgName = "shooter\(index)"
             shooterAnimation += [shooterAtlas.textureNamed(imgName)]
         }
     }

     //Animate the shooter

     override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
         if let shooterNode  = self.childNode(withName: "shooterNode") as? SKSpriteNode
         {
             let animation = SKAction.animate(with: shooterAnimation, timePerFrame: 0.1)
             shooterNode.run(animation)
         }
      }



  }

我在视图中使用的Sprite节点的名称为shooterNode,但是图像似乎根本没有变化。任何帮助都会很棒

编辑:

控制台输出:2020-01-23 17:47:59.470204-0500教程31-Sprike简介套件[5666:203542]启用了金属API验证2020-01-23 17:47:59.715838-0500教程31-Sprike简介套件[5666:203542] SKView:ignoreRenderSyncInLayoutSubviews为NO。呼叫_renderSynchronouslyForTime,没有处理程序2020-01-23 17:48:11.764763-0500教程31-Sprike简介套件[5666:203964] XPC连接中断2020-01-23 17:48:11.765810-0500教程31-Sprike简介套件[5666:203968] [连接]连接中断:将尝试重新连接2020-01-23 17:48:11.765887-0500教程31-Sprike简介工具包[5666:205206] [ServicesDaemonManager] InterruptHandler是叫。 -[FontServicesDaemonManager连接] _block_invoke来自调试器的消息:由于信号15而终止

swift view sprite xcode11
1个回答
0
投票

这里您的代码已修改。这个对我有用。没有更多代码,我不知道您如何设置射击节点,因此我无法复制您的问题。

class ShooterScene: SKScene {

    var textures = [SKTexture]()

    //Also serves as the name of the texture the node uses.
    let NODE_NAME:String = "shooter"

    override init(size: CGSize) {
        super.init(size: size)
        //Better the setup scene in intializer, because didMove(to view) is called multiple times.
        //You only want to call the setup method once.
        self.setup()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }


    func setup() {

       //Node setup
       let shooterNode = SKSpriteNode(imageNamed: NODE_NAME)
       shooterNode.position = CGPoint(x: self.frame.midX, y: self.frame.midY)
       shooterNode.name = NODE_NAME
       addChild(shooterNode)

       //Create animation collection
       let atlas = SKTextureAtlas(named: NODE_NAME)
        //Arrays start at index zero so we need to subtract 1 from the total textures count
        let upperBound = atlas.textureNames.count - 1
        for index in 1...upperBound {
            let textureName = "\(NODE_NAME)\(index)"
            textures += [atlas.textureNamed(textureName)]
        }
    }

    //Animate the shooter when screen pressed.
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

        guard let shooterNode = self.childNode(withName: NODE_NAME) as? SKSpriteNode else { return }
        //For animations that are around 60 Frames per second
        let FPS:Double = 1/60
        let animation = SKAction.animate(with: self.textures, timePerFrame: FPS)
        shooterNode.run(animation)
    }



 }
© www.soinside.com 2019 - 2024. All rights reserved.