Swift3 Swift 3:玩家沿操纵杆| | Spritekit持续移动

Swift3 Swift 3:玩家沿操纵杆| | Spritekit持续移动,swift3,sprite-kit,sprite,joystick,Swift3,Sprite Kit,Sprite,Joystick,我的问题是,我如何将我的精灵与操纵杆结合在一起,使其连续运动?我希望玩家在整个过程中都以一定的速度移动,然后每当用户将操纵杆移动到一定角度时,玩家就会跟着移动 例如:精灵自动移动一定的速度,例如;速度=CGFloat(100),然后它将以这个速度继续,通过速度精灵只是自动地朝一个方向移动,我不知道如何实现它,使我的播放器以恒定的速度移动,如何改变方向是用操纵杆移动 目前,通过我的代码,我可以旋转精灵 我感谢你们给我的帮助,提前谢谢你们 override func touchesBegan(_ t

我的问题是,我如何将我的精灵与操纵杆结合在一起,使其连续运动?我希望玩家在整个过程中都以一定的速度移动,然后每当用户将操纵杆移动到一定角度时,玩家就会跟着移动

例如:精灵自动移动一定的速度,例如;速度=CGFloat(100),然后它将以这个速度继续,通过速度精灵只是自动地朝一个方向移动,我不知道如何实现它,使我的播放器以恒定的速度移动,如何改变方向是用操纵杆移动

目前,通过我的代码,我可以旋转精灵

我感谢你们给我的帮助,提前谢谢你们

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


    for touch in touches {
        touchLocation = touch.location(in: self)

        if (ball.frame.contains(touchLocation)) {

            stickActive = true

        } else {
            stickActive = false
        }
    }



}

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

        if isAlive == true{ 
            joyStickMoved()

        }
        if isAlive == false{
            player.position.x = -300
        }

    }
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        if (stickActive == true) {
            let move: SKAction = SKAction.move(to: base.position, duration: 0.2)
            move.timingMode = .easeOut
            ball.run(move)
    }
}

func joyStickMoved() {

    if (stickActive == true) {



        let v = CGVector(dx: touchLocation.x - base.position.x, dy: touchLocation.y - base.position.y)
        let angle = atan2(v.dy, v.dx)

        // let degree = angle * CGFloat( 180 / Double.pi)

        let length: CGFloat = base.frame.size.height / 2

        let xDist: CGFloat = sin(angle - 1.57079633) * length
        let yDist: CGFloat = cos(angle - 1.57079633) * length
        //   TODO   // xJoystick = touchLocation.x - base.position.x
        // yJoystick = touchLocation.y - base.position.y

        if (base.frame.contains(touchLocation)) {
            ball.position = touchLocation

        } else {
            ball.position = CGPoint(x: base.position.x - xDist, y: base.position.y + yDist)

        }

        player.zRotation = angle - 1.57079633

    }


}

override func update(_ currentTime: TimeInterval) {



    if isAlive == false && stickActive == false {
        lblMain.text = "Game Over"
        player.position.x = -300
        waitThenResetTheGame()
    }

    if stickActive == true {
        moveNodeToLocation()
    }
}

func spawnPlayer(){
    player = SKSpriteNode(color: offWhiteColor, size: playerSize)
    player.position = CGPoint(x: self.frame.midX, y: 130)

    player.physicsBody = SKPhysicsBody(rectangleOf: (player.size))
    player.physicsBody?.affectedByGravity = false
    player.physicsBody?.categoryBitMask = physicsCategory.player
    player.physicsBody?.contactTestBitMask = physicsCategory.fallingBlock
    player.physicsBody?.isDynamic = false
    player.physicsBody?.allowsRotation = false
    player.physicsBody?.angularVelocity = 5
    player.physicsBody?.angularDamping = 0

    player.name = "player"


    self.addChild(player)
    setupFollower()
}
override func touchsbegind(touch:Set,带有事件:UIEvent?){
接触{
触摸位置=触摸位置(in:self)
if(球帧包含(接触位置)){
粘滞=真
}否则{
粘滞=假
}
}
}
覆盖功能触摸移动(touchs:Set,带有事件:UIEvent?){
接触{
触摸位置=触摸位置(in:self)
如果isAlive==true{
乔伊斯蒂克莫维德
}
如果isAlive==false{
player.position.x=-300
}
}
}
覆盖函数touchesend(touchs:Set,带有事件:UIEvent?){
如果(stickActive==真){
让我们移动:SKAction=SKAction.move(到:base.position,持续时间:0.2)
move.timingMode=.easeOut
球。跑(动)
}
}
func joyStickMoved(){
如果(stickActive==真){
设v=CGVector(dx:touchLocation.x-base.position.x,dy:touchLocation.y-base.position.y)
让角度=atan2(v.dy,v.dx)
//让度=角度*CGFloat(180/Double.pi)
让长度:CGFloat=base.frame.size.height/2
设xDist:CGFloat=sin(角度-1.57079633)*长度
让yDist:CGFloat=cos(角度-1.57079633)*长度
//TODO//Xmogage=touchLocation.x-base.position.x
//y操纵杆=触摸位置.y-基准位置.y
if(base.frame.contains(touchLocation)){
ball.position=触球位置
}否则{
ball.position=CGPoint(x:base.position.x-xDist,y:base.position.y+yDist)
}
player.zRotation=角度-1.57079633
}
}
覆盖函数更新(uCurrentTime:TimeInterval){
如果isAlive==false&&stickActive==false{
lblMain.text=“游戏结束”
player.position.x=-300
waitthenresethegame()
}
如果stickActive==真{
moveNodeToLocation()
}
}
func玩家(){
player=SKSpriteNode(颜色:offWhiteColor,大小:playerSize)
player.position=CGPoint(x:self.frame.midX,y:130)
player.physicsBody=SKPhysicsBody(矩形:(player.size))
player.physicsBody?.affectedByGravity=false
player.physicsBody?.categoryBitMask=physiccategory.player
player.physicsBody?.contactTestBitMask=physicCategory.fallingBlock
player.physicsBody?.isDynamic=false
player.physicsBody?.allowsRotation=false
玩家。物理身体?.angularVelocity=5
玩家.physicsBody?.angularDamping=0
player.name=“player”
self.addChild(播放器)
setupFollower()
}

为了实现连续移动,您需要跟踪玩家正在触摸的操纵杆部分;使用操纵手柄位置的不同部分或仅使用节点跟踪触摸。然后在更新函数中使用force移动节点。这样的想法是,当你触摸时,你将一些相应的BOOL设置为true,更新会根据它们移动节点,然后在触摸结束时,BOOL返回false,这样你就停止移动

我已经编写了半sudo代码,但这应该让您知道需要做什么:

//in your player class 
func walk(force: CGFloat) {
  self.physicsbody.applyForce(CGVector(dx: force, dy: 0.0))
}

//in your gamescene touchesBegan
if joystickRight.contains(touchlocation) {
  //declare all these BOOL's at the top of your gameScene class
        isTouching = true
        movingRight = true
        xVelocity = 200
}

//in your gamescene touchesEnded
  isTouching = false
  movingRight = false

//in your gamescene update
if isTouching && movingRight {
        thePlayer.walk(force: xVelocity)
}

好吧,如果你用的是物理,那么你不想做的一件事就是不断地施加脉冲,你想直接设置速度,你的人移动的速度

因为我现在在打电话,所以我要写伪代码:

基本上你想做这样的事情:

velocity =(0,0)

On update

    dir = GetDirection()
    switch(dir)
    Case up:  velocity = (0,200/150) // because 1 newton = 150points. Physics uses newtons
    Case down: velocity = (0,-200/150)
    Case left: velocity = (-200/150,0)
    Case right: velocity = (200/150,0)
    end switch

Sprite.physicsbody.velocity= velocity


End update
仅触摸屏控制:

velocity =(0,0)

On touch

    dir = GetDirection()
    switch(dir)
    Case up:  velocity = (0,200/150) // because 1 newton = 150points. Physics uses newtons
    Case down: velocity = (0,-200/150)
    Case left: velocity = (-200/150,0)
    Case right: velocity = (200/150,0)
    end switch
End touch

On update

Sprite.physicsbody.velocity= velocity


End update

信息太少,无法确定如何进行此操作,请将您的源代码发布到问题中,不要使用png。SO是为在正确格式化时处理代码而构建的。@Knight0fDragon嘿,感谢Knight0fDragon的快速响应,我按照您的建议格式化了文本,现在看起来更干净了,谢谢。第一次尝试很好,但我们甚至不知道他们是否在使用内置物理,试着让你的答案更一般化,或者使用伪编解码器作为建议,我们下次会考虑的@Knight0fDragon我使用的是内置物理。如果有帮助的话,我还包括了我的玩家类。因此,在这种情况下,我的猜测是在游戏场景更新中使用冲动或力量,根据操纵杆移动的位置继续移动玩家!那么我会宣布速度为CGPoint吗?“GetDirection()”这是什么?get direction就是你得到控制器输入的方式(我假设触摸屏),我认为速度是CGVector,看看苹果的文档,所以我使用了“touchLocation”,因为我认为它是正确的,我想我可以使用“v”或“angle”,但我不确定,对于switch语句,我必须声明那些case变量“up”,因为它不允许我使用它们。对于所有的问题,很抱歉。那么你的位置不对,你需要找到一些教程并访问一些论坛组,stack overflow是为特定的问答问题设计的,你需要学习如何编程。我知道如何编程,我没有在我的任何一个项目中使用switch语句,只是想了解如何以最有效的方式正确地使用它。