jeudi 2 juillet 2015

terminating with uncaught exception of type NSException, game error with swift

The purpose for this code is to do random number to display random shapes but the simulator displays them in the same spot which is the bottom left corner of the screen , for some reason the random for the position which is ( a ) is not working

Code:

//
//  StartGame.swift
//  Shapes
//
//  Created by naeim on 6/29/15.
//  Copyright (c) 2015 naeim. All rights reserved.

import UIKit
import SpriteKit
import Darwin




class StartGame: SKScene {


var scoreLabel = SKLabelNode(fontNamed: "cholkDuster")
var square = SKSpriteNode(imageNamed: "square")
var circle = SKSpriteNode(imageNamed: "circle")
var rectangle = SKSpriteNode(imageNamed: "rectangle")
var triangle = SKSpriteNode(imageNamed: "triangle")
let bg = SKSpriteNode(imageNamed: "background.png")
var score = 0




override func didMoveToView(view: SKView) {



    /* Setup your scene here */
    // ********************
    //declaring the shapes
    // ********************



             //random number for the shapes



    var timecreatShapes = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("creatShapes"), userInfo: nil, repeats: true)



    //background image


    bg.position = CGPoint(x: CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame))
    bg.size.width = self.frame.size.width
    bg.size.height = self.frame.size.height
    self.addChild(bg)



    self.scoreLabel.text = "0"
    self.scoreLabel.fontSize = 42
    self.scoreLabel.position = CGPoint(x: CGRectGetMidX(self.frame) , y: CGRectGetMidY(self.frame))
    self.addChild(scoreLabel)
    scoreLabel.zPosition = 2



    self.physicsWorld.gravity = CGVectorMake(0, -0.5)

    //declaring a square image

    square.size = CGSize(width: 150, height: 100)
    square.color = SKColor.redColor()
    square.physicsBody = SKPhysicsBody(circleOfRadius: square.size.height / 2)
    square.physicsBody?.dynamic = true
    square.physicsBody?.allowsRotation = false



    //declaring a circle image

    circle.size = CGSize(width: 150, height: 100)
    circle.physicsBody = SKPhysicsBody(circleOfRadius: square.size.height / 2)
    circle.physicsBody?.dynamic = true
    circle.physicsBody?.allowsRotation = false

    //declaring a triangle

    triangle.size = CGSize(width: 150, height: 100)
    triangle.physicsBody = SKPhysicsBody(circleOfRadius: square.size.height / 2)
    triangle.physicsBody?.dynamic = true
    triangle.physicsBody?.allowsRotation = false

    //declaring rectangle

    rectangle.size = CGSize(width: 150, height: 100)
    rectangle.physicsBody = SKPhysicsBody(circleOfRadius: square.size.height / 2)
    rectangle.physicsBody?.dynamic = true
    rectangle.physicsBody?.allowsRotation = false


        }






func creatShapes (){

    var x = Int ( arc4random_uniform(4) + 1)
    var a = CGFloat ( arc4random_uniform(1000) + 1)


    switch(x){
    case 1:
        circle.position = CGPoint (x:  a , y: self.frame.size.height)
        self.addChild(SKSpriteNode(imageNamed:"circle"))
    case 2:
        square.position = CGPoint(x:  a , y: self.frame.size.height)
        self.addChild(SKSpriteNode(imageNamed:"square"))
    case 3:
        rectangle.position = CGPoint(x:  a , y: self.frame.size.height)
        self.addChild(SKSpriteNode(imageNamed:"rectangle"))
    case 4:
        triangle.position = CGPoint(x: a , y: self.frame.size.height)
        self.addChild(SKSpriteNode(imageNamed:"triangle"))

    default:
        break

    }
    println(x)
    println(a)


}




override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    /* Called when a touch begins */

    for touch: AnyObject in touches {
        let location = touch.locationInNode(self)
       if (self.nodeAtPoint(location) == self.square || self.nodeAtPoint(location) == self.triangle || self.nodeAtPoint(location) == self.circle || self.nodeAtPoint(location) == self.rectangle){

        self.score++

        }
        self.scoreLabel.text = String(self.score)
    }


}

}




Aucun commentaire:

Enregistrer un commentaire