简体   繁体   English

iOS-Swift 2.2-SpriteKit-细分SKSpriteNode类

[英]ios - Swift 2.2 - SpriteKit - sublassing SKSpriteNode class

For a puzzle game project on iOS, I try to subclass the SKSpriteNode class from SpriteKit with the following code: 对于iOS上的益智游戏项目,我尝试使用以下代码从SpriteKit继承SKSpriteNode类:

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

I need to add other variables in SKSpriteNode like another name (name2) in this case. 在这种情况下,我需要在SKSpriteNode中添加其他变量,例如另一个名称(name2)。 Here is the use I made of the class in a class type SKScene: 这是我在类类型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)

 }

}

I have the sprite object from the subclass SKPuzzle wich contains the new variable name2. 我有一个来自子类SKPuzzle的sprite对象,其中包含新变量name2。

sprite.name2 = "\(i)"

The problem I have is the variable selectedNode (created with) 我的问题是变量selectedNode(创建时)

var selectedNode = SKPuzzle()

used later in the program contain always a nil value for the data name and name2. 稍后在程序中使用的数据名称和名称2始终包含nil值。 When I click on the jigsaw parts of the game, I get the following error: 当我单击游戏的拼图部分时,出现以下错误:

fatal error: unexpectly found nil while unwrapping an optional value in the following function: 致命错误:在展开以下函数中的可选值时意外地发现了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 seems containing only nil values. selectedNode似乎仅包含nil值。 The code is working fine when I just use the SKSpriteNode but failed with my SKPuzzle class. 当我只使用SKSpriteNode时,代码运行良好,但是我的SKPuzzle类失败了。

Here is the whole code of the program: 这是程序的全部代码:

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)


    }
 }

}

Thanks by advance for your help, I'm beginning to code with Swift on iOS. 在此先感谢您的帮助,我开始在iOS上使用Swift进行编码。

When selectedNode is created by SKPuzzle() , name is nil. selectedNode被创建SKPuzzle() name是零。

You have to set selectedNode.name to some value in init method. 您必须在init方法中将selectedNode.name设置为某个值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM