游戏运行,没有任何反应

时间:2015-07-07 02:38:43

标签: xcode swift

我正在尝试制作一个简单的游戏,它在随机的地方显示随机的形状,但游戏运行,没有任何反应,我不知道为什么,请帮助。 而且没有错误。 addchild()有函数,removempromparent函数有函数() 起初我没有removefromparent(),但游戏会崩溃,并会给我这个错误。

2015-07-06 22:34:26.452 Shapes[8441:4672856] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attemped to add a SKNode which already has a parent: <SKSpriteNode> name:'(null)' texture:[<SKTexture> 'square' (150 x 150)] position:{883, 768} size:{150, 100} rotation:0.00'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010d55d3f5 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x000000010f484bb7 objc_exception_throw + 45
    2   CoreFoundation                      0x000000010d55d32d +[NSException raise:format:] + 205
    3   SpriteKit                           0x000000010e059af6 -[SKNode addChild:] + 111
    4   Shapes                              0x000000010d372ae4 _TFC6Shapes9StartGame9addSquarefS0_FV12CoreGraphics7CGFloatT_ + 276
    5   Shapes                              0x000000010d372633 _TFC6Shapes9StartGame11creatShapesfS0_FT_T_ + 5267
    6   Shapes                              0x000000010d372852 _TToFC6Shapes9StartGame11creatShapesfS0_FT_T_ + 34
    7   Foundation                          0x000000010dbd3fd4 __NSFireTimer + 83
    8   CoreFoundation                      0x000000010d4c54e4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 20
    9   CoreFoundation                      0x000000010d4c50a5 __CFRunLoopDoTimer + 1045
    10  CoreFoundation                      0x000000010d4883dd __CFRunLoopRun + 1901
    11  CoreFoundation                      0x000000010d487a06 CFRunLoopRunSpecific + 470
    12  GraphicsServices                    0x00000001148f09f0 GSEventRunModal + 161
    13  UIKit                               0x000000010e1e0550 UIApplicationMain + 1282
    14  Shapes                              0x000000010d37532e top_level_code + 78
    15  Shapes                              0x000000010d37536a main + 42
    16  libdyld.dylib                       0x000000010fc73145 start + 1
    17  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)




//  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(3, 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)


        }






func creatShapes (){

    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
    square.zPosition = 10



    //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
    circle.zPosition = 10

    //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
    triangle.zPosition = 10

    //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
    rectangle.zPosition = 10


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


    switch(x){
    case 1:
        addCircle(a)
        removeCircle()
    case 2:
        addSquare(a)
        removeSquare()
    case 3:
        addRectangle(a)
        removeRectangle()
    case 4:
        addTriangle(a)
        removeTriangle()

    default:
        println("kadjaksd")

    }
     println(x)
    println(a)


}

//adding functions
func addCircle(rand : CGFloat)
{
    var a = rand
    circle.position = CGPoint (x: a , y: self.frame.size.height)
    self.addChild(circle)
}

func addSquare(rand : CGFloat)
{
    var a = rand
    square.position = CGPoint(x: a , y: self.frame.size.height)
    self.addChild(square)
}

func addRectangle(rand : CGFloat)
{
    var a = rand
    rectangle.position = CGPoint(x:  a , y: self.frame.size.height)
}

func addTriangle(rand : CGFloat)
{
    var a = rand
    triangle.position = CGPoint(x: a , y: self.frame.size.height)
}

//removing functions

func removeCircle()
{
    self.circle.removeFromParent()
}
func removeSquare()
{
    self.square.removeFromParent()
}
func removeRectangle()
{
    self.triangle.removeFromParent()
}
func removeTriangle()
{
    self.triangle.removeFromParent()
}


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)
    }


}

}

1 个答案:

答案 0 :(得分:2)

问题是你要两次添加一个节点。这就是为什么它说&#34;尝试添加已经拥有父节点的节点。&#34;你可能有两次这样的事情:

self.addChild(square)

您只能添加一个节点。问题是您将square声明为全局变量,因此每次运行createShapes函数时都会添加它。要解决此问题,请将square声明为局部变量,以便每次运行函数时都创建新引用,而不是只有一个引用。如果您遇到其他节点的此问题,您可以采用相同的方式修复它。计时器每三秒运行一次creteShapes,这就是你需要将square声明为局部变量的原因。快乐的编码。