Passing the name of an image object to a function
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
Passing the name of an image object to a function
I have a button that On Mousedown is supposed to start an action on an image. However, the target image changes so the function is part of the card. Syntax like myFunction(image "myImage") isn't working. How do I accomplish my goal. One thing to note - I'm not trying to return any values, so is there an alternative to using a function?
Steve
Steve
Re: Passing the name of an image object to a function
You can use "the target" to reference the origin of the message, in its various flavours, such as "the short name of the target" or "the long id of the target".
You could also put the handler script into an invisible button (say) and use the long id of that to reference the "behavior" of each image. When you use behaviors (sic... can we have a synonym that is spelt correctly please?
) each object that uses that behavior script has its context virtualised so that "me" refers to the target object in question.
And look up in the documentation about the distinction between a "command" and a "function".
Hope that helps
SparkOut
You could also put the handler script into an invisible button (say) and use the long id of that to reference the "behavior" of each image. When you use behaviors (sic... can we have a synonym that is spelt correctly please?

And look up in the documentation about the distinction between a "command" and a "function".
Hope that helps
SparkOut
Re: Passing the name of an image object to a function
Thanks SparkOut. I had looked at the Dictionary for command and that didn't help. Check the documentation? Me?
Now functions and commands are making more sense.
So far it seems okay, but I suspect the put commands are not what you meant by "...use "the target" to reference the origin of the message...". It called ResetToFirst, but didn't get very far. Here's the code:
This was the error: execution error at line 2 (Chunk: error in object expression) near "‰PNG", char 53
Steve

By that I thought you meant put it in a variable, but that didn't work completely. Here's the code from the button:SparkOut wrote:You can use "the target" to reference the origin of the message, in its various flavours, such as "the short name of the target" or "the long id of the target".
Code: Select all
on mouseDown
put image "outer" into obStart
put image "outer2" into obEnd
ResetToFirst obStart, obEnd
end mouseDown
Code: Select all
command ResetToFirst obStart, obEnd
set the height of obStart to the formattedHeight of obStart
set the width of obStart to the formattedWidth of obStart
set the height of obEnd to the formattedHeight of obEnd
set the width of obEnd to the formattedWidth of obEnd
end ResetToFirst
Steve
Re: Passing the name of an image object to a function
You need something more likeYour resetToFirst command looks alright - it just needs to be given strings that identify the object in question to the engine, which the revisions above should hopefully do.
I'm not exactly clear what you're doing, but you may find a behavior a suitable alternative. It would certainly be easier to differentiate between the objects that are supposed to have a handler respond to the mouseDown and those that aren't.
Code: Select all
on mouseDown
-- get a reference to the target object, which will be the object which generated the message caught by this handler
-- so if this handler is in the card script the target could be any object on the card...
-- which means that you should also do some validation at the very least, if you must use "mouseDown" in a card script
put the long name of the target into obStart
-- check the target is actually an image
if word 1 of obStart is "image" then
-- depending on whether this is fixed or dependent on the target we need to make a reference
-- to the ending object
-- assuming we append "2" to the name of the start object we can copy it and ...
put obStart into obEnd
-- then stick the digit "2" in the right place
put "2" before char -1 of word 2 of obEnd
-- obviously you will have to have your own method for determining the names of the objects in question
resetToFirst obStart, obEnd
else
pass mouseDown
end if
end mouseDown
I'm not exactly clear what you're doing, but you may find a behavior a suitable alternative. It would certainly be easier to differentiate between the objects that are supposed to have a handler respond to the mouseDown and those that aren't.
Re: Passing the name of an image object to a function
Thanks again SparkOut.
To give context, this is just some test code so I can understand how to do a few things in my first project. That project resizes and changes the appearance of several images at different times. After finishing, it returns the images to their original size, leaving the other changes in place. In the real project, the command will be called in code, but for these tests I'm using buttons to start the changes and then revert.
I've successfully created the code for the image manipulations as it applies to a specified image, such as: image "myimage". However, I need to recycle the code for all of the images, so I need to put image "myimage" in a variable.
I'm not sure what you mean by the rest of it, but it may be worth pointing out that I didn't really need to image "outer2" and obEnd in this example. I violated the KISS principle! Outer and outer2 are different images and the use of "2' holds no significance. I'm not trying to put obStart into obEnd and copying doesn't seem to apply.
Steve
To give context, this is just some test code so I can understand how to do a few things in my first project. That project resizes and changes the appearance of several images at different times. After finishing, it returns the images to their original size, leaving the other changes in place. In the real project, the command will be called in code, but for these tests I'm using buttons to start the changes and then revert.
I've successfully created the code for the image manipulations as it applies to a specified image, such as: image "myimage". However, I need to recycle the code for all of the images, so I need to put image "myimage" in a variable.
I suspect this is where I'm going wrong. What do you mean? Perhaps you could provide an example?SparkOut wrote:get a reference to the target object
That's what I thought I was doing with this code: put image "outer" into obStartSparkOut wrote:put the long name of the target into obStart
I'm not sure how to check, but it is an imported png. I suspect that's why part of the error message is "...near "‰PNG"...". Of course, checking in the real script is probably a good idea.SparkOut wrote:check the target is actually an image
I'm not sure what you mean by the rest of it, but it may be worth pointing out that I didn't really need to image "outer2" and obEnd in this example. I violated the KISS principle! Outer and outer2 are different images and the use of "2' holds no significance. I'm not trying to put obStart into obEnd and copying doesn't seem to apply.
Steve
Re: Passing the name of an image object to a function
Hi Steve,
you need to supply the "long name" or the "long ID" to your function/handler!
This should work:
Best
Klaus
you need to supply the "long name" or the "long ID" to your function/handler!
This should work:
Code: Select all
on mouseDown
put the long ID of image "outer" into obStart
put the long ID of image "outer2" into obEnd
## or "the long name of image "xxx"
ResetToFirst obStart, obEnd
end mouseDown
Best
Klaus
Re: Passing the name of an image object to a function
Thanks very much Klaus. That did the trick.
I guess I better study up on long names and long IDs.
Steve

Steve
Re: Passing the name of an image object to a function
I thought that was an examplesteve10 wrote:I suspect this is where I'm going wrong. What do you mean? Perhaps you could provide an example?SparkOut wrote:get a reference to the target object

The lines you are quoting are comments in the handler that precede the actual codeline, to describe what we were doing. So the "get a reference to the target object" comment referred to the code line "put the long name of the target into obStart", which is actual syntax.
"put the long name of the target into obStart" was the actual syntax to do this. I was under the impression that you had a number of different images, each with their own name and id, and one group or card script with the mouseDown handler that would be caught and is intended to deal with the different images the same way, but with a reference to whichever on actually had the mouseDown action initiated. So say it was image "outer" that had the mouseDown event happen, and not the image "canoe" or image "balloon" that cold equally have been handled. When the mouseDown message reaches the card script handler we can find which of the images it was which originated the message by use of the keyword "target". So we were able to "put the long name of the target into obStart" which means that in this case the long name of image "outer" of (the current card...) was stored.steve10 wrote:That's what I thought I was doing with this code: put image "outer" into obStartSparkOut wrote:put the long name of the target into obStart
"-- check the target is actually an image" was a comment to describe this codeline: if word 1 of obStart is "image" then...steve10 wrote:I'm not sure how to check, but it is an imported png. I suspect that's why part of the error message is "...near "‰PNG"...". Of course, checking in the real script is probably a good idea.SparkOut wrote:check the target is actually an image
Having retrieved the long name of the target, obStart will look something like:
image "outer" of card 1002 of stack "path and name of/myTestStack.livecode"
so if the first word (word 1) of that long name (held in obStart) is "image" then we know it is an image that generated the message. I put that test in because I didn't think you would want the mouseDown handler to trigger off the resetToFirst handler for any field or other object on the card, or the card itself.
Ah, well the rest of it isn't likely to be too relevant (either). I was trying to help you achieve being able to find a way programmatically to reference which image was the source of a message. I believe I misunderstood you and you were simply trying to get the "reference" to the object (image "outer") to be passed to the resetToFirst handler. Which, as you have now seen from Klaus' post, is quite simple to do once you have been shown the syntax you need.steve10 wrote:I'm not sure what you mean by the rest of it, but it may be worth pointing out that I didn't really need to image "outer2" and obEnd in this example. I violated the KISS principle! Outer and outer2 are different images and the use of "2' holds no significance. I'm not trying to put obStart into obEnd and copying doesn't seem to apply.
Steve