Reputation: 29
I want to generate a node that can be removed by clicking, i can generate it and some times even click it and remove it, but then it comes back or it starts to make strange movements until the game crashes. This is what appears in the console:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attemped to add a SKNode which already has a parent: <SKSpriteNode> name:'(null)' texture:[<SKTexture> 'cuadradoRojo' (164 x 164)] position:{362.90328979492188, 790.66668701171875} size:{54.666667938232422, 54.666667938232422} rotation:0.00'
*** First throw call stack:
(0x183dea59c 0x19453c0e4 0x183dea4dc 0x18841c0fc 0x1000e73d0 0x1000e7d34 0x1000e8038 0x1000e8d44 0x18840e9ac 0x18840bfd8 0x188409038 0x188435fd8 0x100364a9c 0x187f3d280 0x187f3d118 0x184f398d0 0x183d8d5e4 0x183da2200 0x183da2160 0x183da00e0 0x183ccd0a4 0x18ce675a4 0x1885feaa4 0x10010f0c4 0x10010f104 0x194baaa08)
libc++abi.dylib: terminating with uncaught exception of type NSException
I don't have exerience programming so i don't know how to interpretate that. Here is my code, hope you can help. Thanks.
import SpriteKit
enum BodyType : UInt32 {
case colorAzul = 1
case circuloAzul = 2
case colorRojo = 4
case circuloRojo = 8
case cuadrado = 16
}
class Scene: SKScene , SKPhysicsContactDelegate{
var cuadradoRojo = SKSpriteNode(imageNamed: "cuadradoRojo")
var lastYieldTimeInterval:NSTimeInterval = NSTimeInterval()
var lastUpdateTimerInterval:NSTimeInterval = NSTimeInterval()
var velocity:CGFloat = 0
var gameOver = false
var score = 0
override func didMoveToView(view: SKView) {
}
func addAlien(){
cuadradoRojo.physicsBody = SKPhysicsBody(circleOfRadius: cuadradoRojo.size.width)
cuadradoRojo.physicsBody?.dynamic = true
cuadradoRojo.physicsBody?.categoryBitMask = BodyType.cuadrado.rawValue
cuadradoRojo.physicsBody?.contactTestBitMask = BodyType.colorAzul.rawValue | BodyType.colorRojo.rawValue
cuadradoRojo.physicsBody?.collisionBitMask = 0
var actionArray:NSMutableArray = NSMutableArray()
if gameOver == false{
let minX = cuadradoRojo.size.width/2
let maxX = self.frame.size.width - cuadradoRojo.size.width/2
let rangeX = maxX - minX
let position:CGFloat = CGFloat(arc4random()) % CGFloat(rangeX) + CGFloat(minX)
cuadradoRojo.position = CGPointMake(position, self.frame.size.height + cuadradoRojo.size.height)
addChild(cuadradoRojo)
let minDuration = 3
let duration = Int(minDuration)
actionArray.addObject(SKAction.moveTo(CGPointMake(position, -cuadradoRojo.size.height), duration: NSTimeInterval(duration)))
cuadradoRojo.runAction(SKAction.sequence(actionArray))
}
}
func updateWithTimeSinceLastUpdate(timeSinceLastUpdate:CFTimeInterval){
var randomNum = Double(arc4random_uniform(20))
var xTime = ((randomNum / 20) + 0.4)
lastYieldTimeInterval += timeSinceLastUpdate
if (lastYieldTimeInterval > xTime){
lastYieldTimeInterval = 0
randomNum = Double(arc4random_uniform(25))
addAlien()
}
}
override func update(currentTime: CFTimeInterval) {
var timeSinceLastUpdate = currentTime - lastUpdateTimerInterval
lastUpdateTimerInterval = currentTime
velocity = CGFloat(score*3)
if (timeSinceLastUpdate > 1){
timeSinceLastUpdate = 1/60
lastUpdateTimerInterval = currentTime
}
self.physicsWorld.gravity = CGVectorMake(0, -velocity)
updateWithTimeSinceLastUpdate(timeSinceLastUpdate)
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
if self.nodeAtPoint(location) == self.cuadradoRojo{
cuadradoRojo.removeFromParent()
}
}
}
}
Upvotes: 0
Views: 2545
Reputation: 467
Before calling addChild(cuadradoRojo), call cuadradoRojo.removeFromParent(), so you don't add it twice to its parent, SKScene
Upvotes: 2