How do I constrain a specific area of an actor to mouse X and Y?

I recently watched this tutorial "Actor Constraints in GameSalad" detailing how to constrain the center of an actor to the mouse X and Y, but I would like to constrain the relative actor pixel clicked to the mouse X and Y, not the center of the actor.

In other words, I want to avoid the "jumping" nature of "constrain self.Position.X to game.Mouse.Position.X" when I click and drag.

Comments

  • MobileRocketGamesMobileRocketGames Member Posts: 128
    edited November 2012
    One simple way to achieve this would be to use the coordinates of the mouse in a formula.

    So lets say there is a 100x100 scene, and there is a 100x100 square in the center of that scene. If your mouse is at 20x30 and you click. You want to constrain the boxes self.Position.X to the game.Mouse.Position.X+(or minus) the distance between the mouse and the center of the box actor. In this case that is 30x20.

    To do that:

    Create 2 attributes (real) inside the actor. Name them OffsetX and OffsetY.

    Create a rule that says, when touch is pressed:
      change attribute self.OffsetX to (self.Position.X-game.Mouse.Position.X)
      change attribute self.OffsetY to (self.Position.Y-game.Mouse.Position.Y)
    Then create another rule that says, while mouse button is down or whatever (up to you how you want to initiate the following function):
      Timer: After .05 seconds:
        Constrain self.Position.X to (Mouse.Position.X + self.Offset.X)
        Constrain self.Position.Y to (Mouse.Position.Y + self.Offset.Y)
    That timer is necessary and although I don't condone the use of timers, this wasnt meant to be an advanced solution and will most likely work for whatever your purpose is/was.

    As a side note, if you have questions like these its best to create your threads as a question, not a discussion, that way people that provide helpful advice are acknowledged and the thread can be closed when a solution is provided.
    You can always rate my post "Insightful" if it was helpful though.
    Good Luck!
Sign In or Register to comment.