• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - Spritekit 动画没有改变

[复制链接]
菜鸟教程小白 发表于 2022-12-11 21:47:12 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我的游戏中有一个虚拟控制板。如果我点击并按住其中一个方向按钮,播放器会朝正确的方向移动并显示正确的图像和动画。但是,如果我将手指从一个控件滑到另一个控件(比如从右到下),播放器会向下移动,但图像和动画在我抬起手指之前不会改变。下面是我处理触摸的代码:

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

        if upRect.contains(location) {
            moveXDirection = 0
            moveYDirection = 1
            isIdle = false
            playerSprite.walkUpPlayer()
        } else if rightRect.contains(location) {
            moveXDirection = 1
            moveYDirection = 0
            isIdle = false
            playerSprite.walkLRPlayer()
        } else if downRect.contains(location) {
            moveXDirection = 0
            moveYDirection = -1
            isIdle = false
            playerSprite.walkDownPlayer()
        } else if leftRect.contains(location) {
            moveXDirection = -1
            moveYDirection = 0
            isIdle = false
             playerSprite.walkLRPlayer()
        }
        if !isIdle && moveXDirection != 0 {
            playerSprite.xScale = moveXDirection
        }
    }
}

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

        if upRect.contains(location) {
            moveXDirection = 0
            moveYDirection = 1
            isIdle = false
        } else if rightRect.contains(location) {
            moveXDirection = 1
            moveYDirection = 0
            isIdle = false
        } else if downRect.contains(location) {
            moveXDirection = 0
            moveYDirection = -1
            isIdle = false
        } else if leftRect.contains(location) {
            moveXDirection = -1
            moveYDirection = 0
            isIdle = false
        }
        if !isIdle && moveXDirection != 0 {
            playerSprite.xScale = moveXDirection
        }

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

我更改播放器动画的代码是:

func idlePlayer() {
    removeAllActions()
}

func walkLRPlayer() {
    removeAllActions()
    run(SKAction.repeatForever(walkLRAnimation!))
}

func walkUpPlayer() {
    removeAllActions()
    run(SKAction.repeatForever(walkUpAnimation!))
}

func walkDownPlayer() {
    removeAllActions()
    run(SKAction.repeatForever(walkDownAnimation!))
}

任何建议将不胜感激。



Best Answer-推荐答案


您的播放器从 touchesBegan 中的方向开始并在 touchesEnded 中停止。问题是您没有处理 touchesMoves 上的方向变化。

让我们创建一个全局变量,它会告诉我们是否改变了方向。

var previous_direction : Int = -1 // idle state

在触摸功能上,添加 previous_direction 行为。

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch in (touches as! Set<UITouch>) {

        // we start with previous_direction being the start direction

        if upRect.contains(location) {
            previous_direction = 0  // up
        } else if rightRect.contains(location) {
            previous_direction = 1  // right
        } else if downRect.contains(location) {
            previous_direction = 2  // down
        } else if leftRect.contains(location) {
            previous_direction = 3  // left
        }
    }
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch in (touches as! Set<UITouch>) {

        // we create a current_direction also
        var current_direction : Int = -1

        // get current_direction 
        if upRect.contains(location) {
            current_direction = 0 // up
        } else if rightRect.contains(location) {
            current_direction = 1 // right
        } else if downRect.contains(location) {
            current_direction = 2 // down
        } else if leftRect.contains(location) {
            current_direction = 0 // left
        }

        // now we compare if the current_direction is different from previous_direction
        if current_direction != previous_direction
        {
             player.removeAllActions()

             // attribute new direction
             if current_direction == 0 {
                   playerSprite.walkUpPlayer()
             } else if current_direction == 1 {
                   walkLRPlayer.walkLRPlayer()
             } else if current_direction == 2 {
                   walkLRPlayer.walkDownPlayer()
             } else if current_direction == 3 {
                   walkLRPlayer.walkLRPlayer() } 

             // we prepare previous_direction for the next time he enters this function
             previous_direction = current_direction  
        }
    }
 }

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    // reset previous_direction
    previous_direction = - 1 // idle state
}

关于ios - Spritekit 动画没有改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54227703/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap