I have an actor named ball and I want to move it in a straight line which is created by actor ball's X and Y coordinate and cursor's X and Y coordinate. I have Move To behavior in gamesalad but I don't have exact idea how to move the ball with two Cartesian coordinates ball and cursor.
2 Answers
\$\begingroup\$
\$\endgroup\$
Here's how to solve your issue inside GameSalad:
- Create 2 Game Attributes: TargetX and TargetY (both should be real)
- Open your Cursor Actor.
- Add two Constrain Attribute behaviors to the Cursor: 1 - Constrain self.location.x to touch1.x; 2 - Constrain self.location.y to touch1.y)
- Add two more Constrain Attribute behaviors to the Cursor: 1 - Constrain TargetX to self.location.x; 2 - Constrain TargetY to self.location.y
- Open your Ball Actor.
- Add MoveTo Behavior.
- Set MoveTo Coordinates to Match Custom Target Coordinates (X = TargetX; Y = TargetY)
Now drop your ball actor into your scene and test away.
\$\begingroup\$
\$\endgroup\$
1
I don't know about gamesalad in particular, but the general way you solve this problem is the following:
- Translate the cursor's screen coordinates into your world coordinates (so it's in the same coordinate system as the ball). This is your target position.
- Get the vector from the ball to the target position by subtracting the ball position from the target position. This is your unnormalized direction vector.
- Normalize your direction vector
- Figure your your per-frame movement amount. Usually by multiplying your units/second desired speed by the frame time.
- Multiply your normalized direction vector by the per frame movement amount.
- Add that per frame movement amount to your ball's position.
-
\$\begingroup\$ This is a good general explanation for most 2D game programming, but completely unrelated to how things work in GameSalad. \$\endgroup\$– jhockingCommented Feb 4, 2012 at 2:40