2017-06-05 47 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