Page 1 of 1
Can´t change the location of script-created controls
Posted: Thu Feb 20, 2014 12:32 am
by FabricioRocha
Hi all, noob question here. LiveCode 6.5.2, testing in an Android 2.3.3 phone.
My experiment tries to create buttons where the user touched the screen. So I created an empty card with an "on touchEnd" handler script which first puts the mouseLoc into a variable "touchpos", then creates a button, sets the button´s label successfully, but is not able to set the location of "buttonX" to touchpos. The buttons always appear centered in the screen, one above the other, no matter where the touch happened.
I was able to verify that "touchpos" correctly receives the position coordinates. What might be happening? Can I put "the mouseLoc" into a single variable and use it as a new location? Or should I decompose the "x,y" in two different variables and use them separately? Is there any restriction on controls location after they are created, at least for Android?
Thanks!
Fabricio Rocha
Brasilia, Brasil
Re: Can´t change the location of script-created controls
Posted: Thu Feb 20, 2014 3:06 am
by Simon
Hi Fabricio,
I've just tried this in the simulator
Code: Select all
global gCount
on touchEnd
add 1 to gCount
put the mouseLoc into tMouse
answer tMouse
create button ("Click Me" & gCount)
set the loc of btn ("Click Me" & gCount) to tMouse
end touchEnd
And it's working for me, maybe you can see a difference in your code.
Simon
Edit: I put this in the card script and can see how this is pretty useless as you can't do anything else with the app. I guess you can make an opaque rectangle and set it's script to the above.
Re: Can´t change the location of script-created controls
Posted: Thu Feb 20, 2014 5:04 am
by FabricioRocha
Thank you, Simon.
I guess I found a difference:
Code: Select all
set the location of button button1 to touchpos
works, while
Code: Select all
set the location of button1 to touchpos
does not work.
But why exactly must the type of an object be so explicitely declared? In the LiveCode dictionary, I found, at the
name property description:
You can use an object's name to refer to the object in a statement.
And this seems just inconsistent.
Is there a way to retrieve an object's type from its name?
Thank you once again!
Fabricio
Re: Can´t change the location of script-created controls
Posted: Thu Feb 20, 2014 6:04 am
by Simon
Hi Fabricio,
But why exactly must the type of an object be so explicitely declared?
I'm sorry but that question is beyond me.
But it does all work the same
cd "myFirstCard"
img "myNicePicture"
btn "anotherButton"
grp "myInterestingGroup"
...
(note: you should start putting your names in quotes)
Is there a way to retrieve an object's type from its name?
I don't believe so.
You can do something like this:
Code: Select all
set the location of the last button to touchpos
Which would work in this case as the last button created is err... the last btn.
There is also "first" and numeric (based on the level the button is at).
Code: Select all
set the location of button 3 to touchpos
Simon
Re: Can´t change the location of script-created controls
Posted: Thu Feb 20, 2014 9:44 am
by LCNeil
Dear Fabricio,
Objects must be declared as this is the way LiveCode has always worked. An example of why object declaration can be beneficial is if you have multiple objects named the same e.g a Card called "1" and a button called "1"
If you did not declare your object , then LiveCode would not know what object you are referring too.
There are multiple ways to reference/declare objects-
- By name (Button "button1)
- By ID (Button Id 1002)
- By Number (Button 1)
- The last button (as simon suggested)
In regards to retrieving an object type via name, the "name" of an object will always return its type (e.g. button "button1"). There is also the "short name" which would return only "button1" and the "long name" which would return “button “button1” of card <card id here> of stack <stack name here>””
From this information, it should be easy enough to return the type of control. A more visual representation can be via the following script-
Code: Select all
on mouseUp
repeat with x = 1 to the number of controls of this card
put word 2 of the name of control x into tControlName
put word 1 of the name of control x into tType
put tControlname && "is a" && tType && "control" & cr after tControl
end repeat
answer tControl
end mouseUp
I hope this gives you some leads.
Kind Regards,
Neil Roger
--
RunRev Support Team ~
http://www.runrev.com
-
Re: Can´t change the location of script-created controls
Posted: Thu Feb 20, 2014 3:00 pm
by FabricioRocha
Thank you, Simon and Neil.
Actually I found a way to get what I wanted, in the LiveCode Users Guide, and Neil's example uses it. Using the generic type control it is possible to refer to a control by its name without a reference to its specific type.
Thanks!
Fabricio
Re: Can´t change the location of script-created controls
Posted: Thu Feb 20, 2014 3:51 pm
by Klaus
Hola Fabricio,
1. ALWAYS put QUOTES around object NAMES!
Sorry for the capitals, but this will prevent you from future inconveniences!
2.
FabricioRocha wrote:I guess I found a difference:
Code: Select all
set the location of button button1 to touchpos
works, while
Code: Select all
set the location of button1 to touchpos
does not work.
"button1" is just a NAME! Could also be "jesusmaria"!
So only for you it is clear that this is really a BUTTON, the engine does not know this!
So you need to add the TYPE to the name or use CONTROL instead.
But if you have 2 CONTROLS pf different TYPE with the same name, using -> CONTROL "name of control"
might also not work sufficiently.
Best
Klaus