4 Replies Latest reply on Sep 21, 2012 6:38 AM by mdionisio71

    Using keypressed method

    mdionisio71

      Hello,

       

      I have a simple game running here:

      http://www.cim-ave.pt/tocas/lab/index.htm

       

      The game runs smoothly on Chrome and Firefox, but not on Internet Explorer.

       

      The game uses a "repeat while" loop and the "_key.keyPressed()" method to detect if one of the cursor keys is being pressed and, if so, the character is moved according to the corresponding direction.

       

      For some reason, on my pc, with Internet Explorer 9 (and Windows 7 and Shockwave Player 11.6), if I press one of the cursor keys the character starts moving but doesn't stop when I release the key. This doesn't happen if I use another browser like Chrome or Firefox. And to make it worst, there's another machine here with the same software (Internet Explorer 9, Windows 7 and Shockwave Player 11.6) where the game behaves as it is supposed to.

       

      So, it seems that for some odd reason, on my machine and with Internet Explorer 9, the "_key.KeyPressed()" event is not updated with the key state (pressed or not pressed) inside the repeat while loop.

       

      Does anybody have a clue about what might be the problem?

       

      Thanx!

        • 1. Re: Using keypressed method
          Dean Utian Adobe Community Professional

          Could you posr the code used in that part of the game?

           

          Dean

          • 2. Re: Using keypressed method
            mdionisio71 Level 1

            Yes, of course!

            I'd like to add that it's not only on my machine that this happens. There other machines where it happens, and others where it doens't, with the same software configuration: Windows 7, Internet Explorer 9 and Shockwave 11.6.

             

            Here it goes the code that matters. The handler moveObj is called from a keyDown event handler.

             

            ----------------------

            on moveObj me,xy,nomeSptBase

              global gNSprPorta

             

              [do some inicialization stuff]

             

              ePorta = FALSE

              startTimer

              repeat while (locOld <> the loc of sprite sptObj) and (_key.keyPressed(123) or _key.keyPressed(124) or _key.keyPressed(125) or _key.keyPressed(126)) and not ePorta -- mouseDown and (spriteBot = the rollOver)

                ePorta = veSeEPorta(me,listSptsPt)

                --put String("#" & _key.keyCode & "#")

                if the timer > tempoDelay then

                  startTimer

                  set locOld = the loc of sprite sptObj

                  set the loc of sprite sptObj to (locOld + xy)

                  updateStage

                end if

                member("Debug").text = member("Debug").text & ENTER & _key.keyCode

              end repeat

             

              [do some more stuff]

             

            end

            • 3. Re: Using keypressed method
              Dean Utian Adobe Community Professional

              I tried your game on IE and experienced the problem. It did not happen for me on Firefox.

               

              I see you have a repeat loop with an updateStage. This is not the best approach. As in another thread, the advice is to avoid this and rather loop within a frame. Say we created a property variable called pKeyPressed, where 1 is when they are pressed, 0 when it is not, you could toggle this property and then loop in the frame when the property is 1.

               

              For example:

              property pKeyPressed

              property tempoDelay, locOld, sptObjm xy

               

              on beginSprite me

                pKeyPressed = 0

              end

               

              on moveObj me,xy,nomeSptBase

               

              -- do whatever

               

              if (locOld <> sprite(sptObj).loc) AND (_key.keyPressed(123) or _key.keyPressed(124) or _key.keyPressed(125) or _key.keyPressed(126)) AND not ePorta then

              pKeyPressed = 1

              else

                pKeyPressed = 0

              -- do other stuff (which would follow after your previous repeat loop

              end if

               

              end

               

              on exitFrame me

              if pKeyPressed = 1 then

               

              if the timer > tempoDelay then 

                startTimer

                locOld = sprite(sptObj).loc

                sprite(sptObj).loc = (locOld + xy)

              end if

               

              member("Debug").text = member("Debug").text & ENTER & _key.keyCode

               

              end

               

              --

               

              I changed:
              the loc of sprite sptObj

               

              to

              sprite(sptObj).loc

               

               

              Is there any reason you're using ENTER instead of RETURN?

               

              Dean

              • 4. Re: Using keypressed method
                mdionisio71 Level 1

                Hi Dean,

                 

                Thank you for your suggestion!

                I'll give it a try as soon as I can and I'll let you know if it worked.

                About the 'ENTER', there is not a particular reason, I guess I'm just a bit rusty on lingo :-)

                 

                Dionísio