Sprite 套件中的 iCarousel

2024-01-03

解释

我正在尝试构建一个类似于 Crossy Road 的角色选择菜单(如您所见here http://www.therockstargamers.com/wp-content/uploads/2015/04/crossy-road-character-select.png)。所以我找到了这个轮播 https://github.com/nicklockwood/iCarousel,这对我来说是有帮助的,但是我读到的所有内容都是关于将其实现为ViewController,这不是我的情况。我在用着GameScene我没有找到任何谈论它的内容。我可以将其应用到我的游戏中吗?或者甚至是类似于我上面提到的角色选择菜单的另一种效果?


尝试(贝尤武夫)

你可以下载它here https://www.dropbox.com/sh/d62tx0q305gjhcz/AACPHvNCAkO3SrYcFQhGkkhsa?dl=0.


游戏场景.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
    }
}

发生了什么:


提前致谢,
Luiz.


您可以使用 NSNotifications 来显示您的角色选择器。您只需要关注您发布的通知即可SKScene. Your 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)
}

Your 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 view.

然后您可以覆盖hitTest or pointInside。我创建了一个iCarousel子类和覆盖pointInside仅当该点位于旋转木马之一内时才返回 truecontentView的子视图。

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

您需要记住在界面生成器中更改轮播的类并更新您的插座。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Sprite 套件中的 iCarousel 的相关文章

  • 错误消息:您输入的捆绑包 ID 已被使用

    我正在尝试发布一个 iPhone 应用程序 这不是第一个 我过去已经发表过其他的 因此 我在第一个和第二个表单中输入了所需的信息 然后填写了第三个大表单 您还可以在其中上传图标和屏幕截图 好吧 我在上传屏幕截图之前按下了 保存 按钮 因为我
  • 在 iOS 应用程序中拨打电话

    我有一些代码尝试在应用程序中进行调用 但它似乎不起作用 UIApplication myApp UIApplication sharedApplication NSString theCall NSString stringWithForm
  • Swift:设置协议的可选属性

    如何设置协议的可选属性 例如 UITextInputTraits 有许多可选的读 写属性 当我尝试以下操作时 出现编译错误 无法分配给 textInputTraits 中的 keyboardType func initializeTextI
  • jQuery:离线后 POST 出错(iOS 和 Chrome)

    我构建了一个具有离线功能的 HTML5 Web 应用程序 使用 AppCache 程序流程为 Online 在网络上时 应用程序预加载一些基本信息 工作 Offline 用户拿着装有应用程序的平板电脑offline 然后在应用程序上执行他们
  • $0 和 $1 在 Swift 闭包中意味着什么?

    let sortedNumbers numbers sort 0 gt 1 print sortedNumbers 谁能解释一下什么 0 and 1在斯威夫特中意味着什么 另一个样本 array forEach actions append
  • CATextLayer 上 iOS 6 中不需要的垂直填充

    背景 我在 iOS 5 中开始了我的项目 并构建了一个带有图层的漂亮按钮 我在按钮上添加了一个 textLayer 并使用以下代码将其居中 float textLayerVerticlePadding self bounds size he
  • 关闭捕获上下文 Swift

    当我尝试更改闭包中的变量时出现此错误 A C function pointer cannot be formed from a closure that captures context 是否有解决方法或者仍然可以更改闭包内的变量 My C
  • 当应用程序进入前台时,如何重新启动基于块的动画?

    我有以下基于块的动画 UIView animateWithDuration 0 5f delay 0 0f options UIViewAnimationOptionRepeat UIViewAnimationOptionAutorever
  • 根据内容自动更改单元格高度 - Swift

    在 Swift 中使用 UITableView 有人可以帮我根据标签 图片和描述自动更改单元格的高度吗 所有信息都正确传递 我只需要帮助格式化它 我尝试使用调整它cell frame size height 但这没有效果 我可以更改故事板中
  • 防止 iOS 键盘在 cordova 3.5 中滚动页面

    我正在使用 Cordova 3 5 和 jQuery mobile 构建 iOS 应用程序 我在大部分应用程序中禁用了滚动功能 但是 当我选择输入字段时 iOS 键盘会打开并向上滚动页面 我不想要这个功能 由于输入足够高 键盘不会覆盖它 我
  • 通过 Button Swift 中的标签发送行和部分

    我里面有这个cellForRowAtIndexPath cell plusBut tag indexPath row cell plusBut addTarget self action plusHit forControlEvents U
  • Xcode 异步单元测试在主线程上等待

    我正在尝试使用 Xcode 中的单元测试来测试一些异步代码 但主线程被阻塞 问题在于 某些正在测试的代码期望从 iOS 类 AVFoundation 接收回调 但是 AVFoundation 类似乎只会在主线程上回调 问题是 如果我正在进行
  • 在 SwiftUI 中使用可观察对象切换视图

    我正在练习尝试使用 SwiftUI 中的可观察对象切换视图 但我的代码无法正常工作 我知道我可以用 State 来做到这一点 但我想用可观察的对象来实现这一点 当我单击内容视图中的图像时 图像不会改变 有人能帮我吗 内容视图 swift i
  • 诊断和仪器均缺少“僵尸”选项

    运行 Xcode 4 0 2 Zombie 选项丢失 其他 SO 帖子建议找到它的两个地方 Product gt Run looks like this Product gt Profile looks like this 奇怪的是 我之前
  • Mac 上的 Delphi - 可能吗? [关闭]

    Closed 此问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 我负责一个 Delphi Win32 项目管理应用程序 我刚刚完成了向 Delphi 2009 的迁移
  • 在 Object 子类及其自己的子类上实现ignoreProperties()

    我是领域新手 我正在使用继承自 Object 的基类以及该基类的自定义子类创建模型 我的模型要求基类通过覆盖静态来声明一些属性被忽略ignoredProperties 方法 当尝试在某些基类子类上重写该方法时 我收到一个 Swift 编译器
  • 使用强光混合模式时突出显示伪影

    我正在 iPhone 应用程序中使用顶部图像的 HardLight 混合模式混合两个图像 它看起来像这样 UIGraphicsBeginImageContext size sourceImage drawInRect rectangle b
  • 上传存档错误:“缺少 iOS 发行版签名身份......”

    我正在尝试使用 Xcode 将我的 iOS 应用程序存档上传到 iTunes Connect 但是当我单击 上传到 App Store 时 出现错误 Xcode 尝试查找或生成匹配的签名资产并 由于以下问题未能做到这一点 缺少 iOS 为
  • Unwind segue 的用途是什么以及如何使用它们?

    iOS 6 和 Xcode 4 5 有一个称为 Unwind Segue 的新功能 展开转场可以允许过渡到故事板中场景的现有实例 除了 Xcode 4 5 发行说明中的 这个简短条目之外 UIViewController 现在似乎还有几个新
  • Fitbit oauth2 公共 API 停止工作。给出错误 - 抱歉,这不是你..是我们

    几个月前 我准备了一个关于 Fitbit oauth2 公共 API 的演示 其中我使用特定用户登录并获取他的活动 一切正常 但最近 我打开演示并尝试使用同一用户登录 但它没有登录并反复出现此错误 我尝试更改在 Fitbit 上注册的演示应

随机推荐