Page 1 of 3
lock objects from being dragged out of the screen
Posted: Thu May 11, 2017 7:36 pm
by tomerp
hi!
i have an app in which the user can move object around. what will be the most efficient way of preventing them from dragging the object out of the screen?
tnx
tomer
Re: lock objects from being dragged out of the screen
Posted: Thu May 11, 2017 9:36 pm
by dunbarx
Hi.
How are you moving the objects around?
In any case, the "mouseMove" message is sent whether in edit or run mode, so you could (pseudo):
Code: Select all
on mouseMove
put the target
if the mouseLoc is beyond the borders of this card then do not allow this to happen
end mouseMove
Silly, eh? But can you take it from there? To check either the mouseLoc in relation to the edges of the card, or, if you want to get fancy (and I always recommend fancifulness) check the edges of the object in relation to the edges of the card. The "target" is your friend here.
Write back with your fancies.
Craig Newman
Re: lock objects from being dragged out of the screen
Posted: Thu May 11, 2017 9:37 pm
by jmburnod
Hi,
You may define a rect and test if all points of your control is within this rect at mousemove
Best regards
Jean-Marc
Re: lock objects from being dragged out of the screen
Posted: Thu May 11, 2017 9:54 pm
by SparkOut
Learning to do this yourself is the best idea, but Animation Engine includes some very nice features including constraints to location.
Re: lock objects from being dragged out of the screen
Posted: Fri May 12, 2017 12:05 pm
by tomerp
i am moving the object with:
where should i put this code that u wrote? in the card script?
Code: Select all
on mouseMove
put the target
if the mouseLoc is beyond the borders of this card then do not allow this to happen
end mouseMove
Re: lock objects from being dragged out of the screen
Posted: Fri May 12, 2017 1:47 pm
by Randy Hengst
Try something like this in the script of the object you’re moving:
local tStartLoc
on mouseDown
put the loc of target into tStartLoc
grab me
end mouseDown
on mouseUp
if the loc of target is not within the rect of this card then
move target to tStartLoc
end if
end mouseUp
Re: lock objects from being dragged out of the screen
Posted: Fri May 12, 2017 4:28 pm
by Klaus
Hi Tomerp,
what Randy wrote!
Important hint:
.. so you could (pseudo):
PSEUDO in this case means, this is what it should do, but is really NOT the actual script!
Best
Klaus
Re: lock objects from being dragged out of the screen
Posted: Fri May 12, 2017 6:14 pm
by richmond62
I did it
DIFFERENTLY . . . .
Stack removed as updated, improved version uploaded.
Re: lock objects from being dragged out of the screen
Posted: Fri May 12, 2017 6:22 pm
by Klaus
You ALWAYS do it differently!

Re: lock objects from being dragged out of the screen
Posted: Fri May 12, 2017 7:25 pm
by richmond62
This version allows one to resize the stack . . . .
Re: lock objects from being dragged out of the screen
Posted: Fri May 12, 2017 7:32 pm
by Newbie4
...and if you want it to work for any size card, you can generalize the tests for right and bottom. (other than an 800x600 sized card in Richmond's example)
give the card a name (e.g. "main") and modify the code to check for
instead of just
.
and the
instead of 600.
Re: lock objects from being dragged out of the screen
Posted: Fri May 12, 2017 8:16 pm
by Newbie4
RATS,
I tried to get there with a more universal solution before you did (Richmond). But I went back to edit it and while I was editing it, you uploaded your new stack. So your time stamp was earlier than mine.
I like your idea of using variables for the right and bottom locations. It makes the code simpler reading.
Good job, nice example

Re: lock objects from being dragged out of the screen
Posted: Fri May 12, 2017 8:17 pm
by richmond62
It makes the code simpler reading.
That's because I really
don't understand complex code.
Re: lock objects from being dragged out of the screen
Posted: Fri May 12, 2017 8:38 pm
by Newbie4
Yes, I do too but sometimes when teaching newbies, you have to step through the process in logical steps (as you help them think thru it) Some people can not make the jump to the better code in one step.
Re: lock objects from being dragged out of the screen
Posted: Fri May 12, 2017 9:54 pm
by richmond62
To understand what happens when the stack is
resized, and so that
the TROLL doesn't "get lost" when it is resized, you need to look at
the
stackScript: