Click on object, attach to mouse exclusively ?

0 favourites
  • 11 posts
From the Asset Store
Total customisation of the input! You can combine inputs from all peripherals. Make your game accessible for everyone!
  • 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...)

  • Hello

    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.

  • 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).

  • 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...

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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...

    You need to remove the "Cursor is over sprite" condition to fix that.

  • 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

  • basically you want a drag&drop I guess

    drag&drop.capx

  • 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" ?

  • 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

  • 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...

  • yeah offsetting is just hmmm more drag&drop-ish I think

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)