Ios 尝试在Swift+;Spritekit通过将触控绑定到Sprite,但收到我不知道的警告';我不知道如何解决这个问题

Ios 尝试在Swift+;Spritekit通过将触控绑定到Sprite,但收到我不知道的警告';我不知道如何解决这个问题,ios,swift,sprite-kit,multi-touch,subclassing,Ios,Swift,Sprite Kit,Multi Touch,Subclassing,我正在尝试将触摸绑定到特定的精灵。我在Objective-C中也做过类似的工作,在那里效果很好 我得到以下错误 在游戏场景类中:“游戏场景”类没有初始值设定项 在AnimalSprite类init方法中:调用中的额外参数“texture” 我试图用谷歌搜索这个问题,但无法真正解决。有什么想法吗? 我的ViewController是默认的SpriteKit模板。我没碰过它 class GameScene: SKScene { private var draggedNode: Dragga

我正在尝试将触摸绑定到特定的精灵。我在Objective-C中也做过类似的工作,在那里效果很好

我得到以下错误 在游戏场景类中:“游戏场景”类没有初始值设定项 在AnimalSprite类init方法中:调用中的额外参数“texture”

我试图用谷歌搜索这个问题,但无法真正解决。有什么想法吗? 我的ViewController是默认的SpriteKit模板。我没碰过它

class GameScene: SKScene {

    private var draggedNode: DraggableSprite

    override func didMoveToView(view: SKView) {
        /* Setup your scene here */
        self.physicsBody = SKPhysicsBody(edgeLoopFromRect: view.frame);
        self.backgroundColor = SKColor.greenColor()
        self.physicsWorld.gravity.dy = 0

        let animalSpawner = AnimalSpawner()
        self.addChild(animalSpawner)
        animalSpawner.startSpawning()
    }

    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
        /* Called when a touch begins */
        let touch = touches.first as! UITouch
        let touchLocation = touch.locationInNode(self)
        let touchedNode = self.nodeAtPoint(touchLocation)
        if !(touchedNode is AnimalSprite){ return; }

        // Bind touch
        let animalNode = touchedNode as! AnimalSprite
        animalNode.bindTouch(touch)
        self.draggedNode = animalNode
    }

    override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
        let touch = touches.first as! UITouch
        self.draggedNode.unbindTouchIfNeeded(touch)
    }

    override func update(currentTime: CFTimeInterval) {
        /* Called before each frame is rendered */
        self.draggedNode.drag()
    }

}

class DraggableSprite: SKSpriteNode {
    var touch: UITouch?
    private var touchOffset = CGPoint()
    private var isDragged: Bool? {
        get{
            if self.touch != nil {
                return true
            }
            return nil
        }
        set{ self.isDragged = newValue }
    }

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

    func bindTouch(touch: UITouch) {
        self.touch = touch
        let touchLocation = touch.locationInNode(self.parent)
        self.touchOffset = subtractVector(touchLocation, other: self.position)
    }

    func unbindTouchIfNeeded(touch: UITouch){
        if self.touch != touch {

        } else {
            self.touch = nil
        }
    }

    func drag(){
        if (self.touch == nil) { return; }
        let touchLocation = self.touch!.locationInNode(self.parent)
        self.position = subtractVector(touchLocation, other: self.touchOffset)
    }

    //MARK: Private methods
    private func subtractVector(p1: CGPoint, other: CGPoint) -> CGPoint {
        return CGPointMake(p1.x - other.x, p1.y - other.y)
    }

}

class AnimalSprite: DraggableSprite {

    private let animalSize = CGSizeMake(200.0, 200.0)
    private let baseDuration: CGFloat = 10
    private let baseAlpha: CGFloat = 1
    var animalType = Type()
    var isTouched = Bool()

    // Sounds
    ..

    init(){
        super.init(texture: nil, color: nil, size: animalSize)
        self.texture = randomAnimal()
        self.runAction(SKAction.rotateToAngle(0.15, duration: 0.01))
    }

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

    private func randomAnimal() -> SKTexture {
        // Here random animal textture will be returned
        let animalArray = ["cow", "pig", "chicken"]
        let randomIndex = Int(arc4random_uniform(UInt32(animalArray.count)))
        let animalType = animalArray[randomIndex]
        switch (animalType){
            case "cow":
                self.animalType = Type.Cow
                audioPlayer = AVAudioPlayer(contentsOfURL: cowSound, error: nil)
            case "chicken":
                self.animalType = Type.Chicken
                audioPlayer = AVAudioPlayer(contentsOfURL: chickenSound, error: nil)
            case "pig":
                self.animalType = Type.Pig
                audioPlayer = AVAudioPlayer(contentsOfURL: pigSound, error: nil)
            default:
                self.animalType = Type()
        }
        audioPlayer.prepareToPlay()
        return SKTexture(imageNamed: animalArray[randomIndex])
    }

    private func wiggle() -> SKAction {
        let rotateLeft = SKAction.rotateByAngle(-0.3, duration: 0.2)
        let rotateRight = SKAction.rotateByAngle(0.3, duration: 0.2)
        let wiggleSequence = SKAction.sequence([rotateLeft, rotateRight])
        let repeatForever = SKAction.repeatActionForever(wiggleSequence)
        return repeatForever
    }

    // MARK: Actions
    func startMove() {
        if let gameScene = self.scene {

            let destinationX = (self.position.x > gameScene.frame.width/2) ? -self.frame.width : gameScene.frame.width+self.frame.width //0 - CGRectGetWidth(gameScene.frame) - animalSize.width
            let destinationY = self.position.y //CGFloat(arc4random_uniform( UInt32(gameScene.size.height)))
            let duration = NSTimeInterval(baseDuration + CGFloat(Double(arc4random_uniform(10)) / 10.0))

            let travel = SKAction.moveTo(CGPointMake(destinationX, destinationY), duration: duration)
            let remove = SKAction.removeFromParent()
            let wiggle = self.wiggle()
            let groupAction = [SKAction .group([travel, wiggle])]
            let sequence = SKAction.sequence([groupAction,  remove])

            println("dest x: \(destinationX) dest y: \(destinationY)")
            self.runAction(sequence, withKey: "moving")
        }
    }

    func playSound() {
        audioPlayer.play()
    }
}
class游戏场景:SKScene{
私有变量draggedNode:DragableSprite
覆盖func didMoveToView(视图:SKView){
/*在这里设置场景*/
self.physicsBody=SKPhysicsBody(edgeLoopFromRect:view.frame);
self.backgroundColor=SKColor.greenColor()
self.physicsWorld.gravity.dy=0
让animalSpawner=animalSpawner()
self.addChild(animalSpawner)
动物典当
}
覆盖func touchesBegined(触摸:设置,withEvent事件:UIEvent){
/*当触摸开始时调用*/
让touch=touch.first as!UITouch
让touchLocation=touch.locationInNode(自身)
让touchedNode=self.nodeAtPoint(touchLocation)
如果!(touchedNode是AnimalSprite){return;}
//捆绑式接触
让animalNode=touchedNode为!AnimalSprite
animalNode.bindTouch(触摸)
self.draggedNode=动物节点
}
覆盖func touchesEnded(触摸:设置,withEvent事件:UIEvent){
让touch=touch.first as!UITouch
self.draggedNode.UnbindTouchIf needed(触摸)
}
覆盖函数更新(currentTime:CFTimeInterval){
/*在渲染每个帧之前调用*/
self.draggedNode.drag()
}
}
类DraggableSprite:SKSpriteNode{
var touch:UITouch?
私有变量touchpoffset=CGPoint()
二等兵:布尔{
得到{
如果self.touch!=零{
返回真值
}
归零
}
集合{self.isDraged=newValue}
}
必需的初始化?(编码器aDecoder:NSCoder){
fatalError(“初始化(编码者:)尚未实现”)
}
func bindTouch(触摸:UITouch){
self.touch=触摸
让touchLocation=touch.locationInNode(self.parent)
self.touchOffset=减去向量(touchLocation,其他:self.position)
}
func解除绑定触摸如果需要(触摸:UITouch){
如果self.touch!=touch{
}否则{
self.touch=nil
}
}
func drag(){
如果(self.touch==nil){return;}
让touchLocation=self.touch!.locationInNode(self.parent)
self.position=减去向量(touchLocation,其他:self.touchOffset)
}
//标记:私有方法
专用func减法向量(p1:CGPoint,其他:CGPoint)->CGPoint{
返回CGPointMake(p1.x-other.x,p1.y-other.y)
}
}
动物等级:DragableSprite{
private let animalSize=CGSizeMake(200.0,200.0)
私有let baseDuration:CGFloat=10
私有let baseAlpha:CGFloat=1
var animalType=Type()
var isTouched=Bool()
//听起来
..
init(){
super.init(纹理:nil,颜色:nil,大小:animalSize)
self.texture=randomAnimal()
自我运行(SKAction.旋转角度(0.15,持续时间:0.01))
}
必需的初始化(编码器aDecoder:NSCoder){
fatalError(“初始化(编码者:)尚未实现”)
}
private func randomAnimal()->SKTexture{
//这里将返回随机的动物文本
让动物阵列=[“牛”、“猪”、“鸡”]
设randomIndex=Int(arc4random_统一(UInt32(animalArray.count)))
让animalType=animalArray[randomIndex]
开关(动物类型){
案例“奶牛”:
self.animalType=Type.Cow
audioPlayer=AVAudioPlayer(内容URL:cowSound,错误:无)
案例“鸡”:
self.animalType=Type.Chicken
audioPlayer=AVAudioPlayer(内容URL:chickenSound,错误:无)
案例“猪”:
self.animalType=Type.Pig
audioPlayer=AVAudioPlayer(内容URL:pigSound,错误:无)
违约:
self.animalType=Type()
}
audioPlayer.prepareToPlay()
返回SKTexture(ImageName:animalArray[randomIndex])
}
私有函数wiggle()->SKAction{
让rotateLeft=SKAction.rotateByAngle(-0.3,持续时间:0.2)
让rotateRight=SKAction.RotateBingle(0.3,持续时间:0.2)
让wiggleSequence=SKAction.sequence([rotateLeft,rotateRight])
让repeatForever=SKAction.repeatActionForever(wiggleSequence)
永归
}
//马克:行动
func startMove(){
如果让GameSecene=self.scene{
让destinationX=(self.position.x>GameSecene.frame.width/2)?-self.frame.width:GameSecene.frame.width+self.frame.width//0-cDirectGetWidth(GameSecene.frame)-animalize.width
设destinationY=self.position.y//CGFloat(arc4random_统一(UInt32(游戏场景.大小.高度)))
let duration=NSTimeInterval(baseDuration+CGFloat(Double(arc4random_uniform(10))/10.0))
让travel=SKAction.moveTo(CGPointMake(destinationX,destinationY),持续时间:持续时间)
让remove=SKAction.removeFromParent()
让wiggle=self.wiggle()
让groupAction=[SKAction.group([travel,wiggle])]
让sequence=SKAction.sequence([groupAction,remove])
println(“目的地x:\(目的地x)目的地y:\(目的地)”)
self.runAction(序列,带键:“移动”)
}
}
func playSound(){
audioPlayer.play()
}
}

在游戏场景类中,缺少init函数。Init函数对于所有类都是必需的,您需要确保
 override init(size: CGSize) {
    super.init()
}
private var draggedNode: DraggableSprite?
func - (lhs: CGPoint, rhs: CGPoint) -> CGPoint {
    return CGPoint(x: lhs.x - rhs.x, y: lhs.y - rhs.y)
}