Save mouse location then have a second value that updates










0














I'm using the Phaser engine, and I want to have a line be drawn on a click and hold event from the initial mouse position and have it constantly update to draw to the mouse position as it moves. My problem is that when i try to store the initial mouse position it keeps changing. This seems like a simple problem but i'm not very good with this stuff. Here is the code:



var unitLine;
if(game.input.activePointer.isDown)
const firstX = game.input.x;
const firstY = game.input.y;
unitLine = game.add.graphics(100, 100);
unitLine.beginFill(0xFF3300);
unitLine.lineStyle(10, 0xffd900, 1);

unitLine.moveTo(firstX, firstY);
unitLine.lineTo(game.input.x, game.input.y);



that firstX and firstY are changing even when i declare them as a const. Not sure what to do here.










share|improve this question




























    0














    I'm using the Phaser engine, and I want to have a line be drawn on a click and hold event from the initial mouse position and have it constantly update to draw to the mouse position as it moves. My problem is that when i try to store the initial mouse position it keeps changing. This seems like a simple problem but i'm not very good with this stuff. Here is the code:



    var unitLine;
    if(game.input.activePointer.isDown)
    const firstX = game.input.x;
    const firstY = game.input.y;
    unitLine = game.add.graphics(100, 100);
    unitLine.beginFill(0xFF3300);
    unitLine.lineStyle(10, 0xffd900, 1);

    unitLine.moveTo(firstX, firstY);
    unitLine.lineTo(game.input.x, game.input.y);



    that firstX and firstY are changing even when i declare them as a const. Not sure what to do here.










    share|improve this question


























      0












      0








      0







      I'm using the Phaser engine, and I want to have a line be drawn on a click and hold event from the initial mouse position and have it constantly update to draw to the mouse position as it moves. My problem is that when i try to store the initial mouse position it keeps changing. This seems like a simple problem but i'm not very good with this stuff. Here is the code:



      var unitLine;
      if(game.input.activePointer.isDown)
      const firstX = game.input.x;
      const firstY = game.input.y;
      unitLine = game.add.graphics(100, 100);
      unitLine.beginFill(0xFF3300);
      unitLine.lineStyle(10, 0xffd900, 1);

      unitLine.moveTo(firstX, firstY);
      unitLine.lineTo(game.input.x, game.input.y);



      that firstX and firstY are changing even when i declare them as a const. Not sure what to do here.










      share|improve this question















      I'm using the Phaser engine, and I want to have a line be drawn on a click and hold event from the initial mouse position and have it constantly update to draw to the mouse position as it moves. My problem is that when i try to store the initial mouse position it keeps changing. This seems like a simple problem but i'm not very good with this stuff. Here is the code:



      var unitLine;
      if(game.input.activePointer.isDown)
      const firstX = game.input.x;
      const firstY = game.input.y;
      unitLine = game.add.graphics(100, 100);
      unitLine.beginFill(0xFF3300);
      unitLine.lineStyle(10, 0xffd900, 1);

      unitLine.moveTo(firstX, firstY);
      unitLine.lineTo(game.input.x, game.input.y);



      that firstX and firstY are changing even when i declare them as a const. Not sure what to do here.







      javascript phaser-framework






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 at 4:21









      Alex Taylor

      5,42521533




      5,42521533










      asked Nov 12 at 4:11









      Ibrahim Fadel

      708




      708






















          2 Answers
          2






          active

          oldest

          votes


















          2














          The problem is that you're setting firstX and firstY whenever the mouse isDown, so they're basically overwritten every frame that the mouse is down.



          To get around this, try using Phaser's game.input.onDown function:






          var game = new Phaser.Game(500, 500, Phaser.CANVAS, 'test', 
          preload: preload,
          create: create,
          update: update
          );

          function preload()

          let firstX;
          let firstY;

          function create()
          game.input.onDown.add(function()
          firstX = game.input.x;
          firstY = game.input.y;
          , this);

          var unitLine;

          function update()
          if (game.input.activePointer.isDown)
          unitLine = game.add.graphics(0, 0);
          unitLine.beginFill(0xFF3300);
          unitLine.lineStyle(10, 0xffd900, 1);

          unitLine.moveTo(firstX, firstY);
          unitLine.lineTo(game.input.x, game.input.y);


          <script src="https://github.com/photonstorm/phaser-ce/releases/download/v2.11.1/phaser.min.js"></script>





          (Also, I had to change the 100, 100 to 0, 0)






          share|improve this answer




























            0














            It's because you're declaring them in the statement, so the declaration is newly hit each time and the variables are created afresh.



            Firsly, you need to create the variables outside of the statement.



            And then, to fix your issue, I would use a bool to lock them in.



            Something like this:



            var unitLine;
            var firstX;
            var firstY;
            var needToset_XY = true;

            if(game.input.activePointer.isDown)

            if(needToset_XY)
            firstX = game.input.x;
            firstY = game.input.y;
            needToset_XY = false;


            unitLine = game.add.graphics(100, 100);
            unitLine.beginFill(0xFF3300);
            unitLine.lineStyle(10, 0xffd900, 1);

            unitLine.moveTo(firstX, firstY);
            unitLine.lineTo(game.input.x, game.input.y);



            This means the firstX and firstY values can't be changed after the first time.



            If this is all in a game loop, you'll need to declare the top four variables outside of the loop, otherwise they'll renew themselves each time.






            share|improve this answer






















            • It's telling me that i cant assign the firstX and firstY because they are declared as const
              – Ibrahim Fadel
              Nov 12 at 4:36










            • Ah, yeah, makes sense. Declare them as var instead, no harm will be done
              – Stuart Aitken
              Nov 12 at 4:37










            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%2f53255871%2fsave-mouse-location-then-have-a-second-value-that-updates%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            2














            The problem is that you're setting firstX and firstY whenever the mouse isDown, so they're basically overwritten every frame that the mouse is down.



            To get around this, try using Phaser's game.input.onDown function:






            var game = new Phaser.Game(500, 500, Phaser.CANVAS, 'test', 
            preload: preload,
            create: create,
            update: update
            );

            function preload()

            let firstX;
            let firstY;

            function create()
            game.input.onDown.add(function()
            firstX = game.input.x;
            firstY = game.input.y;
            , this);

            var unitLine;

            function update()
            if (game.input.activePointer.isDown)
            unitLine = game.add.graphics(0, 0);
            unitLine.beginFill(0xFF3300);
            unitLine.lineStyle(10, 0xffd900, 1);

            unitLine.moveTo(firstX, firstY);
            unitLine.lineTo(game.input.x, game.input.y);


            <script src="https://github.com/photonstorm/phaser-ce/releases/download/v2.11.1/phaser.min.js"></script>





            (Also, I had to change the 100, 100 to 0, 0)






            share|improve this answer

























              2














              The problem is that you're setting firstX and firstY whenever the mouse isDown, so they're basically overwritten every frame that the mouse is down.



              To get around this, try using Phaser's game.input.onDown function:






              var game = new Phaser.Game(500, 500, Phaser.CANVAS, 'test', 
              preload: preload,
              create: create,
              update: update
              );

              function preload()

              let firstX;
              let firstY;

              function create()
              game.input.onDown.add(function()
              firstX = game.input.x;
              firstY = game.input.y;
              , this);

              var unitLine;

              function update()
              if (game.input.activePointer.isDown)
              unitLine = game.add.graphics(0, 0);
              unitLine.beginFill(0xFF3300);
              unitLine.lineStyle(10, 0xffd900, 1);

              unitLine.moveTo(firstX, firstY);
              unitLine.lineTo(game.input.x, game.input.y);


              <script src="https://github.com/photonstorm/phaser-ce/releases/download/v2.11.1/phaser.min.js"></script>





              (Also, I had to change the 100, 100 to 0, 0)






              share|improve this answer























                2












                2








                2






                The problem is that you're setting firstX and firstY whenever the mouse isDown, so they're basically overwritten every frame that the mouse is down.



                To get around this, try using Phaser's game.input.onDown function:






                var game = new Phaser.Game(500, 500, Phaser.CANVAS, 'test', 
                preload: preload,
                create: create,
                update: update
                );

                function preload()

                let firstX;
                let firstY;

                function create()
                game.input.onDown.add(function()
                firstX = game.input.x;
                firstY = game.input.y;
                , this);

                var unitLine;

                function update()
                if (game.input.activePointer.isDown)
                unitLine = game.add.graphics(0, 0);
                unitLine.beginFill(0xFF3300);
                unitLine.lineStyle(10, 0xffd900, 1);

                unitLine.moveTo(firstX, firstY);
                unitLine.lineTo(game.input.x, game.input.y);


                <script src="https://github.com/photonstorm/phaser-ce/releases/download/v2.11.1/phaser.min.js"></script>





                (Also, I had to change the 100, 100 to 0, 0)






                share|improve this answer












                The problem is that you're setting firstX and firstY whenever the mouse isDown, so they're basically overwritten every frame that the mouse is down.



                To get around this, try using Phaser's game.input.onDown function:






                var game = new Phaser.Game(500, 500, Phaser.CANVAS, 'test', 
                preload: preload,
                create: create,
                update: update
                );

                function preload()

                let firstX;
                let firstY;

                function create()
                game.input.onDown.add(function()
                firstX = game.input.x;
                firstY = game.input.y;
                , this);

                var unitLine;

                function update()
                if (game.input.activePointer.isDown)
                unitLine = game.add.graphics(0, 0);
                unitLine.beginFill(0xFF3300);
                unitLine.lineStyle(10, 0xffd900, 1);

                unitLine.moveTo(firstX, firstY);
                unitLine.lineTo(game.input.x, game.input.y);


                <script src="https://github.com/photonstorm/phaser-ce/releases/download/v2.11.1/phaser.min.js"></script>





                (Also, I had to change the 100, 100 to 0, 0)






                var game = new Phaser.Game(500, 500, Phaser.CANVAS, 'test', 
                preload: preload,
                create: create,
                update: update
                );

                function preload()

                let firstX;
                let firstY;

                function create()
                game.input.onDown.add(function()
                firstX = game.input.x;
                firstY = game.input.y;
                , this);

                var unitLine;

                function update()
                if (game.input.activePointer.isDown)
                unitLine = game.add.graphics(0, 0);
                unitLine.beginFill(0xFF3300);
                unitLine.lineStyle(10, 0xffd900, 1);

                unitLine.moveTo(firstX, firstY);
                unitLine.lineTo(game.input.x, game.input.y);


                <script src="https://github.com/photonstorm/phaser-ce/releases/download/v2.11.1/phaser.min.js"></script>





                var game = new Phaser.Game(500, 500, Phaser.CANVAS, 'test', 
                preload: preload,
                create: create,
                update: update
                );

                function preload()

                let firstX;
                let firstY;

                function create()
                game.input.onDown.add(function()
                firstX = game.input.x;
                firstY = game.input.y;
                , this);

                var unitLine;

                function update()
                if (game.input.activePointer.isDown)
                unitLine = game.add.graphics(0, 0);
                unitLine.beginFill(0xFF3300);
                unitLine.lineStyle(10, 0xffd900, 1);

                unitLine.moveTo(firstX, firstY);
                unitLine.lineTo(game.input.x, game.input.y);


                <script src="https://github.com/photonstorm/phaser-ce/releases/download/v2.11.1/phaser.min.js"></script>






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 12 at 4:33









                Davіd

                3,59541635




                3,59541635























                    0














                    It's because you're declaring them in the statement, so the declaration is newly hit each time and the variables are created afresh.



                    Firsly, you need to create the variables outside of the statement.



                    And then, to fix your issue, I would use a bool to lock them in.



                    Something like this:



                    var unitLine;
                    var firstX;
                    var firstY;
                    var needToset_XY = true;

                    if(game.input.activePointer.isDown)

                    if(needToset_XY)
                    firstX = game.input.x;
                    firstY = game.input.y;
                    needToset_XY = false;


                    unitLine = game.add.graphics(100, 100);
                    unitLine.beginFill(0xFF3300);
                    unitLine.lineStyle(10, 0xffd900, 1);

                    unitLine.moveTo(firstX, firstY);
                    unitLine.lineTo(game.input.x, game.input.y);



                    This means the firstX and firstY values can't be changed after the first time.



                    If this is all in a game loop, you'll need to declare the top four variables outside of the loop, otherwise they'll renew themselves each time.






                    share|improve this answer






















                    • It's telling me that i cant assign the firstX and firstY because they are declared as const
                      – Ibrahim Fadel
                      Nov 12 at 4:36










                    • Ah, yeah, makes sense. Declare them as var instead, no harm will be done
                      – Stuart Aitken
                      Nov 12 at 4:37















                    0














                    It's because you're declaring them in the statement, so the declaration is newly hit each time and the variables are created afresh.



                    Firsly, you need to create the variables outside of the statement.



                    And then, to fix your issue, I would use a bool to lock them in.



                    Something like this:



                    var unitLine;
                    var firstX;
                    var firstY;
                    var needToset_XY = true;

                    if(game.input.activePointer.isDown)

                    if(needToset_XY)
                    firstX = game.input.x;
                    firstY = game.input.y;
                    needToset_XY = false;


                    unitLine = game.add.graphics(100, 100);
                    unitLine.beginFill(0xFF3300);
                    unitLine.lineStyle(10, 0xffd900, 1);

                    unitLine.moveTo(firstX, firstY);
                    unitLine.lineTo(game.input.x, game.input.y);



                    This means the firstX and firstY values can't be changed after the first time.



                    If this is all in a game loop, you'll need to declare the top four variables outside of the loop, otherwise they'll renew themselves each time.






                    share|improve this answer






















                    • It's telling me that i cant assign the firstX and firstY because they are declared as const
                      – Ibrahim Fadel
                      Nov 12 at 4:36










                    • Ah, yeah, makes sense. Declare them as var instead, no harm will be done
                      – Stuart Aitken
                      Nov 12 at 4:37













                    0












                    0








                    0






                    It's because you're declaring them in the statement, so the declaration is newly hit each time and the variables are created afresh.



                    Firsly, you need to create the variables outside of the statement.



                    And then, to fix your issue, I would use a bool to lock them in.



                    Something like this:



                    var unitLine;
                    var firstX;
                    var firstY;
                    var needToset_XY = true;

                    if(game.input.activePointer.isDown)

                    if(needToset_XY)
                    firstX = game.input.x;
                    firstY = game.input.y;
                    needToset_XY = false;


                    unitLine = game.add.graphics(100, 100);
                    unitLine.beginFill(0xFF3300);
                    unitLine.lineStyle(10, 0xffd900, 1);

                    unitLine.moveTo(firstX, firstY);
                    unitLine.lineTo(game.input.x, game.input.y);



                    This means the firstX and firstY values can't be changed after the first time.



                    If this is all in a game loop, you'll need to declare the top four variables outside of the loop, otherwise they'll renew themselves each time.






                    share|improve this answer














                    It's because you're declaring them in the statement, so the declaration is newly hit each time and the variables are created afresh.



                    Firsly, you need to create the variables outside of the statement.



                    And then, to fix your issue, I would use a bool to lock them in.



                    Something like this:



                    var unitLine;
                    var firstX;
                    var firstY;
                    var needToset_XY = true;

                    if(game.input.activePointer.isDown)

                    if(needToset_XY)
                    firstX = game.input.x;
                    firstY = game.input.y;
                    needToset_XY = false;


                    unitLine = game.add.graphics(100, 100);
                    unitLine.beginFill(0xFF3300);
                    unitLine.lineStyle(10, 0xffd900, 1);

                    unitLine.moveTo(firstX, firstY);
                    unitLine.lineTo(game.input.x, game.input.y);



                    This means the firstX and firstY values can't be changed after the first time.



                    If this is all in a game loop, you'll need to declare the top four variables outside of the loop, otherwise they'll renew themselves each time.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 12 at 4:43

























                    answered Nov 12 at 4:31









                    Stuart Aitken

                    5810




                    5810











                    • It's telling me that i cant assign the firstX and firstY because they are declared as const
                      – Ibrahim Fadel
                      Nov 12 at 4:36










                    • Ah, yeah, makes sense. Declare them as var instead, no harm will be done
                      – Stuart Aitken
                      Nov 12 at 4:37
















                    • It's telling me that i cant assign the firstX and firstY because they are declared as const
                      – Ibrahim Fadel
                      Nov 12 at 4:36










                    • Ah, yeah, makes sense. Declare them as var instead, no harm will be done
                      – Stuart Aitken
                      Nov 12 at 4:37















                    It's telling me that i cant assign the firstX and firstY because they are declared as const
                    – Ibrahim Fadel
                    Nov 12 at 4:36




                    It's telling me that i cant assign the firstX and firstY because they are declared as const
                    – Ibrahim Fadel
                    Nov 12 at 4:36












                    Ah, yeah, makes sense. Declare them as var instead, no harm will be done
                    – Stuart Aitken
                    Nov 12 at 4:37




                    Ah, yeah, makes sense. Declare them as var instead, no harm will be done
                    – Stuart Aitken
                    Nov 12 at 4:37

















                    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%2f53255871%2fsave-mouse-location-then-have-a-second-value-that-updates%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







                    這個網誌中的熱門文章

                    How to read a connectionString WITH PROVIDER in .NET Core?

                    In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

                    Museum of Modern and Contemporary Art of Trento and Rovereto