使用SKSpriteKit和Swift 4触摸移动和旋转精灵

时间:2018-10-19 07:24:59

标签: swift sprite-kit sprite touchesmoved touches

我希望我的精灵“角色”移动并随着手指移动而“跟随”手指旋转。我的软件工作正常,但以某种方式设法解决了问题。我不确定自己做了什么,但无法为自己的生活弄清楚。

任何帮助将不胜感激。我确定我只是忽略了一些愚蠢的事情。预先感谢。

标记

GameScene.swift

import SpriteKit
import GameplayKit

//MARK: ---------------Global Variables Section

var nodeType: String = String()

var characterNode = Character()
let characterImage: String = String("default_pose")
var characterTexture:SKTexture = SKTexture()
let characterSize: CGSize = CGSize(width: 64, height: 64)
let characterPosition: CGPoint = CGPoint(x: 0, y: 0)

var enemyNode = Enemy()
let enemyImage: String = String("blob1")
var enemyTexture = SKTexture()
let enemySize: CGSize = CGSize(width: 50, height: 50)
let enemyPosition: CGPoint = CGPoint(x: 150, y: 150)


class GameScene: SKScene, SKPhysicsContactDelegate {


    //MARK: --------------------Class Variables Section

    var affectedByGravity: Bool = false
    var dynamic: Bool = true
    var rotation: Bool = false

    var lastUpdateTime: CFTimeInterval = 0

    //MARK: --------------------DidMove to View Section

    override func didMove(to view: SKView) {

        var node = SKSpriteNode()

        node = addNode(NodeType: "Character")
        applyPhysics(physicsNode: node, nodeImage: characterImage, nodeSize: characterSize, nodeName: node.name!)
        node.zPosition = 10
        addChild(node)

        node = addNode(NodeType: "Enemy")
        applyPhysics(physicsNode: node, nodeImage: enemyImage, nodeSize: enemySize, nodeName: node.name!)
        node.zPosition = 9
        addChild(node)
    }


    //MARK: -----------------------Add Node Section

    func addNode(NodeType: String) -> SKSpriteNode {

        var node = SKSpriteNode()

        switch NodeType {

        case "Enemy":

            node = enemyNode.setupNode(nodeName: "Enemy", nodeImage: "blob1", nodeSize: enemySize, nodePosition: enemyPosition)

        default:
            node = characterNode.setupNode(nodeName: "Player", nodeImage: "default_pose", nodeSize: characterSize, nodePosition: characterPosition)
        }
        return node
    }

    //MARK: ------------------------Physics Section

    func applyPhysics(physicsNode: SKSpriteNode, nodeImage: String, nodeSize: CGSize, nodeName: String) {

        let nodeTexture: SKTexture = SKTexture(imageNamed: nodeImage)

        physicsNode.physicsBody = SKPhysicsBody(texture: nodeTexture, size: nodeSize)
        physicsNode.physicsBody?.isDynamic = dynamic
        physicsNode.physicsBody?.affectedByGravity = affectedByGravity
        physicsNode.physicsBody?.allowsRotation = rotation
        physicsNode.physicsBody?.categoryBitMask = 1
        physicsNode.physicsBody?.contactTestBitMask = 1
        physicsNode.physicsBody?.collisionBitMask = 1

    }

    //MARK: ------------------------Movement and Rotation Section

    func moveAndRotate(sprite: Character, toPosition position: CGPoint) {

        let angle = atan2(position.y - sprite.position.y, position.x - sprite.position.x)

        let rotateAction = SKAction.rotate(toAngle: angle - .pi / 2, duration: 0.05, shortestUnitArc:true)

        sprite.run(rotateAction)

        let offsetX = position.x - (sprite.position.x)
        let offsetY = position.y - (sprite.position.y)

        let normal = simd_normalize(simd_double2(x: Double(offsetX), y:Double(offsetY)))

        characterNode.velocity = CGVector(dx: CGFloat(normal.x) * sprite.movePointsPerSecond, dy: CGFloat(normal.y) * sprite.movePointsPerSecond)
    }

    //MARK: ----------------------Touches Section

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        if let touch = touches.first {
            let touchLocation = touch.location(in: self)
            characterNode.isMoving = true
            moveAndRotate(sprite: characterNode, toPosition: touchLocation)
        }
    }

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

        if let touch = touches.first {
            let touchLocation = touch.location(in: self)
            moveAndRotate(sprite: characterNode, toPosition: touchLocation)
        }
    }

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

        characterNode.isMoving = false
    }


    //MARK: ------------------Update Section

    override func update(_ currentTime: TimeInterval) {

        let deltaTime = max(1.0/30, currentTime - lastUpdateTime)
        lastUpdateTime = currentTime

        update(character: characterNode, dt: deltaTime)
    }

    func update(character: Character, dt: CFTimeInterval) {
        if characterNode.isMoving == true {
            let newX = character.position.x + character.velocity.dx * CGFloat(dt)
            let newY = character.position.y + character.velocity.dy * CGFloat(dt)

            character.position = CGPoint(x: newX, y: newY)
        }
    }
}

Character.swift

import SpriteKit
import GameplayKit

class Character: SKSpriteNode {

    let movePointsPerSecond: CGFloat = 150.0
    var velocity = CGVector(dx: 0.0, dy: 0.0)
    var isMoving = false

    var node: SKSpriteNode = SKSpriteNode()

    func setupNode(nodeName: String, nodeImage: String, nodeSize: CGSize, nodePosition: CGPoint) -> SKSpriteNode {

        node = SKSpriteNode(imageNamed: nodeImage)
        node.name = nodeName
        node.position = nodePosition

        return node
    }
}

1 个答案:

答案 0 :(得分:1)

您的节点不会移动,因为您从未将characterNode分配给屏幕上的节点

Traceback (most recent call last):
  File "D:\Dev\Scripts\NewYorkCase__upwork__\new.py", line 60, in <module>
    main(driver, main_url)
  File "D:\Dev\Scripts\NewYorkCase__upwork__\new.py", line 53, in main
    get_ready_captcha = captcha_symbols_recognized(get_path)
  File "D:\Dev\Scripts\NewYorkCase__upwork__\new.py", line 40, in captcha_symbols_recognized
    print(pytesseract.image_to_string(image))
  File "C:\Users\PANDEMIC\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pytesseract\pytesseract.py", line 294, in image_to_string
    return run_and_get_output(*args)
  File "C:\Users\PANDEMIC\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pytesseract\pytesseract.py", line 202, in run_and_get_output
    run_tesseract(**kwargs)
  File "C:\Users\PANDEMIC\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pytesseract\pytesseract.py", line 178, in run_tesseract
    raise TesseractError(status_code, get_errors(error_string))
pytesseract.pytesseract.TesseractError: (1, 'Error opening data file \\Program Files (x86)\\Tesseract-OCR\\eng.traineddata Please make sure the TESSDATA_PREFIX environment variable is set to your "tessdata" directory. Failed loading language \'eng\' Tesseract couldn\'t load any languages! Could not initialize tesseract.')