动画无法在Swift中工作2.2
我是Swift语言的新手,我正在学习使用过时的swift语言的教程。该教程是关于用png文件创建动画的。所以总共有6个png文件创建了一个动画。这是教程“https://www.youtube.com/watch?v=5aCVyrJbrHQ”的链接。我注意到旧版本Swift和新版本中的一个重要区别。这里是我卡住的地方。动画无法在Swift中工作2.2
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let shooterNode = self.childNodeWithName("shooterNode")
if(shooterNode != nil) {
let animation = SKAction.animateWithTextures(shooterAnimation, timePerFrame: 0.1)
shooterNode?.runAction(animation)
}
新版本:覆盖FUNC的touchesBegan(触摸:设置,withEvent事件:的UIEvent
旧版本:覆盖FUNC的touchesBegan(触摸:NSSet中,withEvent事件:的UIEvent
错误
只要我运行模拟器,然后点击对象,我就没有任何反应,没有错误,只是没有发现任何错误,我检查了是否我用对象名称做了一个错字,但没有发现任何错误。
这是情况下,整个文件的完整代码,你会问它
import UIKit
import SpriteKit
class ShooterScene: SKScene {
var score = 0
var enemyCount = 10
var shooterAnimation = [SKTexture]()
override func didMoveToView(view: SKView) {
self.initShooterScene()
}
func initShooterScene(){
let shooterAtlas = SKTextureAtlas(named: "shooter")
for index in 1...shooterAtlas.textureNames.count {
let imgName = String(format: "shooter%01d", index)
shooterAnimation += [shooterAtlas.textureNamed(imgName)]
}
}
//Anime the shooter
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let shooterNode = self.childNodeWithName("shooterNode")
if(shooterNode != nil) {
let animation = SKAction.animateWithTextures(shooterAnimation, timePerFrame: 0.1)
shooterNode?.runAction(animation)
}
}
}
GameSwift.scene
import SpriteKit
class GameScene: SKScene {
override func didMoveToView(view: SKView) {
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let introLabel = childNodeWithName("introLabel")
if(introLabel != nil) {
let fadeOut = SKAction.fadeOutWithDuration(1.5)
introLabel?.runAction(fadeOut, completion: {
let doors = SKTransition.doorwayWithDuration(1.5)
let shooterScene = SKScene(fileNamed: "ShooterScene")
self.view?.presentScene(shooterScene!, transition: doors)
})
}
}
/* Called before each frame is rendered */
override func update(currentTime: CFTimeInterval) {
}
}
它应该是:
let shooterScene = ShooterScene(fileNamed: "ShooterScene")
,而不是:
let shooterScene = SKScene(fileNamed: "ShooterScene")
非常感谢!有人在评论中对这行代码非常误导:(但是,谢谢bro! –
@CecilBoye没问题:)实际上这是一个常见的错误。 – Whirlwind
您是否检查过每个方法是否都应该执行。在每个方法中放入一些打印语句,并确定是否所有内容都被正确调用。那将是一个开始。同时在'if(shooterNode!= nil){...}'块中放置一个print语句。 – Whirlwind
@Whirlwind感谢您的回应,我在文件中的每个功能上添加了打印,控制台中没有任何内容出现。所以我得出的结论是,我的ShooterScene.sks和ShooterScene.swift文件之间存在某种联系。 –
你在哪里出现'ShooterScene'?实际创建一个新的场景(及其转换)应该看起来像这样:'if let nextScene = ShooterScene(fileNamed:“ShooterScene”){/ *这里有可选转换的场景* /}'所以检查那个部分。 – Whirlwind