1

My question is how can i incorporate my sprite to have a continuous movement along with the joystick? I want the player to be moving at a certain speed the whole time, and then whenever the user moves the joystick a certain angle then the player will just follow along with it.

ex: Sprite automatically moves a certain speed, ex; speed = CGFloat(100), then it will continue at this speed, by speed the sprite just automatically is moving in a direction, I don't have any idea on how to implement it so that my player is moving at a constant rate, and how to change the direction is moves with the joysticks.

Currently with my code i'm able to rotate the sprite.

I appreciate all the help you guys give, thank you in advance.

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()
}
AbyxDev
  • 1,363
  • 16
  • 30
  • too little info to determine how to go about this, and post your source into the question, do not use a png. SO is built to handle code when formatted correctly. – Knight0fDragon Jun 27 '17 at 20:43
  • @Knight0fDragon Hey, thanks for the quick response Knight, I formatted in the text like you suggested, it looks a lot more cleaner now, thank you. – BennettDesign Jun 27 '17 at 21:55

2 Answers2

2

Ok well if you are using physics then one thing you do not want to do is apply impulse constantly, you want to directly set the velocity the speed your guy is moving at.

I am going to write pseudo code since I am on a phone now:

Basically you want to do something like this:

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

Touch screen only controls:

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
Knight0fDragon
  • 16,609
  • 2
  • 23
  • 44
  • So would i declare the velocity as CGPoint? and the "GetDirection()" what is that? – BennettDesign Jun 27 '17 at 23:24
  • get direction is however you get your controller input (I am going to assume the touch screen) and I think velocity is CGVector, check apple documentation – Knight0fDragon Jun 27 '17 at 23:26
  • So, i used the "touchLocation" Because i thought it'd be the right thing to use, I was thinking i could use "v" or "angle" But i wasn't sure, and the for the switch statement do i have to declare those case variables "up" Because it won't let me use them. Sorry for all the questions. – BennettDesign Jun 27 '17 at 23:36
  • 1
    You are in the wrong place then, you need to find some tutorials and visit some forum groups, stack overflow is designed for specific Q&A problems, you need to learn how to program. – Knight0fDragon Jun 27 '17 at 23:38
  • I know how to program, I haven't used a switch statement in any one of my projects, Just trying to understand how to properly go about using it the most efficient way. – BennettDesign Jun 27 '17 at 23:41
  • Please go find some tutorials, stack overflow is not the tool to use to solve the problems you are facing right now. – Knight0fDragon Jun 27 '17 at 23:42
  • Been searching for 3 days on how to do this, This honestly was my last option. /= – BennettDesign Jun 28 '17 at 00:55
0

In order to implement continuous movement, you need to track which part of the joystick your player is touching; either use the different part of the joysticks position or just use nodes to track the touch. Then move your node using force in the update function. So the idea is that when you touch, you set some corresponding BOOL's to true and the update moves the node based on them, then on touch end, the BOOL's return to false so you stop moving.

I've semi sudo coded this, but this should give you an idea of what you need to do:

//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)
}
hyper0009
  • 236
  • 1
  • 11
  • Good first try but we do not even know if they are using the built in physics yet, try making your answer more generic or use pseudo code – Knight0fDragon Jun 27 '17 at 22:34
  • Cheers for the advice, will take that on board for next time ! – hyper0009 Jun 27 '17 at 22:38
  • @Knight0fDragon I'm using built in physics. – BennettDesign Jun 27 '17 at 22:44
  • I also included my player class, if that's any help. – BennettDesign Jun 27 '17 at 22:45
  • So in this case my guess would be to use impulse or force in the gameScene update to keep moving the player based on where the joystick is moving ! – hyper0009 Jun 27 '17 at 22:48
  • @hyper0009 So do i have to make if joystickLeft.contains, and if joystickUp.contains, for all directions? Kinda confused. Sorry! – BennettDesign Jun 27 '17 at 22:54
  • That would be my educated guess, i implemented it this way with buttons so it should be the same with the joystick. maybe try and see if you can check the position of touch within the joy stick. so if the touch is to the right side of the joystick: do the following, else if: do the following. check out how this guy is doing it https://stackoverflow.com/questions/31667967/spritekit-joystick – hyper0009 Jun 27 '17 at 23:03