我有一个创建可收集项目的类:
class CollectableCreate {
var collectable = SKSpriteNode()
var type = String()
var onCollect : () -> Void = {}
init(type: String, position: CGPoint, text: String = "This collectable uses no text!", onCollect : () -> Void) {
self.type = type.lowercaseString
switch self.type {
default:
collectable.size = CGSize(width: 1, height: 200)
collectable.color = SKColor.blueColor()
collectable.physicsBody = SKPhysicsBody(rectangleOfSize: collectable.size)
break
}
collectable.position = position
collectable.name = self.type
collectable.physicsBody?.categoryBitMask = PhysicsCategory.Collectable
collectable.physicsBody?.collisionBitMask = PhysicsCategory.Player
collectable.physicsBody?.contactTestBitMask = PhysicsCategory.Player
self.onCollect = onCollect
}
func chatBoxActivate() -> SKSpriteNode {
collectable.physicsBody?.dynamic = false
collectable.physicsBody?.affectedByGravity = false
return collectable
}
func collect() {
self.onCollect()
}
}
我创建了这样的可收集项目:
let d = CollectableCreate(type: "chatbox", position: CGPoint(x: 500, y: 100), onCollect: {
self.createChatBox("test blalb|blablalba")
})
self.addChild(d.chatBoxActivate())
在didBeginContact上我有这个:
if (firstBody.categoryBitMask == PhysicsCategory.Collectable || secondBody.categoryBitMask == PhysicsCategory.Collectable) {
let collectableBody = (firstBody.categoryBitMask == PhysicsCategory.Collectable ? firstBody.node : secondBody.node)
//collectableBody.collect() <---
collectableBody?.removeFromParent()
}
我想在遇到可收起的时候运行CollectableCreate().collect()
函数。如何实现这一目标?
答案 0 :(得分:1)
因为我从未与SpriteKit
合作过,所以我害怕我在黑暗中拍摄,所以请随时添加/或编辑我的答案。
你应该继承SKSpriteNode
:
class Collectable : SKSpriteNode {
var type: String?
var onCollect: (() -> ())?
convenience init(type: String, position: CGPoint, text: String = "This collectable uses no text!", onCollect : () -> Void) {
self.init()
self.type = type.lowercaseString
switch self.type {
default:
self.size = CGSize(width: 1, height: 200)
self.color = SKColor.blueColor()
self.physicsBody = SKPhysicsBody(rectangleOfSize: self.size)
break
}
self.position = position
self.name = self.type
self.physicsBody?.categoryBitMask = PhysicsCategory.Collectable
self.physicsBody?.collisionBitMask = PhysicsCategory.Player
self.physicsBody?.contactTestBitMask = PhysicsCategory.Player
self.onCollect = onCollect
}
func chatBoxActivate() -> SKSpriteNode {
self.physicsBody?.dynamic = false
self.physicsBody?.affectedByGravity = false
return self
}
func collect(){
self.onCollect?()
}
}
并在以下内容中使用它,并在控制器中添加您正在子类化的任何类,并添加正确的委托:
class SomeClass {
func someFunc(){
let collectable = Collectable(type: "chatbox", position: CGPoint(x: 500, y: 100), onCollect: {
self.createChatBox("FooBar")
})
self.addChild(collectable.chatBoxActivate())
}
func didBeginContact(contact: SKPhysicsContact){
let firstBody = contact.bodyA
let secondBody = contact.bodyB
if (firstBody.categoryBitMask == PhysicsCategory.Collectable || secondBody.categoryBitMask == PhysicsCategory.Collectable) {
let collectableBody = firstBody.categoryBitMask == PhysicsCategory.Collectable ? firstBody.node : secondBody.node
if let collectable = collectableBody as? Collectable {
collectable.collect()
}
collectableBody?.removeFromParent()
}
}
func addChild(collectable: SKSpriteNode){
// Some logic
}
func createChatBox(text: String) {
// Some logic
}
}
希望这可以指出你正确的方向!