ArrowKeys have a delay in my 2D game

Creating Games? Developing something for fun?

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
Moorky
Posts: 3
Joined: Sat Nov 08, 2014 11:30 pm

ArrowKeys have a delay in my 2D game

Post by Moorky » Mon Nov 10, 2014 5:07 pm

Hey Guys! :)
Could anyone please tell me how I can reduce the delay of my object that moves if I use my arrow keys?

Here is the code of the movement:

Code: Select all

on arrowkey x
   
   --Controls
   if x is "up" then
      set the top of graphic "box" to the top of graphic "box" - 3
      if top of graphic "box" < 0 then
         set the top of graphic "box" to 0
      end if
      if intersect( graphic "box" , group "maze" , "opaque pixels") then
         set the bottom of graphic "box" to the bottom of graphic "box" + 3.1
      end if 
   end if
   
   if x is "down" then
      set the bottom of graphic "box" to the bottom of graphic "box" + 3
      if the bottom of graphic "box" > the bottom of card "mycard" then
         set the bottom of graphic "box" to the bottom of card "mycard"
      end if
      if intersect( graphic "box" , group "maze" , "opaque pixels") then
         set the bottom of graphic "box" to the bottom of graphic "box" - 3.1
      end if 
   end if
   
   if x is "left" then
      set the left of graphic "box" to the left of graphic "box" - 3
      if the left of graphic "box" < 0 then
         set the left of graphic "box" to 0
      end if
      if intersect( graphic "box" , group "maze" , "opaque pixels") then
         set the left of graphic "box" to the left of graphic "box" + 3.1
      end if 
   end if
   
   if x is "right" then
      set the right of graphic "box" to the right of graphic "box" + 3
      if right of graphic "box" > the right of card "mycard" then
         set the right of graphic "box" to the right of card "mycard"
      end if
      if intersect( graphic "box" , group "maze" , "opaque pixels") then
         set the right of graphic "box" to the right of graphic "box" - 3.1
      end if 
   end if
   --
end arrowKey
With best regards, Moorky.

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am
Location: Palo Alto

Re: ArrowKeys have a delay in my 2D game

Post by Simon » Wed Nov 12, 2014 2:12 am

Hi Moorky,
Do you have any "wait" messages still??

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9823
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: ArrowKeys have a delay in my 2D game

Post by FourthWorld » Wed Nov 12, 2014 2:27 am

Which version are you using, and on which OS version?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Newbie4
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 327
Joined: Sun Apr 15, 2012 1:17 am
Location: USA
Contact:

Re: ArrowKeys have a delay in my 2D game

Post by Newbie4 » Thu Nov 20, 2014 3:27 am

I tried it with version 6.6.3 and it works fine. If you want it to move faster, use a number larger than 3 (like 10) and see if that is more like the speed you were looking for.

Other than that, I do not know what you mean by delay.

I also tried it with button "box" instead of graphic "box" to see if it made a difference but both were the same speed
Cyril Pruszko
https://sites.google.com/a/pgcps.org/livecode/
https://sites.google.com/a/setonhs.org/app-and-game-workshop/home
https://learntolivecode.com/

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am
Location: Palo Alto

Re: ArrowKeys have a delay in my 2D game

Post by Simon » Thu Nov 20, 2014 3:44 am

Hi Gang,
My post was a question to a previous posting by Moorky in which Moorky had a gameLoop running with a wait command in it (and this code).
Just so you all don't think I'm crazy :)

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: ArrowKeys have a delay in my 2D game

Post by sturgis » Wed Dec 17, 2014 11:03 pm

If the gameloop is being used (as it seems) using on arrowkey is probably not the best method.
Instead, in the updatescreen handle do something like..

Code: Select all

on updatescreen
   dosomethinghere
   dosomethingelsehere
moveGraphicBox

end updatescreen

command moveGraphicBox

end moveGraphicBox
if 65363 is among the items of the keysdown then -- right arrow is down
   if the right of grc "box" + 3 > the right of this card then 
      set the right of grc "box" to the right of this card.
  else
     set the right of grc "box" to the right of grc "box" + 3
end if
   if intersect( graphic "box",group "maze", "opaque pixels") then set the right of grc "box" to the right of grc "box" - 3.1 -- 3.1? 

--Do the same for the other arrow keys. left is 65361, up is 65362, down is 65364
end moveGraphicBox
The nice thing about doing it this way is that you can check for other keys too. So you could have a "checkKeys" handler that then calls a moveright, moveleft,moveup,movedown command as well as any other key initiated tasks you need to handle.

Post Reply

Return to “Games”