gpt4 book ai didi

ios - Swift 2.2 - SpriteKit - 子类 SKSpriteNode 类

转载 作者:行者123 更新时间:2023-11-30 13:23:37 25 4
gpt4 key购买 nike

对于 iOS 上的益智游戏项目,我尝试使用以下代码从 SpriteKit 中继承 SKSpriteNode 类:

class SKPuzzle: SKSpriteNode {
var name2:String = "";
}

我需要在 SKSpriteNode 中添加其他变量,例如本例中的另一个名称 (name2)。以下是我在类类型 SKScene 中对该类的使用:

class GameScene: SKScene {

let background = SKSpriteNode(imageNamed: "BW")
var selectedNode = SKPuzzle()

override init(size: CGSize) {
super.init(size: size)

let imageNames = [sheet.Puzzle13() , sheet.Puzzle19(),sheet.Puzzle30(),
sheet.Puzzle11(), sheet.Puzzle29(), sheet.Puzzle35() ]

for i in 0..<imageNames.count {
let imageName = imageNames[i]

let sprite = SKPuzzle(texture: imageName)

sprite.name = kAnimalNodeName
sprite.name2 = "\(i)"

let offsetFraction = (CGFloat(i) + 1.0)/(CGFloat(imageNames.count) + 1.0)

sprite.position = CGPoint(x: size.width * offsetFraction, y: size.height / 2)
sprite.zPosition = 1

background.addChild(sprite)

}

}

我有来自子类 SKPuzzle 的 Sprite 对象,其中包含新变量 name2。

sprite.name2 = "\(i)"

我遇到的问题是变量 selectedNode (创建的)

var selectedNode = SKPuzzle()

稍后在程序中使用的数据名称和 name2 始终包含 nil 值。当我点击游戏的拼图部分时,出现以下错误:

fatal error :在以下函数中解包可选值时意外发现 nil:

func panForTranslation(translation : CGPoint) {

let position = selectedNode.position

if selectedNode.name! == kAnimalNodeName {
selectedNode.position = CGPoint(x: position.x + translation.x * 2, y: position.y + translation.y * 2)


}
}

selectedNode 似乎只包含 nil 值。当我只使用 SKSpriteNode 时,代码工作正常,但使用 SKPuzzle 类时失败。

以下是该程序的完整代码:

import SpriteKit
import UIKit

private let kAnimalNodeName = "puzzle"
private let kdancing = "dancing"

class SKPuzzle: SKSpriteNode {
var name2:String = "";
}

class GameScene: SKScene {



let background = SKSpriteNode(imageNamed: "BW")
var selectedNode = SKPuzzle()
var selectedVideo = SKVideoNode()



override init(size: CGSize) {
super.init(size: size)

// 1
self.background.name = kdancing
self.background.anchorPoint = CGPointZero
background.zPosition = 0
self.addChild(background)
//background.play()

// 2
let sheet = Statiques()

let sprite_dancing1 = SKSpriteNode(texture: sheet.Dancing1())
let sprite_dancing2 = SKSpriteNode(texture: sheet.Dancing2())
sprite_dancing1.name = kdancing
sprite_dancing2.name = kdancing


sprite_dancing1.position = CGPoint(x: 837, y: 752)
sprite_dancing1.zPosition = 1
sprite_dancing2.position = CGPoint(x: 1241, y: 752)
sprite_dancing2.zPosition = 1

background.addChild(sprite_dancing1)

background.addChild(sprite_dancing2)



let imageNames = [sheet.Puzzle13() , sheet.Puzzle19(), sheet.Puzzle30(), sheet.Puzzle11(), sheet.Puzzle29(), sheet.Puzzle35() ]

for i in 0..<imageNames.count {
let imageName = imageNames[i]

let sprite = SKPuzzle(texture: imageName)

sprite.name = kAnimalNodeName
sprite.name2 = "\(i)"

let offsetFraction = (CGFloat(i) + 1.0)/(CGFloat(imageNames.count) + 1.0)

sprite.position = CGPoint(x: size.width * offsetFraction, y: size.height / 2)
sprite.zPosition = 1

background.addChild(sprite)

}

}

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

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

for touch: AnyObject in touches {
let positionInScene = touch.locationInNode(self)
selectNodeForTouch(positionInScene)
}
}


override func didMoveToView(view: SKView) {

let gestureRecognizer = UIPanGestureRecognizer(target: self, action: Selector("handlePanFrom:"))
self.view!.addGestureRecognizer(gestureRecognizer)
}

func handlePanFrom(recognizer : UIPanGestureRecognizer) {

if recognizer.state == .Began {
var touchLocation = recognizer.locationInView(recognizer.view)
touchLocation = self.convertPointFromView(touchLocation)

self.selectNodeForTouch(touchLocation)
} else if recognizer.state == .Changed {
var translation = recognizer.translationInView(recognizer.view!)
translation = CGPoint(x: translation.x, y: -translation.y)

self.panForTranslation(translation)

recognizer.setTranslation(CGPointZero, inView: recognizer.view)
} else if recognizer.state == .Ended {

}
}

func degToRad(degree: Double) -> CGFloat {
return CGFloat(degree / 180.0 * M_PI)
}

func selectNodeForTouch(touchLocation : CGPoint) {
// 1
let touchedNode = self.nodeAtPoint(touchLocation)

if touchedNode is SKPuzzle {
// 2
if !selectedNode.isEqual(touchedNode) {
selectedNode.removeAllActions()
selectedNode.runAction(SKAction.rotateToAngle(0.0, duration: 0.1))

//selectedNode = touchedNode as! SKSpriteNode

// 3
if touchedNode.name! == kAnimalNodeName {
let sequence = SKAction.sequence([SKAction.rotateByAngle(degToRad(-4.0), duration: 0.1),
SKAction.rotateByAngle(0.0, duration: 0.1),
SKAction.rotateByAngle(degToRad(4.0), duration: 0.1)])
selectedNode.runAction(SKAction.repeatActionForever(sequence))
}
}
}
}



func panForTranslation(translation : CGPoint) {
let position = selectedNode.position

if selectedNode.name! == kAnimalNodeName {
selectedNode.position = CGPoint(x: position.x + translation.x * 2, y: position.y + translation.y * 2)


}
}

}

预先感谢您的帮助,我开始在 iOS 上使用 Swift 进行编码。

最佳答案

selectedNodeSKPuzzle() 创建时,name 为 nil。

您必须在 init 方法中将 selectedNode.name 设置为某个值。

关于ios - Swift 2.2 - SpriteKit - 子类 SKSpriteNode 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37482376/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com