Aidan Kaiser
Aidan Kaiser

Reputation: 521

Spritekit Swift Terminating Error

I am trying to make a new app in sprite kit with a random colored background, and all I have done is set the color of the background and creating a ball in the game but it won't launch first I'll give my code and then the error. Here is my code so far.

import SpriteKit

class GameScene: SKScene, SKPhysicsContactDelegate{

struct PhysicsCategory {
static let None      :UInt32 = 0
static let All       :UInt32 = UInt32.max
static let Triangle  :UInt32 = 0b1   //body1
static let Circle    : UInt32 = 0b10      // 2
 }

var screenWidth: CGFloat! = 0

var screenHeight: CGFloat! = 0


var circle: SKShapeNode! = SKShapeNode()
var bgColor:SKColor!
var arrayOfColors: NSMutableArray! = NSMutableArray()
var backgroundNode: SKSpriteNode! = SKSpriteNode()

override func didMoveToView(view: SKView) {
    super.didMoveToView(view)
    // Create the ball
    if screenWidth == 1024{
        circle = SKShapeNode(circleOfRadius: 22)
    }else if screenWidth == 568 || screenWidth == 480{
        circle = SKShapeNode(circleOfRadius: 11)
    }else{
        circle = SKShapeNode(circleOfRadius: 15)
    }

    circle.fillColor = SKColor.whiteColor()
    circle.alpha = 0
    circle.physicsBody = SKPhysicsBody(circleOfRadius: circle.frame.size.width/2)
    circle.physicsBody?.dynamic = true
    circle.physicsBody?.collisionBitMask = PhysicsCategory.None
    circle.physicsBody?.usesPreciseCollisionDetection = true
    circle.position = CGPoint (x: 29.952, y: 346.555)

    self.addChild(circle)

    //pick random color
    let UInt32Count = UInt32(arrayOfColors.count)
    let randomColor = arc4random_uniform(UInt32Count)
    let color: SKColor = arrayOfColors[Int(randomColor)] as SKColor

    //set random color
    let colorAction = SKAction.colorizeWithColor(color, colorBlendFactor: 1.0, duration: 1.0)
    backgroundNode.runAction(colorAction)
    backgroundNode.size = CGSize(width: 990, height: 640)

    }


 }

That is the full code so far, and here is the error.

Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array' * First throw call stack: (0x24c8d5f7 0x324ffc77 0x24ba1157 0x341ec 0x346d8 0x27fb9861 0x27fd4023 0x37888 0x379b0 0x28131c8d 0x281319fd 0x281378c7 0x2813531f 0x2819f5c1 0x283915f1 0x28393a49 0x2839e2f9 0x283922eb 0x2b4030c9 0x24c53ffd 0x24c532c1 0x24c51e1b 0x24b9eb31 0x24b9e943 0x28196127 0x28190f21 0x3a2e8 0x3a324 0x32a9baaf) libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)

Any help would be appreciated

Upvotes: 2

Views: 105

Answers (1)

gagarwal
gagarwal

Reputation: 4244

You have declared and initialized your empty array:

var arrayOfColors: NSMutableArray! = NSMutableArray()

And then you are trying accessing the empty array:

let color: SKColor = arrayOfColors[Int(randomColor)] as SKColor

This will throw an exception as you showed in console:

'* -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'

You first need to populate this array before accessing and make sure that when you generate random number, it is not out of array bounds.

Upvotes: 3

Related Questions