[ios]一些在Sprite工具包

标签: sprite-kit ios Swift
发布时间: 2017/4/3 19:34:00
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

解释

我试图建立一个字符选择菜单类似于 Crossy 道路之一 (正如你可以看到在这里)。所以找了这一些,这将有助于我所有这些,但我读到的一切都是谈实施它到 ViewController ,这不是我的情况。我使用的 GameScene ,我没有发现任何谈论这件事。有什么我可以实现它对我的游戏吗?或甚至另一个效果相似字符选择菜单上述吗?


尝试 (beyowulf)

您可以下载它在这里


GameScene.swift

import SpriteKit

class GameScene: SKScene {

    var show = SKSpriteNode()
    var hide = SKSpriteNode()

    func showCharPicker(){
        NSNotificationCenter.defaultCenter().postNotificationName("showCharPicker", object: nil)
    }
    func hideCharPicker(){
        NSNotificationCenter.defaultCenter().postNotificationName("hideCharPicker", object: nil)
    }

    override func didMoveToView(view: SKView) {
        /* Setup your scene here */

        print("didMoveToView")

        show = SKSpriteNode(imageNamed: "show")
        show.anchorPoint = CGPointZero
        show.position = CGPointZero
        addChild(show)

        hide = SKSpriteNode(imageNamed: "hide")
        hide.anchorPoint = CGPointZero
        hide.position = CGPoint(x: self.frame.width / 2 - hide.frame.width / 2, y: 0)
        addChild(hide)
    }

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

        for touch in touches{

            let location = touch.locationInNode(self)
            let node = nodeAtPoint(location)

            if node == show{
                print("show")
                showCharPicker()
            }
            else if node == hide{
                print("hide")
                hideCharPicker()
            }
        }
    }
}

GameViewController.swift

import UIKit
import SpriteKit

class GameViewController: UIViewController, iCarouselDataSource, iCarouselDelegate{

    var squaresArray : NSMutableArray = NSMutableArray()

    @IBOutlet weak var carousel: iCarousel!

    deinit{
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }

    func showCarousel(){
        self.carousel.hidden = false
    }
    func hideCarousel(){
        self.carousel.hidden = true
    }

    override func viewDidLoad(){
        super.viewDidLoad()

        // Configure iCarousel
        carousel.dataSource = self
        carousel.delegate = self
        carousel.type = .CoverFlow
        carousel.reloadData()

        self.carousel.hidden = true

        // Register showCarousel and hideCarousel functions
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.showCarousel), name: "showCharPicker", object: nil)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.hideCarousel), name: "hideCharPicker", object: nil)

        // Configure view
        let skView = SKView()
        self.view.insertSubview(skView, belowSubview: self.carousel)
        skView.frame = self.view.bounds

        // Additionals
        skView.showsFPS = true
        skView.showsNodeCount = true
        skView.ignoresSiblingOrder = true

        // Configure scene
        let scene = GameScene(size:self.view.bounds.size)
        scene.scaleMode = .ResizeFill
        scene.size = self.view.bounds.size

        skView.presentScene(scene)
    }

    //iCarousel
    override func awakeFromNib(){
        super.awakeFromNib()
        squaresArray = NSMutableArray(array: ["square1","square2","square3"])
    }
    func numberOfItemsInCarousel(carousel: iCarousel) -> Int{
        return squaresArray.count
    }
    func carousel(carousel:iCarousel, didSelectItemAtIndex index:NSInteger){
        //self.hideCarousel()
    }
    func carousel(carousel: iCarousel, viewForItemAtIndex index: Int, reusingView view: UIView?) -> UIView{

        var itemView: UIImageView

        if (view == nil){
            itemView = UIImageView(frame:CGRect(x:0, y:0, width:200, height:200))
            itemView.contentMode = .Center
        }
        else{
            itemView = view as! UIImageView;
        }

        itemView.image = UIImage(named: "\(squaresArray.objectAtIndex(index))")
        return itemView
    }
    func carousel(carousel: iCarousel, valueForOption option: iCarouselOption, withDefault value: CGFloat) -> CGFloat{

        if (option == .Spacing){
            return value * 2
        }
        return value
    }
}

发生了什么事情:

enter image description here


提前谢谢,
路易斯。

解决方法 1:

你可以使用 NSNotifications 来显示你性格选取器。你只需要观察由张贴通知你 SKScene 。你 viewDidLoad 看起来应该类似︰

override func viewDidLoad(){
    super.viewDidLoad()

    carousel.type = .CoverFlow
    carousel.reloadData()

    let spriteKitView = SKView()
    spriteKitView.frame = self.view.bounds
    self.view.insertSubview(spriteKitView, belowSubview: self.carousel)

    spriteKitView.showsFPS = true
    spriteKitView.showsNodeCount = true
    spriteKitView.ignoresSiblingOrder = true

    self.gameScene = GameScene(size:self.view.bounds.size)
    self.gameScene.scaleMode = .AspectFill
    self.gameScene.imageName = self.images[0] as! String

    self.carousel.hidden = true
    spriteKitView.presentScene(self.gameScene)

    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.showCarousel), name: gameScene.kShowNotification, object: nil)
}

你想要执行 carousel(carousel:iCarousel, didSelectItemAtIndex index:NSInteger) 所以你知道什么选择,于是你可以返回到玩游戏。例如︰

func carousel(carousel:iCarousel, didSelectItemAtIndex index:NSInteger)
{
    self.gameScene.imageName = self.images[index] as! String
    self.hideCarousel()
}

你还需要移除观察之前释放您的视图控制器。

deinit
{
    NSNotificationCenter.defaultCenter().removeObserver(self)
}

SKScene 然后可以投递的通知︰

import SpriteKit

class GameScene: SKScene {
    var imageName = "square1"{
        didSet{
            self.hidden = false
            self.childNode.texture = SKTexture(imageNamed: imageName)
        }
    }

    let kShowNotification = "showPicker"

    var childNode = SKSpriteNode()
    override func didMoveToView(view: SKView) {
        /* Setup your scene here */

        self.childNode = SKSpriteNode(imageNamed: imageName)
        self.childNode.anchorPoint = CGPointZero
        self.childNode.position = CGPointZero
        self.addChild(self.childNode)
    }

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

    func showCharPicker()
    {
        self.hidden = true
        NSNotificationCenter.defaultCenter().postNotificationName(kShowNotification, object: nil)
    }

}

如果你想要改变命中的检测,你需要子类化,你需要它来更改视图。这种情况下你 iCarousel 视图。

然后可以重写 hitTestpointInside 。我创建了 iCarousel 子类,并超越了 pointInside 以只返回 true,当该点在内部的旋转木马上之一 contentView 的子视图。

class CarouselSubclass: iCarousel {

    override func pointInside(point: CGPoint, withEvent event: UIEvent?) -> Bool {
        var inside = false
        for view in self.contentView.subviews
        {
            inside = CGRectContainsPoint(view.frame, point)
            if inside
            {
                return inside
            }
        }
        return inside
    }
}

你需要记住更改您轮播界面生成器中的类和更新以及你插座。

官方微信
官方QQ群
31647020