首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SpriteKit -快速触碰会导致错误行为

SpriteKit -快速触碰会导致错误行为
EN

Stack Overflow用户
提问于 2018-01-28 17:35:33
回答 1查看 51关注 0票数 0

我目前是在一个侧面游戏中工作,在这个游戏中,跳高取决于玩家按下屏幕右半部分的时间。所有操作都很好,除非用户快速触摸屏幕。这使得跳跃尽可能大。

我是不是做错了什么,还是SpriteKit的工作方式出了问题?我该如何解决这个问题?

编辑:以下是我游戏中处理触摸的所有方法:

代码语言:javascript
复制
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
{
    for touch in touches
    {
        swiped = false

        let location = touch.location(in: cameraNode)

        DispatchQueue.main.asyncAfter(deadline: .now() + 0.065)
        {
            if self.swiped == false
            {
                if location.x < 0
                {
                    self.changeColor()
                }
                else
                {
                    self.jump()
                }
            }
        }
    }

}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?)
{
    for touch in touches {
        let location = touch.location(in: cameraNode)

            if location.x > 0
            {
                // Right
                thePlayer.endJump()
            }
    }
}

此外,还有一个手势识别器,它负责左右滑动,有以下处理程序:

代码语言:javascript
复制
    @objc func swipedRight()
{
        if walkstate != .walkingRight
        {
            walkstate = .walkingRight
        }
        else
        {
            boost(direction: 0)
        }

        swiped = true
}

@objc func swipedLeft()
{

        if walkstate != .walkingLeft
        {
            walkstate = .walkingLeft
        }
        else
        {
            boost(direction: 1)
        }

        swiped = true
}

希望这足以描述这些问题。上面的代码是我为处理触摸所做的一切。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-02-03 22:48:07

问题是,我正在使用DispatchQueue命令在短时间延迟后调用跳转方法,以防用户滑动而不是点击。因此,touchesEnded方法在跳转开始之前就会被调用,因此不能再停止。

为了解决这个问题,我添加了一个布尔变量,当玩家触摸屏幕时,这个变量被设置为true,并在用户手指离开屏幕时设置为false。为了跳转,必须将该变量设置为true,因此字符将不再在快速触摸后跳转。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48489371

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档