尝试在 Swift 上触摸屏幕时连续显示一堆图像
Posted
技术标签:
【中文标题】尝试在 Swift 上触摸屏幕时连续显示一堆图像【英文标题】:Trying to get a bunch of images to show in succession when touching screen on Swift 【发布时间】:2020-01-21 22:14:32 【问题描述】:我将不同的图像连续显示在 assets.xcassets 文件夹中,如 shooter、shooter1、shooter2 等,但每当我触摸屏幕时,视图中显示的动画/图像不会改变?
这是我的代码:
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)
我在视图中使用的精灵节点的名称为 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 为否。称呼 _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简介 Kit[5666:205206] [ServicesDaemonManager] interruptHandler 是 叫。 -[FontServicesDaemonManager 连接]_block_invoke 来自调试器的消息:由于信号 15 而终止
【问题讨论】:
您是否将射手节点的名称属性设置为“shooterNode”?设置好之后。你的代码对我有用。 如果你的意思是精灵属性是的,我已经做到了。它仍然不起作用。我想知道这是否是我第一次将精灵放入视图的方式?我直接从 shooter.atlas 文件夹中拖动了图像,因为我没有看到任何其他将精灵放入视图的选项? 我也从控制台得到这个输出。请看上面的编辑 【参考方案1】:您的代码已修改。这个对我有用。没有更多代码,我不知道你是如何设置你的射手节点的,所以我无法复制你的问题。
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)
【讨论】:
我在 shooterscene.sks 上发布了我的 xcode 外观编辑。从你所看到的,我做错了什么吗?以上是关于尝试在 Swift 上触摸屏幕时连续显示一堆图像的主要内容,如果未能解决你的问题,请参考以下文章