gpt4 book ai didi

ios - 在 SpriteKit 中创建按钮 : Swift

转载 作者:搜寻专家 更新时间:2023-10-30 22:04:00 24 4
gpt4 key购买 nike

我想在 SpriteKitSKScene 中创建一个按钮,将 View 发送到另一个 View Controller 。

我尝试使用“带标识符的 performSegue”,但显然 SKScene 不支持此功能。我如何使用 SpriteKit 创建一个将 View 发送到另一个 View 的按钮?

这是我尝试用来执行此操作的代码。

带有“HomeButton.prepareForSegueWithIdentifier()”的行只是一个例子。它实际上不会让我添加“prepareForSegue”部分,它不支持它 <--- 我的意思是当我去添加它时,它无法识别。

class GameOverScene: SKScene {

var HomeButton: SKNode! = nil


init(size: CGSize, won: Bool) {
super.init(size: size)

backgroundColor = SKColor.whiteColor()

HomeButton = SKSpriteNode(color: SKColor.blueColor(), size: CGSize(width: 100, height: 100))
HomeButton.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
HomeButton.userInteractionEnabled = true
self.addChild(HomeButton)

let message = won ? "You Won!" : "You Lose!"





let label = SKLabelNode(fontNamed: "Title 1")
label.text = message
label.fontSize = 40
label.fontColor = SKColor.blackColor()
label.position = CGPoint(x: size.width/2, y: size.height/2)
addChild(label)

runAction(SKAction.sequence([SKAction.waitForDuration(3.0), SKAction.runBlock() {
let reveal = SKTransition.flipHorizontalWithDuration(0.5)
let scene = GameScene(size: size)
self.view?.presentScene(scene, transition: reveal)
}
]))


}

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)

if HomeButton.containsPoint(location) {

HomeButton.prepareForSegueWithIdentifier()

}
}
}

注意:我试过使用按钮,但它们在 SKScene 中不起作用。

如果有任何混淆,我会继续回复。

最佳答案

我已经翻译了 Alessandro Ornano 对 Swift 3.1 的回答:

import SpriteKit

class FTButtonNode: SKSpriteNode {

enum FTButtonActionType: Int {
case TouchUpInside = 1,
TouchDown, TouchUp
}

var isEnabled: Bool = true {
didSet {
if (disabledTexture != nil) {
texture = isEnabled ? defaultTexture : disabledTexture
}
}
}
var isSelected: Bool = false {
didSet {
texture = isSelected ? selectedTexture : defaultTexture
}
}

var defaultTexture: SKTexture
var selectedTexture: SKTexture
var label: SKLabelNode

required init(coder: NSCoder) {
fatalError("NSCoding not supported")
}

init(normalTexture defaultTexture: SKTexture!, selectedTexture:SKTexture!, disabledTexture: SKTexture?) {

self.defaultTexture = defaultTexture
self.selectedTexture = selectedTexture
self.disabledTexture = disabledTexture
self.label = SKLabelNode(fontNamed: "Helvetica");

super.init(texture: defaultTexture, color: UIColor.white, size: defaultTexture.size())
isUserInteractionEnabled = true

//Creating and adding a blank label, centered on the button
self.label.verticalAlignmentMode = SKLabelVerticalAlignmentMode.center;
self.label.horizontalAlignmentMode = SKLabelHorizontalAlignmentMode.center;
addChild(self.label)

// Adding this node as an empty layer. Without it the touch functions are not being called
// The reason for this is unknown when this was implemented...?
let bugFixLayerNode = SKSpriteNode(texture: nil, color: UIColor.clear, size: defaultTexture.size())
bugFixLayerNode.position = self.position
addChild(bugFixLayerNode)

}

/**
* Taking a target object and adding an action that is triggered by a button event.
*/
func setButtonAction(target: AnyObject, triggerEvent event:FTButtonActionType, action:Selector) {

switch (event) {
case .TouchUpInside:
targetTouchUpInside = target
actionTouchUpInside = action
case .TouchDown:
targetTouchDown = target
actionTouchDown = action
case .TouchUp:
targetTouchUp = target
actionTouchUp = action
}

}

/*
New function for setting text. Calling function multiple times does
not create a ton of new labels, just updates existing label.
You can set the title, font type and font size with this function
*/

func setButtonLabel(title: NSString, font: String, fontSize: CGFloat) {
self.label.text = title as String
self.label.fontSize = fontSize
self.label.fontName = font
}

var disabledTexture: SKTexture?
var actionTouchUpInside: Selector?
var actionTouchUp: Selector?
var actionTouchDown: Selector?
weak var targetTouchUpInside: AnyObject?
weak var targetTouchUp: AnyObject?
weak var targetTouchDown: AnyObject?

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if (!isEnabled) {
return
}
isSelected = true
if (targetTouchDown != nil && targetTouchDown!.responds(to: actionTouchDown)) {
UIApplication.shared.sendAction(actionTouchDown!, to: targetTouchDown, from: self, for: nil)
}
}

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

if (!isEnabled) {
return
}

let touch: AnyObject! = touches.first
let touchLocation = touch.location(in: parent!)

if (frame.contains(touchLocation)) {
isSelected = true
} else {
isSelected = false
}

}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
if (!isEnabled) {
return
}

isSelected = false

if (targetTouchUpInside != nil && targetTouchUpInside!.responds(to: actionTouchUpInside!)) {
let touch: AnyObject! = touches.first
let touchLocation = touch.location(in: parent!)

if (frame.contains(touchLocation) ) {
UIApplication.shared.sendAction(actionTouchUpInside!, to: targetTouchUpInside, from: self, for: nil)
}

}

if (targetTouchUp != nil && targetTouchUp!.responds(to: actionTouchUp!)) {
UIApplication.shared.sendAction(actionTouchUp!, to: targetTouchUp, from: self, for: nil)
}
}

}

用法:

@objc func buttonTap() {
print("Button pressed")
}

override func didMove(to view: SKView)
{
backgroundColor = SKColor.white
let buttonTexture: SKTexture! = SKTexture(imageNamed: "button")
let buttonTextureSelected: SKTexture! = SKTexture(imageNamed: "buttonSelected.png")
let button = FTButtonNode(normalTexture: buttonTexture, selectedTexture: buttonTextureSelected, disabledTexture: buttonTexture)
button.setButtonAction(target: self, triggerEvent: .TouchUpInside, action: #selector(GameScene.buttonTap))
button.setButtonLabel(title: "Button", font: "Arial", fontSize: 12)
button.position = CGPoint(x: self.frame.midX,y: self.frame.midY)
button.zPosition = 1
button.name = "Button"
self.addChild(button)
}

我创建了两个.png:

button.png buttonSelected

关于ios - 在 SpriteKit 中创建按钮 : Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38192205/

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