Page 1 of 1
Clone object in runtime and move it to chosen location?
Posted: Wed Jun 17, 2015 9:46 am
by sinep
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
Re: Clone object in runtime and move it to chosen location?
Posted: Wed Jun 17, 2015 3:11 pm
by dunbarx
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
Re: Clone object in runtime and move it to chosen location?
Posted: Thu Jun 18, 2015 11:56 pm
by sinep
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.
Re: Clone object in runtime and move it to chosen location?
Posted: Fri Jun 19, 2015 3:05 am
by sinep
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
Re: Clone object in runtime and move it to chosen location?
Posted: Fri Jun 19, 2015 3:35 am
by dunbarx
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
Re: Clone object in runtime and move it to chosen location?
Posted: Fri Jun 19, 2015 4:19 am
by SparkOut
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
The "with messages" gives the engine a chance to deal with other things, including screen updates.
Re: Clone object in runtime and move it to chosen location?
Posted: Fri Jun 19, 2015 6:47 am
by sinep
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.
Re: Clone object in runtime and move it to chosen location?
Posted: Fri Jun 19, 2015 9:17 am
by SparkOut
Oh, a possible improvement to the jerkiness might be to set the layerMode of the button to dynamic