Making Buttons Solid

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

d.m.holdawayGA2553
Posts: 81
Joined: Mon Jan 09, 2012 4:48 pm

Re: Making Buttons Solid

Post by d.m.holdawayGA2553 » Sat Mar 24, 2012 12:14 am

nope i cant do the random button thing

my mind is blank.

Grade = F

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: Making Buttons Solid

Post by mwieder » Sat Mar 24, 2012 12:26 am

OK - here's a hint:

There are (at least) a couple of ways to get a random number:

1. Use the random() function - the documentation for it is in the dictionary

Code: Select all

put random(10) into tSomeRandomNumber
2. Use the any keyword:

Code: Select all

put any item of "14,78,9,63,2,65,35,4,5,8" into tSomeRandomNumber

d.m.holdawayGA2553
Posts: 81
Joined: Mon Jan 09, 2012 4:48 pm

Re: Making Buttons Solid

Post by d.m.holdawayGA2553 » Sat Mar 24, 2012 2:26 am

i am really struggling with this,

Code: Select all


on mouseUp
   
   put random(10) into tMovex
   put random(10) into tMovey
   put the loc of btn "mover" into startLoc
   
   repeat until intersect(btn "mover",button "theTarget") = "true"
      get the loc of btn "mover" 
      set the loc of btn "mover" to (tMovex + 5) & "," & (tMovey + 5)
      wait 2
      if the loc of button "mover" is not within the rect of field "screen" then
         set the loc of btn "mover" to startLoc
      end if
   end repeat



end mouseUp
i understand random and put and i have a field the size of my card

but my mover button just ends up in the top left corner?

the start location of the button mover is in the middle of the screen.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10320
Joined: Wed May 06, 2009 2:28 pm

Re: Making Buttons Solid

Post by dunbarx » Sat Mar 24, 2012 3:34 am

Struggle, profanity, triumph.

These are the stages of learning to program. And of writing programs. And of debugging. And...

The intersect function was tested through the loop and forked when you hit the target in that first example. But it was a set-up. the movement was restricted to the right, and the target was already in the path.

In the screen model, you have to use the borders of the card as an intermediate target to bounce off of. You will not be able to use the intersect function in this case. If you read the docs, you can see that this function is tailor made for a pair of objects. It has no way of knowing that you are at the edge of the card window. But there are other ways to know that. Read up on the "rect", "top", "left", bottom, and "right" keywords. You will HAVE to know what an item is.

When you do get a "hit" on a card edge, you will want to change direction. You need to make sure that your new random offset values make sense based on which edge you hit. But they stay the same until you hit that edge. Then they can change again.

You yourself saw that you could move the button in any direction by modifying both x and y offsets. So you need to set that up, and make your initial offsets random.

Or maybe start with having the "mover" button just bounce left and right from the edges. Then do diagonals and random changes in x,y.

You are in the struggle stage. You will be fine.

Craig Newman

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10320
Joined: Wed May 06, 2009 2:28 pm

Re: Making Buttons Solid

Post by dunbarx » Sat Mar 24, 2012 4:21 am

Just so you know, this random move exercise is not a trivial thing. It is not hard, but involves lots of basic LC stuff, and is really an intermediate level project. You will get support here until you either become proficient or die trying. Keep writing back, keep experimenting.

Craig Newman

d.m.holdawayGA2553
Posts: 81
Joined: Mon Jan 09, 2012 4:48 pm

Re: Making Buttons Solid

Post by d.m.holdawayGA2553 » Sat Mar 24, 2012 7:34 pm

Code: Select all

 repeat forever
      get the loc of button "mover"
      set the loc of btn "mover" to (item 1 of it + 1) & "," & (item 2 of it)
      wait 2
      if (the right of button "mover" - 10) > the right of this card then
         get the loc of button "mover"
         set the loc of btn "mover" to (item 1 of it - 1) & "," & (item 2 of it)
         wait 2
      end if
      if (the left of button "mover" - 10) > the left of this card then
      end if
   end repeat
i cant seem to get it to exit the loop, i have tired a few different options and it either keeps going off the page or it gets to the end of the page and the wobbles.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10320
Joined: Wed May 06, 2009 2:28 pm

Re: Making Buttons Solid

Post by dunbarx » Sun Mar 25, 2012 4:45 am

OK, I am giving you this because you have been good. You are therefore obliged to spend much time and sweat making it your own. In the same three button card, place this in the starting button script

Code: Select all

on mouseUp
       put random(10) - random(10) into xOffset
         put random(10) - random(10) into yOffset
   put item 3 of the rect of this cd into rightEdge
   put item 4 of the rect of this cd into bottomEdge
         repeat until the top of btn "mover" < 0 or the bottom of btn "mover" > bottomEdge\
            or the left of btn "mover" < 0 or the right of btn "mover" > rightEdge
            get the loc of btn "mover" 
            set the loc of btn "mover" to (item 1 of it + xOffset) & "," & (item 2 of it + yOffset)
      wait 2
            get the rect of btn "mover"
            switch
                  case item 1 of  it <= 0
                        set the loc of btn "mover" to (item 1 of the loc of btn "mover"+ abs(xOffset)) & "," & (item 2 of the loc of btn "mover" )
                        put 0 -  xOffset into xOffset
                        break
                  case item 2  of it <= 0
                        set the loc of btn "mover" to (item 1 of the loc of btn "mover") & "," & (item 2 of the loc of btn "mover"  + abs(yOffset))
                        put 0 -  yOffset into yOffset          
                        break
                  case item 3  of it >= rightEdge
                        set the loc of btn "mover" to (item 1 of the loc of btn "mover" - xOffset) & "," & (item 2 of the loc of btn "mover" )
                        put 0 -  xOffset into xOffset
                        break
                  case item 4 of it >= bottomEdge
                        set the loc of btn "mover" to (item 1 of the loc of btn "mover") & "," & (item 2 of the loc of btn "mover" - yOffset )
                        put 0 - yOffset into yOffset
                        break
            end switch
            if  intersect(btn "mover",btn "tTarget") = "true" then exit to top
         end repeat
end mouseUp
Now and then there could be an issue with this code. Not that it doesn't work, mind you. What do you think about that?

Can you find the rationale behind each line of the code? Homework.

There are more elegant ways to do this, relying less on repeat loops, by using the "send" command with its "in time" variant to a subroutine. But that is for another day.

Craig Newman

d.m.holdawayGA2553
Posts: 81
Joined: Mon Jan 09, 2012 4:48 pm

Re: Making Buttons Solid

Post by d.m.holdawayGA2553 » Sun Mar 25, 2012 10:15 am

thats really good, bit mind blowing BUT still really good.

its pretty far out from what i needed but i this hase been a wonderful learning experience

so thanks to everyone who has added to this post with advice and help.

now i just need to work out how i can work this with touch instead of buttons.

thanks again tho everyone!

d.m.holdawayGA2553
Posts: 81
Joined: Mon Jan 09, 2012 4:48 pm

Re: Making Buttons Solid

Post by d.m.holdawayGA2553 » Sun Mar 25, 2012 12:31 pm

Code: Select all

if  intersect(btn "mover",btn "tTarget") = "true" then exit to top
can i use group instead of button

Code: Select all

if  intersect(group "movers",btn "tTarget") = "true" then exit to top

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10320
Joined: Wed May 06, 2009 2:28 pm

Re: Making Buttons Solid

Post by dunbarx » Sun Mar 25, 2012 6:27 pm

This is the sort of question you answer by trying it out.

Craig Newman

d.m.holdawayGA2553
Posts: 81
Joined: Mon Jan 09, 2012 4:48 pm

Re: Making Buttons Solid

Post by d.m.holdawayGA2553 » Sun Mar 25, 2012 6:43 pm

yes your right and on that note, i feel i have no more questions to ask on this subject.

thanks again

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10320
Joined: Wed May 06, 2009 2:28 pm

Re: Making Buttons Solid

Post by dunbarx » Sun Mar 25, 2012 9:36 pm

That would be either great news or a pity. There is always more to ask and to know.

Craig Newman

d.m.holdawayGA2553
Posts: 81
Joined: Mon Jan 09, 2012 4:48 pm

Re: Making Buttons Solid

Post by d.m.holdawayGA2553 » Mon Mar 26, 2012 11:23 am

well i got loads of questions, i was just being polite lol

thanks

Post Reply