Button Question

Getting into LiveCode for iOS? Ask your questions here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
cu3e
Posts: 46
Joined: Mon May 23, 2011 10:03 am

Button Question

Post by cu3e » Mon May 23, 2011 10:07 am

Hello, how Do I create a button that changes the look when i click on it (like button_normal and button_pressed) and after release the button it changes to a third stage?

Klaus
Posts: 14182
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Button Question

Post by Klaus » Mon May 23, 2011 12:33 pm

Hi cu3e,

you could use different icons for the different "states".
In the inspector for the buttons you can check "Icons and border" and set the icons.

Is that what you are looking for?


Best

Klaus

cu3e
Posts: 46
Joined: Mon May 23, 2011 10:03 am

Re: Button Question

Post by cu3e » Mon May 23, 2011 12:37 pm

I think thats what Iam looking for :) Thank you.

Can you tell me the meaning of the different states?

Icon
Hilight Icon
Disabled Icon
Visited Icon
Hover Icon
-- I understand

but what is

armed icon?

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4166
Joined: Sun Jan 07, 2007 9:12 pm

Re: Button Question

Post by bn » Mon May 23, 2011 1:06 pm

Hi cu3e,

look at the attached stack. Three states of the button. The visited of the button is set in the button on mouseDown.

Look at the icon numbers for the button in the properties inspector. And look at the id of each of the images to the left. That is how you assign an image to a button.

Kind regards

Bernd
Attachments
buttonThreeStates.livecode.zip
(11.12 KiB) Downloaded 311 times

Klaus
Posts: 14182
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Button Question

Post by Klaus » Mon May 23, 2011 1:14 pm

Hi cu3e,

take a look at Bernds stack, seeing is believing :D

"armed"/"armedicon" is similar to "hilite" but yet different.:roll:
To be honest, I still don't get the "armed" thing completely after
using Livecode for over 10 years 8)

Check the dictionary for "armed" rep. "armedicon" for more info.


Best

Klaus

cu3e
Posts: 46
Joined: Mon May 23, 2011 10:03 am

Re: Button Question

Post by cu3e » Mon May 23, 2011 1:40 pm

thank you both! Now I understand it. One more Question regarding buttons.

How can I include a sound to the clicking of a button?

Klaus
Posts: 14182
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Button Question

Post by Klaus » Mon May 23, 2011 1:46 pm

Hi cu3e,

EDIT!
Onerlooked the forum section!
So you need to play an external file on iOS.

Add the clicksound file to your standalone (copy files) and add this to your "mouseup" handler(s):

Code: Select all

on mouseup
  play (specialfolderpath("engine") &"/click.aif")
  ## Do your other mouseup stuff...
  ## ...
end mouseup
If you need this click sound more often, I would use a separate command for the sound.
Add this to the main stack script:

Code: Select all

command playClick
  play (specialfolderpath("engine") &"/click.aif")
end playClick
Use it like this anywhere you need to:

Code: Select all

on mouseup
  playplayClick
  ## Do your other mouseup stuff...
  ## ...
end mouseup
I highly recommend to check these stack here:
http://www.runrev.com/developers/lesson ... nferences/

Great learning resources!


Best

Klaus

cu3e
Posts: 46
Joined: Mon May 23, 2011 10:03 am

Re: Button Question

Post by cu3e » Mon May 23, 2011 2:47 pm

ok Thank you.

One Question. Is it possible to activate a sound as long as a icon is moved? Iam thinking about the multitouch, drag and drop. Like moving a icon from the left to the right side with the finger, during the movement I would like to have a sound activated which stops as soon as the finger moved away from the item.

Klaus
Posts: 14182
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Button Question

Post by Klaus » Mon May 23, 2011 3:46 pm

Hi cu3e,

sure, just start the sound (looping?) when the touch starts (or "mousedown")
and stop the sound when the touch release/stop (or "on mouseup")

Like this for a mouseX script:

Code: Select all

on mousedown
   play (specialfolderpath("engine") &"/nice_loopable_sound.aif") looping
end mousedown

on mouseup
  play empty
end mouseup

## Important!
on mouserelease
  play empty
end mouserelase
Best

Klaus

cu3e
Posts: 46
Joined: Mon May 23, 2011 10:03 am

Re: Button Question

Post by cu3e » Mon May 23, 2011 4:03 pm

Awesome help, thank you so much.

Do you know if the sound command for android is the same as for iOS since I want to programme cross-platform?

play (specialfolderpath("engine") &"/nice_loopable_sound.aif")

Klaus
Posts: 14182
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Button Question

Post by Klaus » Mon May 23, 2011 4:15 pm

Hi cu3e,

sorry, no idea about Android. Check the "Android Release Notes": Menu -> Help


Best

Klaus

cu3e
Posts: 46
Joined: Mon May 23, 2011 10:03 am

Re: Button Question

Post by cu3e » Mon May 23, 2011 6:35 pm

Last question :)

Is it possible to have a button visible in the editor mode, but invisible in the running mode? I want to create something like "hotspots" on a picture, where the user can click on it and here a sound for example or switching cards. Therefore the button should be invisible but for organization reasons I would like to have them visible in editor.

Klaus
Posts: 14182
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Button Question

Post by Klaus » Mon May 23, 2011 7:38 pm

Hi cu3e,
cu3e wrote:Last question :)
I strongly doubt! :D

OK, this is not possible directly, an object is always visible or not!

But you can use an old trick to simulate this:
1. Create a filled graphic or button (color does not matter)
2. Resize and position it as needed
3. In the "Blending" section of the Inspector of that graphic/button, select "noop" in the list!
This will make the graphic invisible for our eyes but not for the "mouse"!

You can find and select this object in the "Application Browser".


Best

Klaus

cu3e
Posts: 46
Joined: Mon May 23, 2011 10:03 am

Re: Button Question

Post by cu3e » Mon May 23, 2011 8:19 pm

Gr8t! Thank you very much Sir. Maybe I have another question :)

I saw the Sheep Game and have a question which fits to "buttons" :)

How can I archive it that the button with the sheep is randomize rotated like one sheep -5°, next sheep +25°?

Here the code...

Code: Select all

local sGameRunning, sTimer, sScore, sLevel, sDragging

on mouseDown
   if sGameRunning is true then
      put true into sDragging
   end if
end mouseDown

on touchMove pId, pX, pY
   if the cType of the target is "sheep" and sDragging is true then
      set the loc of the target to pX, pY
   end if
end touchMove

on touchEnd pId
   put false into sDragging
   if within(field "pen", the loc of the target) and the cType of the target is "sheep" then
      delete the target
      scoreIncrement
      if the number of controls in group "sheep" < 1 then
         add 1 to sLevel
         levelGenerate
      end if
   end if
end touchEnd

on gameInitialize
   lock screen
   put 0 into sScore
   put "x" && sScore into field "score"
   put 30 into sTimer
   put sTimer into field "time"
   put 1 into sLevel
   levelClear
   levelGenerate
   unlock screen
end gameInitialize

on levelGenerate
   lock screen
   reset the templateButton
   set the style of the templatebutton to "transparent"
   set the borderwidth of the templatebutton to "0"
   set the showname of the templatebutton to "false"
   set the width of the templatebutton to "60"
   set the height of the templatebutton to "50"
   set the icon of the templatebutton to "5002"
   set the visible of the templatebutton to true
   set the autohilite of the templatebutton to false
   set the cType of the templateButton to "sheep"
   set the sharedhilite of the templateButton to false
   set the traversalon of the templateButton to false
   set the threed of the templateButton to false
   set the hiliteborder of the templateButton to false
   repeat with x = 1 to sLevel
      levelAddSheep x
   end repeat
   unlock screen
end levelGenerate

on levelClear
   lock screen
   repeat with x = the number of controls in group "sheep" down to 1
      delete control x of group "sheep"
   end repeat
   unlock screen
end levelClear

on levelAddSheep pNumber
   repeat forever
      put random(300 - 20 + 1) + 20 - 1, random(400 - 20 + 1) + 20 - 1 into tLoc
      if tLoc is not within the rect of control "pen" then
         exit repeat
      end if 
   end repeat
   set the loc of the templateButton to tLoc
   create button ("sheep"&pNumber) in group "sheep"
end levelAddSheep

on scoreIncrement
   add 1 to sScore
   put "x" && sScore into field "score"
end scoreIncrement

on gameStart
   put true into sGameRunning
   set the icon of button "start" to 5001
   send "timerPlay" to me in 1 second
end gameStart

on gameStop
   put false into sGameRunning
   set the icon of button "start" to 5000
   gameInitialize
end gameStop

on timerPlay
   if sGameRunning is true then
      subtract 1 from sTimer
      if sTimer > 0 then
         put sTimer into field "time"
         send "timerPlay" to me in 1 second 
      else
         put false into sGameRunning
         put sScore into field "finalscore" of group "result"
         show group "result"
         set the layer of group "result" to top
      end if
   end if
end timerPlay

on motionStart pMotion
   if pMotion is "shake" then
      gameStop
      set the layer of group "result" to 1
      hide group "result"
   end if
end motionStart

Post Reply