GameSalad.com beta game creation for the rest of us ™

Make iPhone Games

How do I make an actor follow the mouse?

Steps for Mouse Position Movement

To have an actor's movement based on the position of the mouse, you will just need two Constrain Attribute behaviors.

  1. Add one Constrain Attribute behavior to the actor and set it to constrain the actor's X position to the value of the mouse's X position.
  2. Add one Constrain Attribute behavior to the actor and set it to constrain the actor's Y position to the value of the mouse's Y position.

Note: This will only work if your scene does not scroll. i.e. You have the default scene size.

Following the Mouse Position on a Scrolling Scene

If you wanted to change the size of the scene, you would need to use the scene's camera's origin to have accurate positioning. See the instructions here to see how that is done.

Following the Mouse Position With an Offset

If you wanted to have an actor follow the mouse relative where you clicked the actor, incorporate the following:

Attributes

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

Rules

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
This version (22/10/09 09:16) was Approved by Administrator.