2017-06-05 49 views
1

我有兩個功能,一個叫球屬性 - 設置()和發射():我請ball.launchSpriteKit類分離問題

var ball = Ball() 

()後,我想知道它在哪裏上x軸使用:

ball.position.x 

但由於某種原因,我一直收到零。現在,我從touchesBegan啓動球並試圖獲得touchesEnded的位置。正如我所說,啓動部分工作正常。球出現了,並且如預期的那樣彈跳了,但是在那之後我沒做任何球屬性有任何影響。下面是完整的代碼:

Ball.swift

class Ball: SKSpriteNode { 

    var ball: SKSpriteNode! 

    func setUp(parentNode: SKNode) { 

     ball = SKSpriteNode(imageNamed: "ball2") 
     ball.name = "ball" 
     ball.position = CGPoint(x: 20, y: 200) 
     ball.physicsBody = SKPhysicsBody(circleOfRadius: ball.frame.width/2) 
     ball.physicsBody?.restitution = 0 
     ball.physicsBody?.velocity = CGVector(dx: 0, dy: 0) 

     ball.physicsBody?.categoryBitMask = PhysicsCategories.Ball 
     ball.physicsBody?.collisionBitMask = PhysicsCategories.Edge 
     ball.physicsBody?.contactTestBitMask = PhysicsCategories.Edge 
     parentNode.addChild(ball) 
    } 


    func launch() { 
     ball.physicsBody?.applyImpulse(CGVector(dx: 25, dy: 0)) 
    } 

} 

GameScene.swift

import SpriteKit 
import GameplayKit 

struct PhysicsCategories { 

    static let Ball:  UInt32 = 0 
    static let Edge:  UInt32 = 0b1 
} 

class GameScene: SKScene, SKPhysicsContactDelegate { 

    var ball = Ball() 

    override func didMove(to view: SKView) { 

     let border = SKPhysicsBody(edgeLoopFrom: self.frame) 
     border.friction = 0 
     border.restitution = 1 
     physicsBody = border 
     border.categoryBitMask = PhysicsCategories.Edge 
     border.collisionBitMask = PhysicsCategories.Ball 
     border.contactTestBitMask = PhysicsCategories.Ball 

     physicsWorld.contactDelegate = self 
    } 

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
     ball.setUp(parentNode: self) 
     ball.launch() 
    } 

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { 
     print(ball.position.x) //value is always zero 
    } 


    func didBegin(_ contact: SKPhysicsContact) { 
     let collision = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask 

     switch collision { 
     case PhysicsCategories.Ball | PhysicsCategories.Edge: 
      ball.removeFromParent() //doesn't get removed 
     default: 
      break 
     } 
    } 
} 

回答

2

的問題是,您試圖訪問錯誤的性質。您在您的gamescene.swift中創建Ball,但除此之外什麼也不做,除了致電launch(),但launch()僅影響ball.ball,因爲您已經創建了兩個球屬性。

您在gamescene中創建的球永遠不會添加到場景中,也不會執行任何操作,因此爲什麼它會顯示您的位置永不改變。

爲了改善這種情況,您要使用的Ball類作爲藍圖供您gamescene創建球性能

class Ball: SKSpriteNode { 

    // You don't want to create an object of the type you are creating here... 
    // as a property of the class!: 
    // var ball: SKSpriteNode! 

    func setUp(parentNode: SKNode) { 

    self.name = "ball" 
    self.position = CGPoint(x: 20, y: 200) 
    self.physicsBody = SKPhysicsBody(circleOfRadius: ball.frame.width/2) 
    self.physicsBody?.restitution = 0 
    self.physicsBody?.velocity = CGVector(dx: 0, dy: 0) 

    self.physicsBody?.categoryBitMask = PhysicsCategories.Ball 
    self.physicsBody?.collisionBitMask = PhysicsCategories.Edge 
    self.physicsBody?.contactTestBitMask = PhysicsCategories.Edge 
    self.parentNode.addChild(ball) 
    } 


    func launch() { 
    self.physicsBody?.applyImpulse(CGVector(dx: 25, dy: 0)) 
    } 

} 

現在當你調用ball.launch(),你實際上是在修改狀態ball,不ball.ball(因爲我刪除ball.ball並將其更改爲self =})

下面是一個簡單的表示什麼你在做,爲什麼它不能按預期工作:

class Cat { 

var fluffy: Cat! 

    var name = "no name" 

    func changeName() { 
    fluffy = Cat() 
    fluffy.name = "Fluffy" 
    } 

} 

var fluffy = Cat() 
fluffy.changeName() 
print(fluffy.name) // no name 
print(fluffy.fluffy.name) // Fluffy