GameSalad

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

How to make the actor not jump to mouse position?

AtlantenAtlanten Posts: 56Member, PRO
I have an actor that can be moved around, you click on it and drag it to another position. So the actor's position gets constrained to the mouse position when you click on it.

When you click on the actor somewhere off-center, it jumps to position itself centered on the mouse click point, and it doesn't look so good...

How can I write an expression to make the actor keep its relative position to the mouse click, but still follow it around?

Thanks

Comments

  • CodeMonkeyCodeMonkey Posts: 1,803Head Chef, Member, PRO
    Store the position of the actor when it is clicked relative to the mouse.

    Real Attributes: self.OffsetX and self.OffsetY
    Boolean Attribute: self.grabbed.

    Rule: when touch(pressed) or mouse down&mouse position over.
    --Change Attribute: self.OffsetX = game.mouse.Position.X - self.Position.X
    --Change Attribute: self.OffsetY = game.mouse.Position.Y - self.Position.Y
    --Change Attribute: self.grabbed = true

    Rule: When Attribute: self.grabbed=true
    --Constrain Attribute: self.Position.X = game.mouse.Position.X - self.OffsetX
    --Constrain Attribute: self.Position.Y = game.mouse.Position.Y - self.OffsetY

    I think that should work
  • CodeMonkeyCodeMonkey Posts: 1,803Head Chef, Member, PRO
    Store the position of the actor when it is clicked relative to the mouse.

    Real Attributes: self.OffsetX and self.OffsetY
    Boolean Attribute: self.grabbed.

    Rule: when touch(pressed) or mouse down&mouse position over.
    --Change Attribute: self.OffsetX = game.mouse.Position.X - self.Position.X
    --Change Attribute: self.OffsetY = game.mouse.Position.Y - self.Position.Y
    --Change Attribute: self.grabbed = true

    Rule: When Attribute: self.grabbed=true
    --Constrain Attribute: self.Position.X = game.mouse.Position.X - self.OffsetX
    --Constrain Attribute: self.Position.Y = game.mouse.Position.Y - self.OffsetY

    I think that should work
  • AtlantenAtlanten Posts: 56Member, PRO
    Ooh, that works perfectly

    Thank you so much
  • HunnenkoenigHunnenkoenig Posts: 1,173Member
    That is awsame! Thanks!

    I was thinking too complicated and I wanted to count magnitude and subtract that value from the positions :-)

    Since I have learned magnitude, I seem to be attached to it somehow :-P

    This is exactly what I was looking for. Thanks!
  • synthesissynthesis Posts: 1,693Member
    Thanks for bumping this Hunn. Good stuff here!
  • StormyStudioStormyStudio United KingdomPosts: 3,987Member
    thats amazing....I was willing to ignore this annoying feature of my game, but it can be fixed....look forward to implementing that tonight...

    hmm... has my life got to the point where all I want to do on an exciting evening is implement new rules...someone please kill me.... but wait till I've added the code..
  • HunnenkoenigHunnenkoenig Posts: 1,173Member
    @draper3000: the time will come, when your excitement will be, when you can pee without pains :-)
    Wait with killing until then :-)
  • StormyStudioStormyStudio United KingdomPosts: 3,987Member
    that works a treat...amazing...many thanks to CodeMonkey.

    Due to my own stupid confusion over booleans, as to whether you can write 'true' or 'false' or whether to put numbers '1' or '0' in.

    I changed it a tiny bit, making self.grabbed an integer.

    Then having it so when pressed

    self.grabbed = 1

    then in the otherwise box (so essentially when not pressed)

    self.grabbed = 0

    so it resets it ready for something else to be grabbed.
  • StormyStudioStormyStudio United KingdomPosts: 3,987Member
    OK QUESTION:

    i've got an object that moves when grabbed (now using the way above..).

    It also rotates to the angle of a 2nd touch...
    using rotate to position, of 2nd touch X and 2nd touch Y.

    This works, but it rotates around the centre of the object not where it has now been grabbed. Is it possible to use the offset from above to change where it rotates?
This discussion has been closed.