Use a function outside a handler to manipulate a graphic

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
RalphORama
Posts: 7
Joined: Wed Oct 22, 2014 5:19 pm

Use a function outside a handler to manipulate a graphic

Post by RalphORama » Thu Nov 13, 2014 12:55 am

Hello! I'm trying to write a function that will manipulate a graphic, but I'm having some trouble getting it to do so. Here's my code:

Code: Select all

function changePoly fPoly,fStyle,fSides,fAngle,fBGColor
	set the style of graphic fPoly to fStyle
	set the polySides of graphic fPoly to fSides
	set the angle of graphic fPoly to fAngle
	set the backgroundColor of graphic fPoly to fBGColor
end changePoly

-- on mouseUp and all that jazz
switch
	case tButtonLabel = "Red"
		--changePoly("changeling","regular",8,68, red)
		-- currently, I'm using the same handlers in every part of my switch. 
		-- I'd like to write a function like the one commented out to simplify things greatly.

		set the style of graphic "changeling" to "regular"
		set the polySides of graphic "changeling" to 8
		set the angle of graphic "changeling" to 68
		set the backgroundColor of graphic "changeling" to red
		break
[/size]
When I uncomment the changePoly("changeling","regular",8,68, red) function and click my button, I get the error "Handler: Can't find handler" on the line where I call the function. Is there any way I can fix this, or do I have to violate DRY in the case of modifying graphics?

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

Re: Use a function outside a handler to manipulate a graphic

Post by Klaus » Thu Nov 13, 2014 1:20 am

Hi Ralph,

well, a functiuon is supposed top return something so you need to put that returned value into a variable, even if it is EMPTY like in your function.
I would suggest to turn your function into a simple handler, which makes more sense in this case:

Code: Select all

command changePoly fPoly,fStyle,fSides,fAngle,fBGColor
   set the style of graphic fPoly to fStyle
   set the polySides of graphic fPoly to fSides
   set the angle of graphic fPoly to fAngle
   set the backgroundColor of graphic fPoly to fBGColor
end changePoly

--on mouseUp and all that jazz
switch
   case tButtonLabel = "Red"

     ## This will work now, but without parens!
      changePoly "changeling","regular",8,68,red
      ...
If you REALLY want to use your function, do like this:
...
put changePoly("changeling","regular",8,68, red) into dummy_var
...


Best

Klaus

RalphORama
Posts: 7
Joined: Wed Oct 22, 2014 5:19 pm

Re: Use a function outside a handler to manipulate a graphic

Post by RalphORama » Thu Nov 13, 2014 4:02 am

Wow, I did not know about the command feature! Thank you for the help!

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

Re: Use a function outside a handler to manipulate a graphic

Post by Klaus » Thu Nov 13, 2014 1:02 pm

Hi Ralph,

well, "command xyz" is just like any "on xyz" handler like "on mouseup", so nothing special actually. :D
But the keyword "on" should be reserved for LC build-in handlers like "on mouseup" etc. so use "command" for your own handlers.

Here some great learning resources:
http://www.hyperactivesw.com/revscriptc ... ences.html


Best

Klaus

Post Reply