1
votes

I'm using SKAction to scale a node. After about 2 minutes of game play the node disappears. Basic gameplay is you have to keep touching the node to shrink it otherwise it grows. So you can't play if it's not there. I'm not sure why the node disappears now.

code that creates node :

node.create(mainScreenview, position: CGPoint(x: frame.size.width / 2, y: frame.size.height / 2), size: CGSize(width: 150, height: 150), color: greyColour, name: "Test")


func nodeAction() {
    node.runAction(SKAction.scaleBy(10.0, duration: 8.5))
}

Function to compare correct answer

 func compareNodes() {

if !nodesToCheck.isEmpty {
  let checkThisNode = nodesToCheck[0] as! SKSpriteNode
  if checkThisNode.name == node.name {
    node.physicsBody = nil
    node.removeAllActions()

    score += 1
    nodesToCheck.removeAll()
    changeSizeToSmall()


  } else {
    gameOver()
  }
}

}

func changeSizeToSmall() {

'node.size = CGSize(width: 60, height: 60)'

now SKAction.resizeToWidth(60, height: 60, duration: 1.0))

The error message for this line

node.physicsBody = SKPhysicsBody(circleOfRadius: node.size.width / 2)

Thread1: EXC_BAD_ACCESS (code=, address= 0x0)

and always highlights the node's physicsbody. There is nothing between these lines and the rest of the node code

    node.physicsBody?.affectedByGravity = false
    node.physicsBody?.categoryBitMask = PhysicsCategory.node.rawValue
    node.physicsBody?.contactTestBitMask = PhysicsCategory.bird.rawValue
    node.physicsBody?.collisionBitMask = 0
}

The app crashes with that error. The memory usage doesn't change too much during the game but it does increase. I've commented out the SKAction line and I can play the game for as long as I like. So I understand that Shrinking the node and adding a physics body uses a lot of memory but I don't know what to do instead. I've tried removing the node after each time it's called and recreating it again. Tried debugging with the instruments to see what is being dealloc, looking for reference cycles but I'm really not sure what I'm looking for.

What can I do to stop this from happening?

Thanks for any help

1
When are nodeAction and changeSizeToSmall called? - Ali Beadle
the nodeAction gets called every time after you press the node it checks to see if the previous node you touched was correct if it is correct, It calls changeSizeTosmall() So that it creates a growing and shrinking effect. if it's wrong it just continues to grow. - sdIos
So nodeAction gets called every time you touch a node? But it has a duration of 8.5 seconds. How frequently are you touching the node when the game is playing? More often than once per 8.5 seconds? - Ali Beadle
Yes I remove the action from the node if it is correct and change the size to small add the physics body back on and run the action on the node again. So it's being called every few seconds / a lot if the answer is correct. There is only 1 node that is growing and shrinking but there are other spritenodes that are being touched in the scene. - sdIos
I see. It is difficult to see what is going on with just those little bits of code. Can you provide some more of your code? In particular is the line that crashes within changeSizeToSmall and is there anything else before it? Also you say that the node disappears and the app crashes - do these happen at the same time? If not how long after the node disappears before the crash? Does it always crash after 2 minutes or just sometimes? - Ali Beadle

1 Answers

1
votes

I changed the node's size property so that it would be changed by an SKAction instead this seems to work.

SKAction.resizeToWidth(60, height: 60, duration: 1.0))