Clone object in runtime and move it to chosen location?

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
sinep
Posts: 12
Joined: Sat Jun 13, 2015 5:19 am

Clone object in runtime and move it to chosen location?

Post by sinep » Wed Jun 17, 2015 9:46 am

I am trying to make it so that a user can select an image and drag a copy of it to a chosen location without the original image/box moving.

For example, say it was a forest building game. I have a side bar with several types of trees. I want to be able to allow the user to select a tree and drag a copy of it to where they want.

I have the ability to clone an object and grab the original and move it around. However, with just "clone button" and "grab it" the clone appears to the bottom right of the original and the original is grabbed. I want to grab the clone and keep the original in its original location.

So far I have:

on mouseDown

clone button "b1"
set the name of it to "b2"
set the loc of it to the loc of button "b1"


grab it
end mouseDown

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10331
Joined: Wed May 06, 2009 2:28 pm

Re: Clone object in runtime and move it to chosen location?

Post by dunbarx » Wed Jun 17, 2015 3:11 pm

Hi.

So much fun. In the script of btn "b1":

Code: Select all

on mouseDown
lock screen
clone button "b1"
set the name of last btn  to "b2"
set the loc of last btn to the loc of btn "b1"
end mouseDown
Now in the card script:

Code: Select all

on mousemove
   if  "card" is not in the target and the mouse is down then set the loc of last btn to the mouseLoc
end mousemove
This is free. But you must still pay for it:

1- Why is the mouseMove handler in the card script?
2- Why lock the screen?
3- Figure a way, since you are using a very nice naming schema (B1, B2, etc) to modify this so that each new cloned button takes a unique and correct name. That is, if you did this four times, you would not end up with four "B2's" but rather, "B2", "B3", "B4" and "B5".

Get going.

Craig Newman

sinep
Posts: 12
Joined: Sat Jun 13, 2015 5:19 am

Re: Clone object in runtime and move it to chosen location?

Post by sinep » Thu Jun 18, 2015 11:56 pm

If I use the code below I am able to clone my original button (as desired) and move it to where I want (as desired). However, on subsequent clicks, I am only able to clone the newly placed button “b2” and move that clone. I want to keep cloning the original each time it’s clicked and not allow the clones to be cloned, if that makes sense:
on mouseDown
lock screen
clone button "b1"
set the name of last btn to "b2"
set the loc of last btn to the loc of btn "b1"

grab btn "b2"
repeat until the mouse is up
set the loc of last btn to the mouseLoc
end repeat
grab me

end mouseDown

just to add, when I am able to move the clone around it is slightly laggy and not as smooth as it ought to be ...should I have a "wait ___ milliseconds?"

I feel as though I’m getting close and learning other things as I practice this. As you can see I am completely new and still figuring a lot out. Thank you so much for your assistance and patience! I appreciate it very much.

sinep
Posts: 12
Joined: Sat Jun 13, 2015 5:19 am

Re: Clone object in runtime and move it to chosen location?

Post by sinep » Fri Jun 19, 2015 3:05 am

Okay, another update.

I just added "unlock screen" as well as the "wait" which I'm not actually sure if it does anything.

Now with the following code the only problem is that each of the cloned buttons will also clone. Is there a way to stop clone so that only "b1" a.k.a. the original button gets cloned and the others (the clones of b1) are just moveable?

on mouseDown
lock screen
clone button "b1"
set the name of last btn to "b2"
set the loc of last btn to the loc of btn "b1"
unlock screen

grab btn "b2"

repeat until the mouse is up
set the loc of last btn to the mouseLoc
wait 100 milliseconds
end repeat
grab me

end mouseDown

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10331
Joined: Wed May 06, 2009 2:28 pm

Re: Clone object in runtime and move it to chosen location?

Post by dunbarx » Fri Jun 19, 2015 3:35 am

Hi.

The clones will, er, clone, because they contain the same script as the parent.

Add a line that sets the script of the last button to empty.

Craig

SparkOut
Posts: 2947
Joined: Sun Sep 23, 2007 4:58 pm

Re: Clone object in runtime and move it to chosen location?

Post by SparkOut » Fri Jun 19, 2015 4:19 am

And "pay" with the answers to Craig's original homework questions in his first post in this thread. Question 3 will help you see what is behind one of your current problems.

For solving lagginess / jerkiness of the dragging, don't introduce an unnecessary delay, with the wait 100 milliseconds. But do introduce a "zero duration moment" of interrupt within the loop

Code: Select all

wait 0 milliseconds with messages
The "with messages" gives the engine a chance to deal with other things, including screen updates.

sinep
Posts: 12
Joined: Sat Jun 13, 2015 5:19 am

Re: Clone object in runtime and move it to chosen location?

Post by sinep » Fri Jun 19, 2015 6:47 am

dunbarx wrote:Hi.

The clones will, er, clone, because they contain the same script as the parent.

Add a line that sets the script of the last button to empty.

Craig
Amazing!! This was exactly it. Thanks so much to you and the others for helping.

SparkOut
Posts: 2947
Joined: Sun Sep 23, 2007 4:58 pm

Re: Clone object in runtime and move it to chosen location?

Post by SparkOut » Fri Jun 19, 2015 9:17 am

Oh, a possible improvement to the jerkiness might be to set the layerMode of the button to dynamic

Post Reply