本文介绍了无法以我自己定义的速度移动我的头像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法以我定义的速度(设置为 40)在我的游戏场景中移动我的头像.看起来好像化身和敌人在太空中自由漂浮.这是我的代码-导入 SpriteKit

I am unable to move my avatar in my gameScene at my defined speed(set at 40). It looks as if the avatar and the enemy is free floating in space. Here is my code-import SpriteKit

公共类 GameScene:SKScene、SKPhysicsContactDelegate {

public class GameScene: SKScene, SKPhysicsContactDelegate {

let playerSpeed: CGFloat = 40.0
let coronaSpeed: CGFloat = 10.0

var mask: SKSpriteNode?
var player: SKSpriteNode?
var mcorona: [SKSpriteNode] = []

var lastTouch: CGPoint? = nil

override public func didMove(to view: SKView) {
    physicsWorld.contactDelegate = self

    // Animations
    player = childNode(withName: "player") as? SKSpriteNode

    mask = childNode(withName: "mask") as? SKSpriteNode
    mask!.run(SKAction.repeatForever(
        SKAction.sequence([
            SKAction.moveBy(x: 0, y: 10, duration: 0.45),
            SKAction.moveBy(x: 0, y: -10, duration: 0.45)
            ]
    )))

    for child in self.children {
        if child.name == "corona" {
            if let child = child as? SKSpriteNode {
                mcorona.append(child)
            }
        }
    }
    // </> Animations
}

override public func touchesBegan(_ touches: Set<UITouch>,with event: UIEvent?) { handleTouches(touches) }

override public func touchesMoved(_ touches: Set<UITouch>,with event: UIEvent?) { handleTouches(touches) }

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

fileprivate func handleTouches(_ touches: Set<UITouch>) { lastTouch = touches.first?.location(in: self) }

override public func didSimulatePhysics() {
    if player != nil {
        updatePlayer()
        updateZombies()
    }
}


fileprivate func shouldMove(currentPosition: CGPoint,
                            touchPosition: CGPoint) -> Bool {
    guard let player = player else { return false }
    return abs(currentPosition.x - touchPosition.x) > player.frame.width / 2 ||
        abs(currentPosition.y - touchPosition.y) > player.frame.height / 2
}

fileprivate func updatePlayer() {
    guard let player = player,
        let touch = lastTouch
        else { return }
    let currentPosition = player.position
    if shouldMove(currentPosition: currentPosition,
                  touchPosition: touch) {
        updatePosition(for: player, to: touch, speed: playerSpeed)


    } else {
        player.physicsBody?.isResting = true
    }
}


func updateZombies() {
    guard let player = player else { return }
    let targetPosition = player.position

    for corona in mcorona {
        updatePosition(for: corona, to: targetPosition, speed: coronaSpeed)
    }
}
fileprivate func updatePosition(for sprite: SKSpriteNode, to target: CGPoint, speed: CGFloat) {

    let currentPosition = sprite.position
    let angle = CGFloat.pi + atan2(currentPosition.y - target.y, currentPosition.x - target.x)
    let rotateAction = SKAction.rotate(toAngle: angle + (CGFloat.pi*0.5), duration: 0)
    sprite.run(rotateAction)
    //sprite.physicsBody?.isDynamic = true

    let velocityX = speed * cos(angle)
    let velocityY = speed * sin(angle)

    let newVelocity = CGVector(dx: velocityX, dy: velocityY)
    sprite.physicsBody?.velocity = newVelocity
    let moveToTouch = SKAction.move(to: CGPoint(x: target.x, y: target.y), duration: TimeInterval(speed))
    sprite.run(moveToTouch)
    sprite.physicsBody?.affectedByGravity = false

}

public func didBegin(_ contact: SKPhysicsContact) {

    var firstBody: SKPhysicsBody
    var secondBody: SKPhysicsBody

    if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
        firstBody = contact.bodyA
        secondBody = contact.bodyB
    } else {
        firstBody = contact.bodyB
        secondBody = contact.bodyA
    }

    // Check contact
    if firstBody.categoryBitMask == player?.physicsBody?.categoryBitMask &&
        secondBody.categoryBitMask == mcorona[0].physicsBody?.categoryBitMask {
        // Player & corona
        gameOver(false)
    } else if firstBody.categoryBitMask == player?.physicsBody?.categoryBitMask &&
        secondBody.categoryBitMask == mask?.physicsBody?.categoryBitMask {
        // Player & mask
        gameOver(true)
    }


}
fileprivate func gameOver(_ didWin: Bool) {
    let resultScene = MenuScene(size: size, didWin: didWin, levelToSend: 2)
    let transition = SKTransition.flipVertical(withDuration: 1.0)
    view?.presentScene(resultScene, transition: transition)
}

}

这是一个表示情况的 GIF-https://media.giphy.com/media/TLNx0x3HUtpdPJ9KIK/giphy.gif请帮助!.提前致谢!

Here is a GIF representing the situation-https://media.giphy.com/media/TLNx0x3HUtpdPJ9KIK/giphy.gifPLS HELP! . Thanks in advance!

推荐答案

func getDuration(pointA:CGPoint,pointB:CGPoint,speed:CGFloat)->TimeInterval{
        let xDist = (pointB.x - pointA.x)
        let yDist = (pointB.y - pointA.y)
        let distance = sqrt((xDist * xDist) + (yDist * yDist));
        let duration : TimeInterval = TimeInterval(distance/speed)
        return duration
    }

更改了 moveToTouch 文件,如下所示

changed the moveToTouch file as shown below

let moveToTouch = SKAction.move(to: CGPoint(x: target.x, y: target.y),duration: getDuration(pointA:currentPosition,pointB:target,speed:speed))
sprite.run(moveToTouch)

这篇关于无法以我自己定义的速度移动我的头像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 04:39