image on click handler
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
image on click handler
Dear group,
I have several images (loaded from files) on my card. I want to achieve the following behavior: when I click on one of the images, a certain function should be called. However, I lack the understanding necessary to add an "on click" handler to any of the images. How to go about that?
Thanks!
I have several images (loaded from files) on my card. I want to achieve the following behavior: when I click on one of the images, a certain function should be called. However, I lack the understanding necessary to add an "on click" handler to any of the images. How to go about that?
Thanks!
Dear ukimiku,
Please open the user manual that comes with Revolution as a pdf. You should be able to find it in the start centre.
In the manual, look up the concepts command, function, message and the commands mouseUp and mouseDown. I promise that you will be much happier after taking the time to read this. While you're at it, make sure that you also understand the message hierarchy.
Best regards,
Mark
Please open the user manual that comes with Revolution as a pdf. You should be able to find it in the start centre.
In the manual, look up the concepts command, function, message and the commands mouseUp and mouseDown. I promise that you will be much happier after taking the time to read this. While you're at it, make sure that you also understand the message hierarchy.
Best regards,
Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode
Dear ukimiku,
I agree with Mark that eventually you will have to dig into the manual. And the examples in the Resource Center under the Help Menu in Rev are well written and very instructive, they are worth every minute you spend with them.
But nothing beats the wonders of a working script.
In Rev just about anything can have a script, not only buttons, cards, and stacks. A field or a graphic or an image.
Just put a script into the image you imported like:You access the script of an image via the property inspector after selecting the image and then script. If you issue a put command without a destination then it is implicitly the message box where the output goes.
regards
Bernd
I agree with Mark that eventually you will have to dig into the manual. And the examples in the Resource Center under the Help Menu in Rev are well written and very instructive, they are worth every minute you spend with them.
But nothing beats the wonders of a working script.
In Rev just about anything can have a script, not only buttons, cards, and stacks. A field or a graphic or an image.
Just put a script into the image you imported like:
Code: Select all
on mouseUp
put "you clicked " & the long id of me
end mouseUp
regards
Bernd
image on click handler
Dear Mark & Bernd,
thanks for pointing me to that wealth of information. Funny: I had already programmed "on mouseUp" handlers for buttons but then, late at night, somehow thought that images deserved a special handler, called "on click". Well...
Anyhow, my program is running now. While the mouseUp handlers work, I am tired of copying and pasting the same handler into the handler of every single image. In RealBasic, one could define an array of controls (in this case, an array of images), then write one generic mouseUp handler for all the controls in the array (all the images). I suspect strongly that something equivalent must be possible in RunRev, too. I read about "behaviors" somewhere. Is that a viable option?
Thanks again.
Regards
thanks for pointing me to that wealth of information. Funny: I had already programmed "on mouseUp" handlers for buttons but then, late at night, somehow thought that images deserved a special handler, called "on click". Well...

Anyhow, my program is running now. While the mouseUp handlers work, I am tired of copying and pasting the same handler into the handler of every single image. In RealBasic, one could define an array of controls (in this case, an array of images), then write one generic mouseUp handler for all the controls in the array (all the images). I suspect strongly that something equivalent must be possible in RunRev, too. I read about "behaviors" somewhere. Is that a viable option?
Thanks again.
Regards
Hi ukimiku,
If you work with Rev 3.5 or later, you can use parentscripts (also called behaviors). Create a button with a script (the parent) and set the parentscript of another object (the child) to the long id of the parent object. This should give you the same result as in RB.
Best,
Mark
If you work with Rev 3.5 or later, you can use parentscripts (also called behaviors). Create a button with a script (the parent) and set the parentscript of another object (the child) to the long id of the parent object. This should give you the same result as in RB.
Best,
Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode
Dear ukimiku,
that is what Mark rather implicitly meant by studying the message hierarchy.
You can place a handler further down the message path, e.g. the card script, that does what you want. Then you can put into the image scripts a comand
in the card script you would put:
Now you reduced the amount of code considerably.
Or you could put into the card script:And no code into the images. Then whenever you click on an image on that card it will trigger this code in the card script. Even less code. If you put this code into the stack script then every image on every card would be handled by this command.
If you click on the card then this will put "you clicked on something other then an image" into the message box, the same would go for a graphic, that has no mouseUp handler.
Then you can use, as you mentioned, behaviors. Those are very powerful options and do exactly what you intend to do, but for an exercise I would try the other options before that. It shows more about the way messages travel in Rev. Who is the sender, who intercepts the messages, do I have to pass the message?
regards
Bernd
that is what Mark rather implicitly meant by studying the message hierarchy.
You can place a handler further down the message path, e.g. the card script, that does what you want. Then you can put into the image scripts a comand
Code: Select all
on mouseUp
DoWhatTheImageShouldDo
end mouseUp
Code: Select all
on DoWhatTheImageShouldDo
put "images should be visible"
end DoWhatTheImageShouldDo
Or you could put into the card script:
Code: Select all
on mouseUp
if the target contains "image" then
put "you clicked on an image"
else
put "you clicked on something other then an image"
end if
end mouseUp
If you click on the card then this will put "you clicked on something other then an image" into the message box, the same would go for a graphic, that has no mouseUp handler.
Then you can use, as you mentioned, behaviors. Those are very powerful options and do exactly what you intend to do, but for an exercise I would try the other options before that. It shows more about the way messages travel in Rev. Who is the sender, who intercepts the messages, do I have to pass the message?
regards
Bernd
Hi,
What Bernd describes is, of course, much easier than what I wrote about parentscripts. I was thinking about replicating RealBasic's way to do it, but putting a script higher in the message hierarchy is almost always sufficient and much easier to do.
Best,
Mark
What Bernd describes is, of course, much easier than what I wrote about parentscripts. I was thinking about replicating RealBasic's way to do it, but putting a script higher in the message hierarchy is almost always sufficient and much easier to do.
Best,
Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode
Thanks to the both of you!
I think I understand the message path approximately now. Since I don't want to distinguish many different kinds of user clicks, I didn't want to write my code into the "mouseUp" handler of the card, it would have become messy. So I wrote a line that assigned the parentscript property of some picture the long id of the example picture in the openCard event handler. This worked fine a coule of times, then I removed this openCard handler, saved the stack, closed it, reopened it - and the behavior was still there. It seems it got stored with the image. Some tries later, the behavior was completely gone, forgotten. I rewrote that openCard handler (it was only one line), but from then on, I have not been able to reproduce the former behavior.
So I reverted to Bernd's ideas, but strangely, now neither the openStack handler nor the openCard handler does anything - at all! No matter what I write, i.e. answer "11" - nothing happens in the openCard handler, neither in the runtime environment nor in the standalone application. Now, while I'm not despeate, I am still at a loss as to what might have happened. Have I corrupted the whole stack? I will try another one...
Thanks!
Regards
I think I understand the message path approximately now. Since I don't want to distinguish many different kinds of user clicks, I didn't want to write my code into the "mouseUp" handler of the card, it would have become messy. So I wrote a line that assigned the parentscript property of some picture the long id of the example picture in the openCard event handler. This worked fine a coule of times, then I removed this openCard handler, saved the stack, closed it, reopened it - and the behavior was still there. It seems it got stored with the image. Some tries later, the behavior was completely gone, forgotten. I rewrote that openCard handler (it was only one line), but from then on, I have not been able to reproduce the former behavior.
So I reverted to Bernd's ideas, but strangely, now neither the openStack handler nor the openCard handler does anything - at all! No matter what I write, i.e. answer "11" - nothing happens in the openCard handler, neither in the runtime environment nor in the standalone application. Now, while I'm not despeate, I am still at a loss as to what might have happened. Have I corrupted the whole stack? I will try another one...
Thanks!
Regards
Another update: the stack I was working on appears indeed corrupted. I deleted all handlers (making backup copies beforehand) and then reinserted them one by one, and the original behavior of the stack never showed again. In fact, the on cardOpen and on stackOpen event handlers won't do anything at all anymore. Can't file this as a bug report, though, as I can't remember the sequence of steps needed to arrive at this corrupted stack.
Anyhow, I opened a brand-new mainstack, inserted my back-up copies of the event handlers there, and now everything is fine. Strange.
Anyhow, I opened a brand-new mainstack, inserted my back-up copies of the event handlers there, and now everything is fine. Strange.
Hi ukimiku,
Change stackOpen and cardOpen into openStack and openCard and your "bug" should return. I am quite sure that your stack is not corrupt. Corrupt stacks cause completely different, much nastier problems.
If you have openCard and openStack (rather than the incorrect cardOpen and stackOpen) handlers at both card level and stack level, the handlers at stack level won't run. This might be an explanation of what you are observing.
Best regards,
Mark
Change stackOpen and cardOpen into openStack and openCard and your "bug" should return. I am quite sure that your stack is not corrupt. Corrupt stacks cause completely different, much nastier problems.
If you have openCard and openStack (rather than the incorrect cardOpen and stackOpen) handlers at both card level and stack level, the handlers at stack level won't run. This might be an explanation of what you are observing.
Best regards,
Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode