Display levels using SpriteKit / Views or ViewController

I am making a game in Swift and want the user to be able to select a level (click on it) to play that particular level. Each level is a window on the screen with the level number in it. Thus, if there were 20 levels in the area, you will see 20 boxes on the screen with level # and the name and number of stars that you have reached. I am confused about the best approach to this. I see two ways:

  • You create a view manager that has a UICollectionView, and I pass the levels to it as a data source, and also handle the on tap event. When you click on it, I can make presentScene into the corresponding game scene (SKScene) to process a normal game drawing.

  • I visualize the levels directly as SKSpriteNodes (boxes for levels) on the screen using the for .. loop and execute addChild and set the position of each sprite. Since I could have 20 levels, I would draw ~ 5 per line, since I assume that this will fit most devices from the emulators I tried. It looks like I'm recreating what the UICollectionView does automatically, and besides, I need to do a trick, such as positioning and offsets, etc.

Right now I’m doing approach # 2 above, because it seems that most Swift learning games do not use bulletin boards or presentation controllers, but instead just focus on creating everything using SKScenes, and have one presentation controller, whose task is simply to present one scene . So, as I found out, I went in that direction.

What is the right approach?

Thank!

+4
source share
1 answer

Any of your methods will work, but you will need to decide which method is best for your particular scenario, so the “right approach” is up to you. I had the same question about six months ago, and the following solution worked flawlessly for me:

SKSpriteNodes SKScene

, SKNode, SpriteKitButton, , SKSpriteNode UIButton SpriteKit, .

, , UIButton, SKScene, .

SKScenes , UIViewControllers UIViews SKScenes.

, , self.view?.presentScene... , , .

:

1) :

import SpriteKit

class SpriteKitButton: SKNode {

    var button: SKSpriteNode!
    var buttonUnpressedTexture: SKTexture!
    var buttonPressedTexture: SKTexture!
    var action: (String) -> Void

    init(buttonUnpressedImage: String, buttonPressedImage: String, buttonAction: (String) -> Void) {

        //Initialize your button here
    }

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

    }

    override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {

    }

    override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {

    }
}

2) SKScene :

    let artboardButton = SpriteKitButton(buttonUnpressedImage: "artboardButtonUnpressed.png", buttonAction: openArtboard)
    artboardButton.position = CGPoint(x: ..., y: ...)
    artboardButton.zPosition = 0.0
    artboardButton.userInteractionEnabled = true
    self.addChild(artboardButton)

    //Add a function to handle what to do when the button is pressed
    func openArtboard() {

    }

touches SKNode, , , , . , , .

for 20 .

willMoveFromView, .

+1

Source: https://habr.com/ru/post/1629092/


All Articles