在xcode中的SpriteKit中,我正在建立一个游戏,玩家控制一个球(精灵节点),当球接触“ColorOrbChanger”(精灵节点)时,玩家会采用该颜色并且球体被移除,但是我无法弄清楚为什么removeFromParent无法正常工作。提前谢谢:)
class GameScene: SKScene, SKPhysicsContactDelegate {
var RedOrb = SKSpriteNode()
var OrangeOrb = SKSpriteNode()
var YellowOrb = SKSpriteNode()
var GreenOrb = SKSpriteNode()
var BlueOrb = SKSpriteNode()
var PurpleOrb = SKSpriteNode()
func OrbColorPicker() -> SKSpriteNode {
let RedOrb = SKSpriteNode(imageNamed: "ColourNodeRed")
RedOrb.color = SKColor.redColor()
RedOrb.size = CGSize(width: 40, height: 60)
RedOrb.position = CGPoint(x: self.frame.width - 140, y: self.frame.height / 2)
RedOrb.physicsBody = SKPhysicsBody(rectangleOfSize: RedOrb.size)
RedOrb.physicsBody?.categoryBitMask = PhysicsCatagory.RedOrb
RedOrb.physicsBody?.contactTestBitMask = PhysicsCatagory.Player
RedOrb.physicsBody?.affectedByGravity = false
RedOrb.physicsBody?.dynamic = false
RedOrb.runAction(moveAndRemove)
RedOrb.zPosition = 2
/* ^ plus all the other colours Orange, Yellow, Green, Blue, Purple, then i added all those into an array that spat out a random colour orb) */
var ColorOrbArray = [RedOrb, OrangeOrb, YellowOrb, GreenOrb, BlueOrb, PurpleOrb]
let countArray = UInt32(ColorOrbArray.count)
let pickOneNode = arc4random_uniform(countArray)
let randomElement = Int(pickOneNode)
if randomElement == 0{
self.addChild(RedOrb)
}
if randomElement == 1{
self.addChild(OrangeOrb)
}
if randomElement == 2{
self.addChild(YellowOrb)
}
if randomElement == 3{
self.addChild(GreenOrb)
}
if randomElement == 4{
self.addChild(BlueOrb)
}
if randomElement == 5{
self.addChild(PurpleOrb)
}
return ColorOrbArray[randomElement]
func didBeginContact(contact: SKPhysicsContact) {
if (contact.bodyA.categoryBitMask == PhysicsCatagory.Player) && (contact.bodyB.categoryBitMask == PhysicsCatagory.RedOrb) || (contact.bodyA.categoryBitMask == PhysicsCatagory.RedOrb) && (contact.bodyB.categoryBitMask == PhysicsCatagory.Player){
let changeColorActionRed = SKAction.colorizeWithColor(SKColor.redColor(), colorBlendFactor: 1.0, duration: 0)
Player.runAction(changeColorActionRed)
RedOrb.removeFromParent()
}
}