一个场景中有多少个SKNode?为什么我的表现如此糟糕?

时间:2015-01-05 18:27:01

标签: swift sprite-kit

我正在开发一款纸牌游戏,52张卡片,20张可见,剩下的就是#34;堆叠"。没有物理,没有什么特别的,只有带PNG图像的SKSpriteNodes。我在iMAC和iPhone 5S上以90%的CPU运行它"感觉"就像一秒钟,直到有接触的反应。 在一个场景中拥有如此多的SKSpriteNodes是一个问题吗?或者.22的scaleFactor可能是问题吗?

我的错误在哪里?

我在这里布局卡片

class PlayingCardView : SKSpriteNode {

    init(imageNamed: String, cardIndex: Int) {
        let cardTexture = SKTexture(imageNamed: imageNamed)
        let cardScaleFactor:CGFloat = 0.22  
        let cardSize = CGSizeMake(cardTexture.size().width * cardScaleFactor,cardTexture.size().height * cardScaleFactor)
        super.init(texture: cardTexture, color: nil, size: cardSize)
        self.name = "\(cardIndex)"
    }
}

// MARK: UpdateUI
func updateUI() {
        assert(game.cards.count > 0, "No Cards")
        if status == GameIs.Paused {return}
        removeAllCards()
        PlayingCardViews.removeAll(keepCapacity: false)

        var scaleFactor:CGFloat = 0.8
        var upOffset:CGFloat = 0.0
        var cardsLeft = 0

        // create spritenotes for all 52 cards
        for i in 0..<game.cards.count {
            let card = game.cardAtIndex(i)
            // all cards until cardPositions.count face up. the rest on the stack
            var (x,y) = (7,6)
            if i<cardPositions.count {
                (x,y) = cardPositions[i]
            }
            // determine real position from the grid information
            let xPos:CGFloat = realXForPositionX(x)
            let yPos:CGFloat = realYForPositionY(y)
            let image =  backgroundImageForCard(card)
            // we save the currents positon in the Model
            card.positionGrid = (x,y)
            let cardSprite = PlayingCardView(imageNamed: backgroundImageForCard(card), cardIndex:i)
            PlayingCardViews.append(cardSprite) 
            if !card.removed {
                cardSprite.position = CGPointMake(realXForPositionX(x),realYForPositionY(y))  
                cardLayer.addChild(cardSprite)
                ++cardsLeft  
            }
        }
        if cardsLeft == 0 { gameOver(0, timeLeft: secondsLeft) }
    }

1 个答案:

答案 0 :(得分:1)

好的,我刚刚注意到你在添加所有精灵并在Update方法中设置你的场景!你应该创建一个setup(){}方法,添加精灵和设置,否则添加卡片每秒被调用60次!

从didMoveToView(仅调用一次)调用您的自定义安装方法,然后只调用代码来移动或更新游戏玩法所需的卡片。