Making an image larger then smaller with code

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
gyroscope
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 404
Joined: Tue Jan 08, 2008 3:44 pm
Contact:

Making an image larger then smaller with code

Post by gyroscope » Fri May 23, 2008 4:17 pm

Hi, I've taken time out today from my main first project to amend a neat bit of code I found on the excellent RevNet site. The idea was that I'd "put this one under my belt" for a future project but after three or so hours of trying to get it to work, I've conceded defeat!!

The future project would be a Seek & Find type game, so that if a user clicks on a correctly found item, the image would become larger, smaller, larger and finally smaller again before disappearing. (Most of this type of games seem to have this added visual interest, as I'm sure you know...)

OK, here's the code I'm stuck on:

Code: Select all

global tRun, tWidth, tHeight

on mouseUp pMouseBtnNo

--- cos of wrong coding, keeps wandering off!!!-------------
set the location of image "TestPic.png" to 217, 92
------------------------------------------------------------
  put the formattedwidth of image "TestPic.png" into tWidth
  put the formattedheight of image "TestPic.png" into tHeight
  put 56 into tRun

DoSmall
wait 10 ticks
--DoLarge
--wait 10 ticks
--DoSmall
--wait 10 ticks
--DoLarge
--set the visible of image "TestPic.png" to false
-- etc
end mouseUp

on DoSmall
   if tRun > 30 then
 --  repeat with tRun =56 to 31
        put tWidth - 1 into tWidth
        put tHeight - 1 into tHeight
        put tRun - 1 into tRun
        
        set the height of image "TestPic.png" to round(tHeight * (tRun/tWidth))
        set the width of image "TestPic.png" to round(tWidth * (tRun/tHeight))	
 --  end repeat
          send "DoSmall" to me in 10 milliseconds
end if  

--- test to see what tRun is: strange, thought it should be 31...... -----------
answer tRun
end DoSmall

on DoLarge
    if tRun < 70  then
        lock screen
put tWidth + 1 into tWidth
put tHeight + 1 into tHeight
put tRun + 1 into tRun

    set the height of image "TestPic.png" to round(tHeight * (tRun/tWidth))
 set the width of image "TestPic.png" to round(tWidth * (tRun/tHeight))
    send "DoLarge" to me in 20 milliseconds
     unlock screen
    end if     
end DoLarge  
As it stands, DoSmall works fine, so does DoLarge on its own, but running both just gets the image floating off to nirvana!

I put in answer tRun and the surprising answer was just 1 less than the original value; I thought that by sending "DoSmall to me in 10 mill" was another type of loop, whereby the value of tRun would decrease by 1 every time. Obviously not! I guess this is why a repeat loop is no good, and why, even though DoSmall hasn't finished, DoLarge gets in on the act. (Or repeat loops and "send to me's" don't mix?)

I've tried lots of things to get them to run properly i.e finish DoSmall first before running DoLarge, but to no avail.

Although this isn't a pressing problem, i'd be most grateful for any explanations of my semi-questions in second para up, and some sort of solution please!

:)

BvG
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1239
Joined: Sat Apr 08, 2006 1:10 pm
Contact:

Post by BvG » Fri May 23, 2008 6:28 pm

1. You are rounding, so there's no reason to act surprised about changing size.

2. send <message> to me in <timeperiod> is nonblocking. it'll run independent of other stuff. Repeat for each is blocking, so it will stop everything from executing (even "send to me in time" stuff). What you see is most likely a combination of these behaviours.

Make all loops blocking (send to me (no time) would be blocking), and don't expect rounded math to be precise.
Various teststacks and stuff:
http://bjoernke.com

Chat with other RunRev developers:
chat.freenode.net:6666 #livecode

gyroscope
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 404
Joined: Tue Jan 08, 2008 3:44 pm
Contact:

Post by gyroscope » Fri May 23, 2008 8:52 pm

Ah, I see now, it all makes sense with your explanation as to what's going on; I couldn't see the wood for the trees there!

I'll give repeat loops another go perhaps, and leave "send to me" stuff alone for this particular handler.

I do understand that using round is not precise as such but was still confused as to why the variable in the round algorithm wasn't changing; this must have to do with the fact that "send...to me" runs independantly of other stuff, and not acting like a type of repeat loop, as you informed me.

Thank you very much, BvG.

:)

gyroscope
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 404
Joined: Tue Jan 08, 2008 3:44 pm
Contact:

Post by gyroscope » Fri May 23, 2008 11:44 pm

Hi, just for the record, I tried a repeat loop again and got it to work nicely (I had put SuchandSuch = 50 to 30 instead of down to, silly me). Anyhow, making the image smaller works fine, and making it larger again also works, but the final image is out of proportion.

For interest, here's the first repeat loop:

Code: Select all

   repeat with RumB = 56 down to 40
        put tWidth - 1 into tWidth
        put tHeight - 1 into tHeight
         
        set the height of image "TestPic.png" to round(tHeight * (RumB/tWidth))
        set the width of image "TestPic.png" to round(tWidth * (RumB/tHeight))	
        wait 10 milliseconds
    end repeat
I've discovered that in this particular instance, using round or not doesn't makes any difference. Anyhow, there must be a slightly different algorythm/equation, whatever it's called (i.e round(tHeight * (RumB/tWidth)) ) for enlarging in proportion. (This is with tWidth and tHeight each with + 1 and RumB 40 to 56) I'll have to play around with that another time.

So a little bit less fog in my personal learning curve...

:)

gyroscope
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 404
Joined: Tue Jan 08, 2008 3:44 pm
Contact:

Post by gyroscope » Sat May 24, 2008 12:24 pm

Oh dear, I'm feeling mightily daft here:

The So Software script that I took a bit from to amend, works brilliantly for reducing a portrait or landscape shaped pic so as to keep its proportion into a square image box but after sending myself on a wild goose chase, I've realised it doen't work for reducing and enlarging a rectangular image all that well.

A straightforward solution suddenly struck me and I'm kicking myself that I didn't think of it sooner (it would have saved hours :x):

Simply put each image in a square image box and provided its png with a transparent background, only the image reacts to a mouseUp message. Then, in a repeat loop, take 1 off the width and 1 of the height of the box each time to reduce, or + 1 to enlarge. D'oh!!! :wink:

Hmm, in a way, I wish I hadn't started this post... :oops:

Post Reply