Page 1 of 1
Graphic Scaling Example
Posted: Sun Oct 17, 2010 7:41 pm
by Zephitron
In the User Guide, on page 395, an example is given of scaling a graphic thus:
local lCount
on mouseUp
put 0 into scaleGraphic
end mouseUp
on scaleGraphic
add 1 to lCount
if lCount > 100 then exit scaleGraphic
get the rect of graphic "rectangle"
add 1 to item 1 of it
add 1 to item 2 of it
subtract 1 from item 3 of it
subtract 1 from item 4 of it
set the rect of graphic "rectangle" to it
send "scaleGraphic" to me in 10 milliseconds
end scaleGraphic
Why does it say "put 0 into scaleGraphic" since it's not calling a function and that 0 doesn't go anywhere? (I'm assuming this script is in the button on the card with the graphic). Was this meant to be a counter resetter?
What's are some good ways to reset the lCount counter (in this sort of situation where one is calling a loop made from a handler calling itself)?
Re: Graphic Scaling Example
Posted: Sun Oct 17, 2010 8:45 pm
by bn
Hi Zephitron,
Code: Select all
local lCount
on mouseUp
put 0 into scaleGraphic
end mouseUp
Code: Select all
on scaleGraphic
add 1 to lCount
if lCount > 100 then exit scaleGraphic
if you look at code the script local varible LCount is set to 0 because the "on scaleGraphic" starts with add 1 to LCount. And beside the reason of the specific way to program this it has the advantage that Rev "knows" that the script locar varible LCount is a number and takes that into account. Meaning behind the scene Rev treats it as a numerical variable instead of a litteral variable. That used to be more important when CPUs were slower, and I just assume that it is still the case as it used to be (did not benchmark it). Anyway the whole point of Rev is that you don't have to care about what kind of data you put into a variable, Rev takes care of that. Of course that slows it down a bit.
What's are some good ways to reset the lCount counter (in this sort of situation where one is calling a loop made from a handler calling itself)?
Code: Select all
on scaleGraphic
add 1 to lCount
-- if lCount > 100 then exit scaleGraphic -- blocked
get the rect of graphic "rectangle"
add 1 to item 1 of it
add 1 to item 2 of it
subtract 1 from item 3 of it
subtract 1 from item 4 of it
set the rect of graphic "rectangle" to it
put lCount mod 101 into lCound -- 101 mod 101 is 0, so the next round will add 1 and start with 1
send "scaleGraphic" to me in 10 milliseconds
end scaleGraphic
please have a look at the comments in the script. This runs forever and you would have to provide a way out, but you can to that by setting a flag like you did in the moveClouds example: a script local varible that is set to true until you set it to false. Mod is very convenient in those circumstances.
regards
Bernd
Re: Graphic Scaling Example
Posted: Mon Oct 18, 2010 12:50 am
by Zephitron
Hi Bernd,
I'm still a tad flummoxed:
If you block (comment out) "if lCount > 100 then exit scaleGraphic " and use an external control to stop the loop, like this (all the "put" statements are so I can see the values in a little "debug" field (if you know a better way of watching variables, let me know)):
on StopIt
put true into StopThis
scaleDownGraphic
end StopIt
on DoScaleDown
put false into StopThis
scaleDownGraphic
end DoScaleDown
on scaleDownGraphic
if StopThis is false then
add 1 to lCount
put " lCount:" && lCount into line 1 of field "debug"
-- if lCount > 100 then exit scaleDownGraphic
get the rect of graphic "rectangle" // rect property is: left,top,right,bottom
put "rect: " && rect of graphic "rectangle" into line 2 of field "debug"
add 1 to item 1 of it // left
put "left: " && item 1 of it into line 3 of field "debug"
add 1 to item 2 of it // top
put "top: " && item 2 of it into line 4 of field "debug"
subtract 1 from item 3 of it // right
put "right: " && item 3 of it into line 5 of field "debug"
subtract 1 from item 4 of it // bottom
put "bottom: " && item 4 of it into line 6 of field "debug"
put lCount mod 101 into lCount
put "lCount: " && lCount into line 7 of field "debug"
set the rect of graphic "rectangle" to it
send "scaleDownGraphic" to me in 100 milliseconds
end if
end scaleDownGraphic
That does indeed work fine as far as starting and stopping it, but then there's no need or point to the loop counter anymore. The idea of the loop counter is that it runs for a while and then stops itself. But this now is externally controlled.
If you unblock "if lCount > 100 then exit scaleGraphic", lCount never gets to 101, so the mod statement will never make lCount 0.
So... what I would like would be a way to say
if lCount > 100 then set lCount to 0 and exit scaleDownGraphic
so that the next time the loop is called it lCount is set back to 0.
Come to think of it, it might be more useful to be able to tell it how far to shrink, and at what rate.
as far as the reason for the zero, I understand what you are saying about telling Rev that it's a number. My puzzlement is about the statement (in a button I suppose) of
put 0 into scaleGraphic
is supposed to put 0 into lCount? Because it doesn't. That's why I was wondering if this was a typo, or if this should be a function that sends a value.
Thanks again.
Re: Graphic Scaling Example
Posted: Mon Oct 18, 2010 1:39 pm
by bn
Hi Zephitron,
sorry, I was not attentive to the obvious typo in the user guide page 395 of the user guide for Rev 4.0, Livecode 4.5 has the same example in the user guide on page 375. That makes no sense at all
local lCount
on mouseUp put 0 into scaleGraphic
end mouseUp
on scaleGraphic
add 1 to lCount
...
it actually creates a local variable scaleGraphic within the mouseUp handler. That variable is never used and can do no harm. The scope of the local variable is the mouseUp handler and it does not interfere with the command name.
I reported this as a bug in the documentation:
http://quality.runrev.com/qacenter/show_bug.cgi?id=9106
I did not notice it because I have a built-in auto-correction feature...
As to your question regarding mod: if you only want to it from 1 to 100 mod is not necessary. You can test for it and exit if you want. Before exiting you could set lCount to something that the scaleDown part uses. Mod is good if you want something to continously go over a range of numbers.
If you dont get this to work, again, a little stack with an explanation of what it is supposed to do and not doing is helpful. Uploading zipped files works for me again, so you might want to go that route.
regards
Bernd