Game Dev: How to move the sprite in 90 deg angle zigzag?










1














I want to create line that have linked series of line segments that gets longer every seconds, and each time the user taps the direction of growth turns 90 degrees.



enter image description here



I finally managed to create at least some working prototype of my zig zag line.
My main idea is to initially create two points in the same position,
and then just move the end point in the desired direction. If the player changes the state of the line, then we add another point in the current head position.



Not sure if this was a good decision. Please give me some advise or maybe idea for more elegant solution.



For now I got this:



enter image description here



Here is my code:



//
// GameScene.swift
// Flarrow
//
// Created by Денис Андрейчук on 11/9/18.
// Copyright © 2018 Денис Андрейчук. All rights reserved.
//

import SpriteKit

class GameScene: SKScene
//For state control
enum State
case moveUp
case moveLeft
case moveRight


var currentState = State.moveUp
var pathArray = [CGPoint]()
let line = SKShapeNode()

override func didMove(to view: SKView)
self.backgroundColor = .gray

currentState = .moveUp

pathArray.append(CGPoint(x: 0, y: -UIScreen.main.bounds.height))
pathArray.append(CGPoint(x: 0, y: -UIScreen.main.bounds.height))
createLine()


//When user touch on screen, check for touch position, change state based on that
//and add duplicate of current last point
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
let touch = touches.first!
let location = touch.location(in: self)

if location.x > 0
currentState = .moveRight
pathArray.append(pathArray[pathArray.endIndex - 1])
else
currentState = .moveLeft
pathArray.append(pathArray[pathArray.endIndex - 1])



//Init line
func createLine()
let path = CGMutablePath()
path.move(to: pathArray[0])

for point in pathArray
path.addLine(to: point)


line.path = path
line.fillColor = .clear
line.lineWidth = 5
line.strokeColor = .red

self.addChild(line)


//Update last point possition based on current state
override func update(_ currentTime: TimeInterval)
let path = CGMutablePath()
path.move(to: pathArray[0])

switch currentState
case .moveUp:
pathArray[1].y += 1
case .moveLeft:
pathArray[pathArray.endIndex - 1].y += 1
pathArray[pathArray.endIndex - 1].x -= 1
case .moveRight:
pathArray[pathArray.endIndex - 1].y += 1
pathArray[pathArray.endIndex - 1].x += 1


for point in pathArray
path.addLine(to: point)


line.path = path





In the last question I did not formulate my question very well, so now I described it in more detail.










share|improve this question




























    1














    I want to create line that have linked series of line segments that gets longer every seconds, and each time the user taps the direction of growth turns 90 degrees.



    enter image description here



    I finally managed to create at least some working prototype of my zig zag line.
    My main idea is to initially create two points in the same position,
    and then just move the end point in the desired direction. If the player changes the state of the line, then we add another point in the current head position.



    Not sure if this was a good decision. Please give me some advise or maybe idea for more elegant solution.



    For now I got this:



    enter image description here



    Here is my code:



    //
    // GameScene.swift
    // Flarrow
    //
    // Created by Денис Андрейчук on 11/9/18.
    // Copyright © 2018 Денис Андрейчук. All rights reserved.
    //

    import SpriteKit

    class GameScene: SKScene
    //For state control
    enum State
    case moveUp
    case moveLeft
    case moveRight


    var currentState = State.moveUp
    var pathArray = [CGPoint]()
    let line = SKShapeNode()

    override func didMove(to view: SKView)
    self.backgroundColor = .gray

    currentState = .moveUp

    pathArray.append(CGPoint(x: 0, y: -UIScreen.main.bounds.height))
    pathArray.append(CGPoint(x: 0, y: -UIScreen.main.bounds.height))
    createLine()


    //When user touch on screen, check for touch position, change state based on that
    //and add duplicate of current last point
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
    let touch = touches.first!
    let location = touch.location(in: self)

    if location.x > 0
    currentState = .moveRight
    pathArray.append(pathArray[pathArray.endIndex - 1])
    else
    currentState = .moveLeft
    pathArray.append(pathArray[pathArray.endIndex - 1])



    //Init line
    func createLine()
    let path = CGMutablePath()
    path.move(to: pathArray[0])

    for point in pathArray
    path.addLine(to: point)


    line.path = path
    line.fillColor = .clear
    line.lineWidth = 5
    line.strokeColor = .red

    self.addChild(line)


    //Update last point possition based on current state
    override func update(_ currentTime: TimeInterval)
    let path = CGMutablePath()
    path.move(to: pathArray[0])

    switch currentState
    case .moveUp:
    pathArray[1].y += 1
    case .moveLeft:
    pathArray[pathArray.endIndex - 1].y += 1
    pathArray[pathArray.endIndex - 1].x -= 1
    case .moveRight:
    pathArray[pathArray.endIndex - 1].y += 1
    pathArray[pathArray.endIndex - 1].x += 1


    for point in pathArray
    path.addLine(to: point)


    line.path = path





    In the last question I did not formulate my question very well, so now I described it in more detail.










    share|improve this question


























      1












      1








      1







      I want to create line that have linked series of line segments that gets longer every seconds, and each time the user taps the direction of growth turns 90 degrees.



      enter image description here



      I finally managed to create at least some working prototype of my zig zag line.
      My main idea is to initially create two points in the same position,
      and then just move the end point in the desired direction. If the player changes the state of the line, then we add another point in the current head position.



      Not sure if this was a good decision. Please give me some advise or maybe idea for more elegant solution.



      For now I got this:



      enter image description here



      Here is my code:



      //
      // GameScene.swift
      // Flarrow
      //
      // Created by Денис Андрейчук on 11/9/18.
      // Copyright © 2018 Денис Андрейчук. All rights reserved.
      //

      import SpriteKit

      class GameScene: SKScene
      //For state control
      enum State
      case moveUp
      case moveLeft
      case moveRight


      var currentState = State.moveUp
      var pathArray = [CGPoint]()
      let line = SKShapeNode()

      override func didMove(to view: SKView)
      self.backgroundColor = .gray

      currentState = .moveUp

      pathArray.append(CGPoint(x: 0, y: -UIScreen.main.bounds.height))
      pathArray.append(CGPoint(x: 0, y: -UIScreen.main.bounds.height))
      createLine()


      //When user touch on screen, check for touch position, change state based on that
      //and add duplicate of current last point
      override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
      let touch = touches.first!
      let location = touch.location(in: self)

      if location.x > 0
      currentState = .moveRight
      pathArray.append(pathArray[pathArray.endIndex - 1])
      else
      currentState = .moveLeft
      pathArray.append(pathArray[pathArray.endIndex - 1])



      //Init line
      func createLine()
      let path = CGMutablePath()
      path.move(to: pathArray[0])

      for point in pathArray
      path.addLine(to: point)


      line.path = path
      line.fillColor = .clear
      line.lineWidth = 5
      line.strokeColor = .red

      self.addChild(line)


      //Update last point possition based on current state
      override func update(_ currentTime: TimeInterval)
      let path = CGMutablePath()
      path.move(to: pathArray[0])

      switch currentState
      case .moveUp:
      pathArray[1].y += 1
      case .moveLeft:
      pathArray[pathArray.endIndex - 1].y += 1
      pathArray[pathArray.endIndex - 1].x -= 1
      case .moveRight:
      pathArray[pathArray.endIndex - 1].y += 1
      pathArray[pathArray.endIndex - 1].x += 1


      for point in pathArray
      path.addLine(to: point)


      line.path = path





      In the last question I did not formulate my question very well, so now I described it in more detail.










      share|improve this question















      I want to create line that have linked series of line segments that gets longer every seconds, and each time the user taps the direction of growth turns 90 degrees.



      enter image description here



      I finally managed to create at least some working prototype of my zig zag line.
      My main idea is to initially create two points in the same position,
      and then just move the end point in the desired direction. If the player changes the state of the line, then we add another point in the current head position.



      Not sure if this was a good decision. Please give me some advise or maybe idea for more elegant solution.



      For now I got this:



      enter image description here



      Here is my code:



      //
      // GameScene.swift
      // Flarrow
      //
      // Created by Денис Андрейчук on 11/9/18.
      // Copyright © 2018 Денис Андрейчук. All rights reserved.
      //

      import SpriteKit

      class GameScene: SKScene
      //For state control
      enum State
      case moveUp
      case moveLeft
      case moveRight


      var currentState = State.moveUp
      var pathArray = [CGPoint]()
      let line = SKShapeNode()

      override func didMove(to view: SKView)
      self.backgroundColor = .gray

      currentState = .moveUp

      pathArray.append(CGPoint(x: 0, y: -UIScreen.main.bounds.height))
      pathArray.append(CGPoint(x: 0, y: -UIScreen.main.bounds.height))
      createLine()


      //When user touch on screen, check for touch position, change state based on that
      //and add duplicate of current last point
      override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
      let touch = touches.first!
      let location = touch.location(in: self)

      if location.x > 0
      currentState = .moveRight
      pathArray.append(pathArray[pathArray.endIndex - 1])
      else
      currentState = .moveLeft
      pathArray.append(pathArray[pathArray.endIndex - 1])



      //Init line
      func createLine()
      let path = CGMutablePath()
      path.move(to: pathArray[0])

      for point in pathArray
      path.addLine(to: point)


      line.path = path
      line.fillColor = .clear
      line.lineWidth = 5
      line.strokeColor = .red

      self.addChild(line)


      //Update last point possition based on current state
      override func update(_ currentTime: TimeInterval)
      let path = CGMutablePath()
      path.move(to: pathArray[0])

      switch currentState
      case .moveUp:
      pathArray[1].y += 1
      case .moveLeft:
      pathArray[pathArray.endIndex - 1].y += 1
      pathArray[pathArray.endIndex - 1].x -= 1
      case .moveRight:
      pathArray[pathArray.endIndex - 1].y += 1
      pathArray[pathArray.endIndex - 1].x += 1


      for point in pathArray
      path.addLine(to: point)


      line.path = path





      In the last question I did not formulate my question very well, so now I described it in more detail.







      swift sprite-kit






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 11 at 14:29

























      asked Nov 11 at 14:16









      Den Andreychuk

      587




      587



























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53249608%2fgame-dev-how-to-move-the-sprite-in-90-deg-angle-zigzag%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53249608%2fgame-dev-how-to-move-the-sprite-in-90-deg-angle-zigzag%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Darth Vader #20

          How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

          Ondo