gpt4 book ai didi

swift - 如何使用 UISwipeGestureRecognizer 而不是与 Swift 和 SpriteKit 一起使用 TouchMoved?

转载 作者:行者123 更新时间:2023-11-30 14:11:28 25 4
gpt4 key购买 nike

在尝试使用 TouchMoved 函数拖放 SKSpriteNodes 时,当我尝试滑动 Sprite 时,让我拖动 Sprite 的代码不起作用。

这就是我所拥有的:

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?){
for touch: AnyObject in touches{
let location = touch.locationInNode(self)
let touchedNode = self.nodeAtPoint(location)
let MySprite = SKSpriteNode(imageNamed: "image.png")

if touchedNode == MySprite{
MySprite.position = location
}
}
}

这就是我正在尝试做的事情:

func swipedRight(sender:UISwipeGestureRecognizer){
let MySprite = SKSpriteNode(imageNamed: "image.png")
let touch = UITouch()
let location = touch.locationInNode(self)
let touchedNode = self.nodeAtPoint(location)

if touchedNode == MySprite{
MySprite.position = location
}
}

override func didMoveToView(view: SKView) {
let swipeRight:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("swipedRight"))
swipeRight.direction = .Right
view.addGestureRecognizer(swipeRight)
}

但是当我尝试滑动 Sprite 时,我收到 SIGABRT 错误。我查看了很多资料,但没有看到像这样的问题的答案。

有人可以给我一些关于如何使用 UISwipeGestureRecognizer 拖动 SKSpriteNode 的建议吗?

最佳答案

使用此函数在 Spritekit 中滑动 View 。

    var leftSwipe = UISwipeGestureRecognizer(target: self, action: Selector("handleSwipes:"))
var rightSwipe = UISwipeGestureRecognizer(target: self, action: Selector("handleSwipes:"))

func handleSwipes(sender:UISwipeGestureRecognizer ) {
if (sender.direction == .Right) {
if screenIndex > 0 && disableFlag == 0{
disableFlag = 1
let action = SKAction.moveTo(CGPointMake(scrollBg.position.x + frame.size.width, scrollBg.position.y), duration: 0.10)
let resetAction = SKAction.runBlock {
self.disableFlag = 0
}
scrollBg.runAction(SKAction.sequence([action,resetAction]))
screenIndex--
var suiteStatus = NSUserDefaults.standardUserDefaults().integerForKey(itemIDArr[screenIndex] as NSString)
if suiteStatus == 2 {
playEffectSound(suitSoundArray[screenIndex] as NSString)
}
}
}
if (sender.direction == .Left){
if screenIndex < itemIDArr.count-1 && disableFlag == 0{
disableFlag = 1
let action = SKAction.moveTo(CGPointMake(scrollBg.position.x - frame.size.width, scrollBg.position.y), duration: 0.10)
let resetAction = SKAction.runBlock {
self.disableFlag = 0
}
scrollBg.runAction(SKAction.sequence([action,resetAction]))
screenIndex++
var suiteStatus = NSUserDefaults.standardUserDefaults().integerForKey(itemIDArr[screenIndex] as NSString)
if suiteStatus == 2 {
playEffectSound(suitSoundArray[screenIndex] as NSString)
}
}
}
}

https://stackoverflow.com/users/5171280/brofist5

关于swift - 如何使用 UISwipeGestureRecognizer 而不是与 Swift 和 SpriteKit 一起使用 TouchMoved?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31712653/

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