gpt4 book ai didi

Swift/SpriteKit 按钮拖动关闭取消不起作用?

转载 作者:搜寻专家 更新时间:2023-11-01 07:20:32 29 4
gpt4 key购买 nike

您好,我需要您的帮助,我一直在寻找这个问题的答案。

我还简化了代码以摆脱分配您不需要知道的信息/垃圾。

我有一个使用 SpriteKit 和 Swift 的 IOS 游戏的主菜单场景。有一个主菜单播放按钮。我想要的是按下按钮时按钮变大一点。当我的手指松开时,按钮变小。使用 override func touchesBegan/touchesEnded 效果很好。我的问题是当我的手指被拖离按钮时就像取消一样。按钮不会返回到原始大小。我很确定我需要使用

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

但是经过多次尝试,当我的手指被拖离按钮时,我没有得到将按钮恢复到原始大小的预期结果。感谢您的帮助。

import Foundation
import SpriteKit
import UIKit

class StartScene: SKScene {

var playButton: SKNode! = nill

override func didMoveToView(view: SKView) {

// Create PlayButton image
playButton = SKSpriteNode(imageNamed: "playButtonStatic")
// location of Button
playButton.position = CGPoint(x:CGRectGetMidX(self.frame), y:520);
self.addChild(playButton)
playButton.setScale(0.5)
}



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


for touch: AnyObject in touches {
let location = touch.locationInNode(self)
if playButton.containsPoint(location) {
playButton.setScale(0.6)}}}



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

for touch: AnyObject in touches {

let location = touch.locationInNode(self)
if playButton.containsPoint(location) {
playButton.setScale(0.5)
}}}



// Problem Area

override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) {
for touch: AnyObject in touches! {

let location = touch.locationInNode(self)
if playButton.containsPoint(location) {
playButton.setScale(0.5)
}}}

}

感谢您的回复

最佳答案

在 TouchesEnded 中找到了使用 Else 语句的解决方案

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

for touch: AnyObject in touches {

let location = touch.locationInNode(self)
if playButton.containsPoint(location) {
playButton.setScale(0.5)

}
else
{
playButton.setScale(0.5)
}}

关于Swift/SpriteKit 按钮拖动关闭取消不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39344879/

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