【问题标题】:SpriteKit node not being removed from parentSpriteKit 节点未从父节点中移除
【发布时间】:2016-01-23 00:25:31
【问题描述】:

我有一个问题,为什么我的 SpriteKit 节点没有从下面的代码中的父节点中删除。

我的项目目前分为两部分,一个是构建 SpriteKit 项目时默认的 GameScene 类,另一个是我制作的用于对游戏中的每个圆圈进行操作的 Circles 类。

这些圆圈在这里存储在一个数组中:

var circlesInPlay = [Circles]()

基本上,我正在尝试设计一个简单的游戏,其中圆圈的大小会减小,当它们的宽度为 0 时,会从屏幕上移除。

我的代码在这里,在

override func update(currentTime: CFTimeInterval) {


    timeSinceUpdate = timeSinceUpdate + 1
    print("time since update: " + String(timeSinceUpdate))

    if timeForUpdate == timeSinceUpdate {
        newCircle()
        timeSinceUpdate = 0
        //timeForUpdate = Int(arc4random_uniform(100) + 1)
        timeForUpdate = 100
    }

    //Check to see if circle transparancies are 0

    if checkGameOver() {

        gameOver()
    }

    updateCircles()
    removeCircles()

timeSinceUpdate 变量是自最后一个圆圈被放置到屏幕上的时间,每更新一帧,该时间就增加一。

updateCircles() 调用这个,

func updateCircles() {

    for c in circlesInPlay {

        c.update()

    }

}

在我在另一个 swift 文件中创建的 Circles 类中调用 update():

    func update() {

    transparancy = transparancy - transparancyDecrease
    size = size - ds

    if (size <= 0) {
        node.removeFromParent()
    }

    node.size.height = size
    node.size.width = size

}

更新函数对 removeCircles() 的另一个调用在这里

func removeCircles() {

    var posn = circlesInPlay.count - 1

    for c in circlesInPlay {

        if (c.size < 0) {

            c.produceNode().removeFromParent()
            circlesInPlay.removeAtIndex(posn)
            posn = posn - 1

        }

    }

}

我真正想说的是,我不知道为什么圆圈有时会卡住或没有从屏幕上删除。

非常感谢任何帮助!

整个游戏代码如下:

import SpriteKit

class GameScene: SKScene {

    var bgImage = SKSpriteNode(imageNamed: "background.png")

    let screenSize = UIScreen.mainScreen().bounds

    var timeSinceUpdate: Int = 0

    var circlesInPlay = [Circles]()

    var timeForUpdate = Int(arc4random_uniform(200) + 1)

    override func didMoveToView(view: SKView) {

        bgImage.position = CGPointMake(self.size.width/2, self.size.height/2)
        bgImage.zPosition = -100
        self.addChild(bgImage)






    }

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
       /* Called when a touch begins */

        for touch in touches {
            let location = touch.locationInNode(self)

            if !isTouchInCircle(location) {

                gameOver()
            }


        }
    }

    override func update(currentTime: CFTimeInterval) {
        /* Called before each frame is rendered */

        timeSinceUpdate = timeSinceUpdate + 1
        print("time since update: " + String(timeSinceUpdate))

        if timeForUpdate == timeSinceUpdate {
            newCircle()
            timeSinceUpdate = 0
            //timeForUpdate = Int(arc4random_uniform(100) + 1)
            timeForUpdate = 100
        }

        //Check to see if circle transparancies are 0

        if checkGameOver() {

            gameOver()
        }

        updateCircles()
        removeCircles()

    }

    func newCircle(){

        let sizeX = UInt32(CGRectGetMaxX(self.frame))
        let randomx = CGFloat(arc4random_uniform(sizeX))

        let sizeY = UInt32(CGRectGetMaxY(self.frame))
        let randomy = CGFloat(arc4random_uniform(sizeY))

        //let randomx = CGFloat(arc4random_uniform(UInt32(self.size.width)))
        //let randomy = CGFloat(arc4random_uniform(UInt32(self.size.height)))

        if (circlesInPlay.count < 5) {

            circlesInPlay.append(Circles.init(x: randomx, y: randomy))
            self.addChild((circlesInPlay[circlesInPlay.count - 1]).produceNode())
        }

    }

    func checkGameOver() -> Bool {


        for c in circlesInPlay {

            if c.getTransparancy() == 0 {

                return false

            }

        }

        return true
    }

    func isTouchInCircle(touch: CGPoint) -> Bool {

        for c in circlesInPlay {

            if (c.getX() <= (touch.x * 1.10)) {

                return true
            }

        }

        return false

    }


    func updateCircles() {

        for c in circlesInPlay {

            c.update()

        }

    }

    func removeCircles() {

        var posn = circlesInPlay.count - 1

        for c in circlesInPlay {

            if (c.size < 0) {

                c.produceNode().removeFromParent()
                circlesInPlay.removeAtIndex(posn)
                posn = posn - 1

            }

        }

    }


    func gameOver() {


    }

}



import Foundation
import SpriteKit
import GameKit


class Circles {


    var node = SKSpriteNode()

    var size: CGFloat = 200
    var ds: CGFloat = 2

    var colorC: String;


    var xpos: CGFloat
    var ypos: CGFloat

    var transparancy: Int
    var transparancyDecrease: Int = 1



    let arrayColors = ["circleRed.png", "circleBlue.png", "circlePink.png", "circleGreen.png", "circleYellow.png"]


    //The innitial constructor
    init(x: CGFloat, y: CGFloat) {

        self.xpos = x
        self.ypos = y

        self.transparancy = 100

        let randomIndex = Int(arc4random_uniform(UInt32(arrayColors.count)))

        colorC = arrayColors[randomIndex]

        node.texture = SKTexture(imageNamed: colorC)
        node.position = CGPointMake(self.xpos, self.ypos)
        node.size.height = self.size
        node.size.width = self.size

    }

    func produceNode() -> SKSpriteNode {

        return node

    }


    func setColor() {

        let randomIndex = Int(arc4random_uniform(UInt32(arrayColors.count)))

        colorC = arrayColors[randomIndex]



    }


    func update() {

        transparancy = transparancy - transparancyDecrease
        size = size - ds

        if (size <= 0) {
            node.removeFromParent()
        }

        node.size.height = size
        node.size.width = size

    }

    func getX() -> CGFloat {

        return xpos
    }

    func getY() -> CGFloat {

        return ypos
    }


    /*
    func getColor() -> SKColor {

        return colorC

    }*/

    func getTransparancy() -> Int {

        return transparancy

    }

}

【问题讨论】:

  • 在 removeCircles() 方法中,如果 c.size

标签: ios sprite-kit


【解决方案1】:

问题出在这个 sn-p 中:

        var posn = circlesInPlay.count - 1

    for c in circlesInPlay {

        if (c.size < 0) {

            c.produceNode().removeFromParent()
            circlesInPlay.removeAtIndex(posn)
            posn = posn - 1

        }

    }

您总是从集合中删除最后一个元素,而不是您在迭代中使用的元素。在迭代时更改集合时也要小心。如果您从集合中删除元素,索引将会更改。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-22
    • 1970-01-01
    • 1970-01-01
    • 2016-02-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多