我是Swift的新手,所以如果这令人困惑,请原谅我。
我试图获得我的一个节点" guava"当" ninja"消失碰撞它。
目前我的代码在我的createScene()函数中设置如下:
func spawnGuava(){
//The way the Guava spawn and move
let guavaNode = SKNode()
let guava = SKSpriteNode(texture: guavaNodeTexture)
guava.setScale(0.75)
guava.position = CGPointMake (self.size.width + 160, self.size.height * 0.05)
guava.physicsBody = SKPhysicsBody(rectangleOfSize: guava.size)
guava.physicsBody?.affectedByGravity = false
guava.physicsBody?.dynamic = true
guava.alpha = 0.75
guava.physicsBody?.categoryBitMask = PhysicsCatagory.Guava
guava.physicsBody?.collisionBitMask = PhysicsCatagory.None
guava.physicsBody?.contactTestBitMask = PhysicsCatagory.Ninja
guava.zPosition = 0
guavaNode.addChild(guava)
guava.runAction(GuavaMoveAndRemove)
self.addChild(guavaNode)
func ninjaAteGuava(ninja: SKSpriteNode, guava: SKSpriteNode){
guavaNode.removeFromParent()
}
func didCollideWithGuava(contact: SKPhysicsContact){
var firstBody: SKPhysicsBody
var secondBody: SKPhysicsBody
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask{
firstBody = contact.bodyA
secondBody = contact.bodyB
}
else{
firstBody = contact.bodyB
secondBody = contact.bodyA
}
if ((firstBody.categoryBitMask & PhysicsCatagory.Guava != 0) && (secondBody.categoryBitMask & PhysicsCatagory.Ninja != 0)) {
ninjaAteGuava(firstBody.node as! SKSpriteNode, guava: secondBody.node as! SKSpriteNode)
}
}
这里有关于忍者的一些信息
let ninjaTexture = SKTexture(imageNamed:"ninja")
ninjaTexture.filteringMode = SKTextureFilteringMode.Nearest
ninja = SKSpriteNode(texture: NinjaTexture)
ninja.setScale(0.5)
ninja.position = CGPoint(x: self.frame.size.width * 0.50, y: self.frame.size.height * 0.6)
ninja.physicsBody = SKPhysicsBody(circleOfRadius: ninja.size.height / 2)
ninja.physicsBody?.categoryBitMask = PhysicsCatagory.Ninja
ninja.physicsBody?.collisionBitMask = PhysicsCatagory.Ground | PhysicsCatagory.Wall |
ninja.physicsBody?.contactTestBitMask = PhysicsCatagory.Ground | PhysicsCatagory.Wall | PhysicsCatagory.Guava
ninja.physicsBody!.dynamic = true
ninja.physicsBody!.allowsRotation = false
self.addChild(ninja)
额外信息:
struct PhysicsCatagory {
static let None : UInt32 = 0
static let Boognish : UInt32 = 0x1 << 1
static let Ground : UInt32 = 0x1 << 2
static let Wall : UInt32 = 0x1 << 3
static let Guava : UInt32 = 0x1 << 4
static let Pepper: UInt32 = 0x1 << 5
}
答案 0 :(得分:0)
guavaNode
guava
应为ninjaAteGuava function
func ninjaAteGuava(ninja: SKSpriteNode, guava: SKSpriteNode){
guavaNode.removeFromParent()
}
应该是:
func ninjaAteGuava(ninja: SKSpriteNode, guava: SKSpriteNode){
guava.removeFromParent()
}
这是因为函数中的参数是番石榴;不是guavaNode。我不知道为什么Xcode没有给你一个错误,但这应该有效。希望这会有所帮助。
答案 1 :(得分:0)
您是否已将guavaNode
添加到场景或guava
?
因为您要从场景中删除guavaNode
,但我怀疑您可能已将guava
添加到场景中
guava.runAction(GuavaMoveAndRemove)
或者可能还有其他内容可以触发您的错误?