Click on object, attach to mouse exclusively ?

Get help using Construct 2

Post » Mon Oct 31, 2011 6:48 pm

There's a behavior I find myself always reimplementing : pressing on an object (or touching it), attaching it to the mouse cursor or the finger, and releasing it when I let go the mouse button or the finger.
Everytime I seems to implement it a different way, never too happy with the results and the slight variations.
One of the biggest problem I encounter is the right way to make it "exclusive" (I don't want to attach every instance of the same object when I pass on it when I'm already attached to one).
Does that kind of thing merits its own behavior ?
Is there a quick and efficient way to do that every time ? (something that doesn't break on border cases...)
B
33
S
9
G
6
Posts: 709
Reputation: 6,704

Post » Mon Oct 31, 2011 7:32 pm

Hello @Pode.

You can create a personal instance for the object and change it when the mouse grab one of them, change it boolean, true or false. So, when you grab one, make the grabbing code to ignore all the others checking if the boolean is true.

That's it.
ImageImageImageImageImageImage
B
93
S
19
G
12
Posts: 1,192
Reputation: 18,301

Post » Mon Oct 31, 2011 8:02 pm

To TELLES0808 (& others) : I give here the link of an example I made to illustrate my way of doing it. I don't know if it's the "right" way of doing it (or even an efficient one !).

Exclusive mouse click attach : http://dl.dropbox.com/u/1412774/exclusiveClickTest.capx (should work by using touch instead of mouse also).Pode2011-11-02 09:47:15
B
33
S
9
G
6
Posts: 709
Reputation: 6,704

Post » Mon Oct 31, 2011 8:04 pm

By the way, a problem can arise sometimes when your browser is a bit slow : the mouse cursor can go outside the desired target between two ticks, so the target doesn't fllow the mouse anymore...
B
33
S
9
G
6
Posts: 709
Reputation: 6,704

Post » Tue Nov 01, 2011 12:38 pm

[QUOTE=Pode] By the way, a problem can arise sometimes when your browser is a bit slow : the mouse cursor can go outside the desired target between two ticks, so the target doesn't fllow the mouse anymore...[/QUOTE]

You need to remove the "Cursor is over sprite" condition to fix that.
B
7
S
2
G
4
Posts: 164
Reputation: 2,418

Post » Wed Nov 02, 2011 9:49 am

Aw. Logical, but I don't know why I didn't set that...
Anyway, here's the new version : http://dl.dropbox.com/u/1412774/exclusiveClickTest2.capx
B
33
S
9
G
6
Posts: 709
Reputation: 6,704

Post » Wed Nov 02, 2011 10:41 am

basically you want a drag&drop I guess

drag&drop.capx
Last edited by Yann on Sun Sep 14, 2014 11:02 pm, edited 1 time in total.
B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

Post » Wed Nov 02, 2011 12:23 pm

@Yann: I really like your implementation ?
I have a few questions about it, though. When you click on a Sprite, you are picking a random instance. Why ? Doesn't break the idea of finding the Sprite underneath the mouse cursor ?

You also are using an offset for x and y when referring to "Self". In that context, which object is adressed by "Self" ?
B
33
S
9
G
6
Posts: 709
Reputation: 6,704

Post » Wed Nov 02, 2011 12:41 pm

random is just something I came up with to avoid moving two sprite at the same time when you have two overlapping sprites.
It should be "pick top most" (in construct classic) but it's not yet implemented in construct 2 so pick random seems to be the only choice
If you are just over one sprite pick random has no choice but to pick this only sprite
(the first condition do the main picking, the second is just to avoid to pick multiple sprite)

Self is just the object you are working on
Sprite set X to self.X
is the same as
Sprite set X to Sprite.X

I often use self 'cause it's easier to copyPaste code between different objects and self is only 4 letters to type :D
B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

Post » Wed Nov 02, 2011 3:23 pm

The random stuff is clever, then !
Thinking about that, removing the x/y offsetting and putting the sprite directly on the mouse position also works...
B
33
S
9
G
6
Posts: 709
Reputation: 6,704

Next

Return to How do I....?

Who is online

Users browsing this forum: Bad Wolf, davides, NRABrazil, Vallar, Walessio, Yahoo [Bot] and 27 guests