Reputation: 713
I am trying to develop a game as a complete beginner. I setup a game scene that does reference a object called taxiNode and BlockNode correctly.
I now want to make things interactive and want to add an impulse on the taxiNode, when tapping the BlockNode. For that I set up func interact() within my BlockNode class, but I can not access my TaxiNode.
Here is my code for the BlockNode Class
import SpriteKit
class BlockNode: SKSpriteNode, CustomNodeEvents, InteractiveNode {
func didMoveToScene() {
print("block added")
userInteractionEnabled = true
}
func interact() {
taxiNode.physicsBody!.applyForce(CGVectorMake(0, 400))
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
super.touchesEnded(touches, withEvent: event)
print("destroy block")
//interact()
}
}
My GameScene Class looks like this
import SpriteKit
struct PhysicsCategory {
static let None: UInt32 = 0
static let Taxi: UInt32 = 0b1 // 1
static let Block: UInt32 = 0b10 // 2
static let Edge: UInt32 = 0b100 // 4
/* static let Edge: UInt32 = 0b1000 // 8
static let Label: UInt32 = 0b10000 // 16
static let Spring:UInt32 = 0b100000 // 32
static let Hook: UInt32 = 0b1000000 // 64 */
}
protocol CustomNodeEvents {
func didMoveToScene()
}
protocol InteractiveNode {
func interact()
}
class GameScene: SKScene, SKPhysicsContactDelegate {
var taxiNode: TaxiNode!
override func didMoveToView(view: SKView) {
/* Setup your scene here */
// Calculate playable margin
let maxAspectRatio: CGFloat = 16.0/9.0 // iPhone 5
let maxAspectRatioHeight = size.width / maxAspectRatio
let playableMargin: CGFloat = (size.height - maxAspectRatioHeight)/2
let playableRect = CGRect(x: 0, y: playableMargin,
width: size.width, height: size.height-playableMargin*2)
physicsBody = SKPhysicsBody(edgeLoopFromRect: playableRect)
physicsWorld.contactDelegate = self
physicsBody!.categoryBitMask = PhysicsCategory.Edge
enumerateChildNodesWithName("//*", usingBlock: {node, _ in
if let customNode = node as? CustomNodeEvents {
customNode.didMoveToScene()
}
})
taxiNode = childNodeWithName("taxi") as! TaxiNode
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
}
And I get the following error within my BlockNode Class
"Use of unresolved identifier "taxiNode"
Does anyone have a clue what I need to fix to adress the taxiNode and make the receive my impulse?
Upvotes: 0
Views: 72
Reputation: 16837
Look up scope of variables to learn more.
Your block Node does not know what taxi node is, nor should it.
What you need to do here is let your blockNode know what taxi is.
To do this, you have to pass it in:
First establish the function correctly:
func interact(taxiNode : TaxiNode) {
taxiNode.physicsBody!.applyForce(CGVectorMake(0, 400))
}
Then when you need to interact:
blockNode.interact(taxiNode)
Make sure you fix your protocol to handle this.
protocol InteractiveNode {
func interact(taxiNode:TaxiNode)
}
Upvotes: 1