gpt4 book ai didi

swift - 如何将 UItapGesture 识别器指定为按钮的 CGpoint

转载 作者:行者123 更新时间:2023-11-28 06:44:33 26 4
gpt4 key购买 nike

我正在制作一个游戏广告,但我很难制作一个跳转按钮。我已经创建了跳上跳下的 SKaction 序列,它的工作原理非常完美。

func JumpArrow () {
self.addChild(jumpArrow)
jumpArrow.position = CGPointMake(60, 145)
jumpArrow.xScale = 1
jumpArrow.yScale = 1
}

func heroJumpMovement () {
let heroJumpAction = SKAction.moveToY(hero.position.y + 85,
duration: 0.5)
let heroFallAction = SKAction.moveToY(hero.position.y , duration:
0.5)

let jumpWait:SKAction = SKAction.waitForDuration(CFTimeInterval(1))

let heroMovementSequence:SKAction =
SKAction.sequence([heroJumpAction, heroFallAction ,jumpWait])
hero.runAction(heroMovementSequence)

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

for touch in touches {

let location = touch.locationInNode(self)
let node = nodeAtPoint(location)

if node == jumpArrow {

heroJumpMovement()
}

但是,我有一个问题。如果您快速点击按钮,播放器将飞出屏幕。我希望我可以创建一个 UItapGestureRecognizer 并为点击创建一个延迟,这样你就不能每秒点击按钮 2-4 次,你只能点击一次。如果这是错误的方法,请告诉我

最佳答案

添加延迟是错误的做法。

相反,在您的 touchesBegan 函数中,在您调用 heroJumpMovement() 之前,您应该检查您的玩家是否在地面上.

另一种方法是检查最后一次跳转 SKActionSequence 是否完成

要执行上述操作,您会得到类似这样的东西(未检查代码):

var canJump = true; // Variable will be true if we can jump

func JumpArrow () {
self.addChild(jumpArrow)
jumpArrow.position = CGPointMake(60, 145)
jumpArrow.xScale = 1
jumpArrow.yScale = 1
}

func heroJumpMovement () {
let heroJumpAction = SKAction.moveToY(hero.position.y + 85,
duration: 0.5)
let heroFallAction = SKAction.moveToY(hero.position.y , duration:
0.5)

let jumpWait:SKAction = SKAction.waitForDuration(CFTimeInterval(1))

let heroMovementSequence:SKAction =
SKAction.sequence([heroJumpAction, heroFallAction ,jumpWait])
canJump = false; // We are about to jump so set this to false
hero.runAction(heroMovementSequence, completion: {canJump = true;}) // Set the canJump variable back to true after we have landed

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

for touch in touches {

let location = touch.locationInNode(self)
let node = nodeAtPoint(location)

if node == jumpArrow {
if (canJump) { // Make sure we are allowed to jump
heroJumpMovement()
}
}

注意 canJump 变量。

关于swift - 如何将 UItapGesture 识别器指定为按钮的 CGpoint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36875236/

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