Variables, image flip and delay? Going insane [Solved]

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
trenatos
Posts: 189
Joined: Wed Jul 03, 2013 6:46 am
Location: Virginia, USA
Contact:

Variables, image flip and delay? Going insane [Solved]

Post by trenatos » Sat Jul 06, 2013 9:53 pm

As I'm messing about reading a tutorial on making a simple game, I thought I'd make a few small simple changes.

I'm trying to make the image (Button) flip based on a variable, but I can't get it to work.

Using a global named direction, defaults to "left"
In a key function for moving, I added this:

Code: Select all

if x is "right" then
      set the right of the button "box" to the right of the button "box" +10
      if direction <> "right" then
         flip image "dragon.jpg" horizontal
      end if
      put "right" into direction
   end if
However, the image keeps flipping every time I press the right button, and there seems to be a delay of one keypress for the actual flipping.

What could I be doing wrong?
Last edited by trenatos on Sun Jul 07, 2013 11:48 pm, edited 1 time in total.
Marcus

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am
Location: Bordeaux, France

Re: Variables, image flip and delay? Going insane

Post by Dixie » Sun Jul 07, 2013 5:36 am

Not too sure what you're after here but...

Code: Select all

   if x = "right" then
      set the right of button "box" to the right of button "box" +10
   else
      flip image "dragon.jpg" horizontal
   end if
   put "right" into direction

trenatos
Posts: 189
Joined: Wed Jul 03, 2013 6:46 am
Location: Virginia, USA
Contact:

Re: Variables, image flip and delay? Going insane

Post by trenatos » Sun Jul 07, 2013 5:47 am

Ok, I have a picture of a dragon, I want it to point in the direction of travel.

x is simply the key being pressed.

I have a global variable called direction, which equals either right or left, depending on the last direction (button) pushed.

I'm having trouble flipping the image according the state of the variable.

For some reason, it seems to "skip" a keypress, and flip on every keypress after that.
Marcus

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am
Location: Bordeaux, France

Re: Variables, image flip and delay? Going insane

Post by Dixie » Sun Jul 07, 2013 6:25 am

In the openCard handler...

Code: Select all

global direction

on openCard
   put "left" into direction
   put direction into fld 1
end openCard
In the script of the button

Code: Select all

global direction

on mouseUp
   /* I'm assuming that the default.. ie. on starting the direction will be left */
   if direction = "right" then
      put "left" into direction
   else
      put "right" into direction
   end if
   flip image 1 horizontal
   put direction into fld 1
end mouseUp
See attached...
Attachments
flip.livecode.zip
(130.27 KiB) Downloaded 239 times

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9647
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Variables, image flip and delay? Going insane

Post by dunbarx » Sun Jul 07, 2013 6:31 am

Hi..

I would not use a global at all, just set a custom property of the image. I have one image and two buttons on a card. One button is named "rt" and the other is named "lft" I have this in the card script:

Code: Select all

on mouseUp
   switch 
      case  the short name of the target = "rt" and the direction of img 1 = "rt"
         set the left of img 1 to the the left of img 1 + 10
         break
      case  the short name of the target = "rt"  and the direction of img 1 = "lft"
         flip img 1 horizontal
         set the left of img 1 to the the left of img 1 + 10
         break
      case  the short name of the target = "lft" and the direction of img 1 = "lft"
         set the left of img 1 to the the left of img 1 - 10
         break
      case  the short name of the target = "lft" and the direction of img 1 = "rt"
         flip img 1 horizontal
         set the left of img 1 to the the left of img 1 - 10
         break
   end switch
   
   set the direction of img 1 to the short name of the target
   
end mouseUp
Craig Newman

trenatos
Posts: 189
Joined: Wed Jul 03, 2013 6:46 am
Location: Virginia, USA
Contact:

Re: Variables, image flip and delay? Going insane

Post by trenatos » Sun Jul 07, 2013 8:28 am

Thanks guys, I got it working :)
Together with my mostly-working socket client/server I'll be making a simple game with online highscores.
Marcus

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”