gpt4 book ai didi

ios - Sprite Kit 中的 iCarousel

转载 作者:可可西里 更新时间:2023-11-01 00:59:17 29 4
gpt4 key购买 nike

解释

我正在尝试构建一个类似于 Crossy Road 的角色选择菜单(如您所见 here )。所以我找到了这个 iCarousel ,这对我有帮助,但我读到的所有内容都在谈论如何将它实现到 ViewController,这不是我的情况。我正在使用 GameScene 但我没有发现任何关于它的内容。无论如何我可以将它实现到我的游戏中吗?甚至另一种类似于我上面提到的角色选择菜单的效果?


尝试(beyowulf)

可以下载here .


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


提前致谢,
路易斯。

最佳答案

您可以使用 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()
}

您还需要在 View Controller 被释放之前移除观察。

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

}

如果你想改变命中检测,你需要子类化你需要改变的 View 。本例是您的 iCarousel View 。

然后您可以覆盖 hitTestpointInside。我创建了一个 iCarousel 子类并覆盖了 pointInside 以仅当该点位于轮播的 contentView 的 subview 之一内时才返回 true。

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

您需要记住在界面构建器中更改轮播的类并更新您的 socket 。

关于ios - Sprite Kit 中的 iCarousel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37756502/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com