Mickey Hooks is a popular mobile game with a pendulum game mechanic. The main character in the game swings across dangerous terrain by clicking and holding a pendulum block in the scene. This game mechanic can easily be re-created in GBs using the Distance Constraint component. Almost all components that are set up in the GameBuilder Studio (GBs) editor can have their properties changed at runtime. Either by writing custom code or by triggering a change property action. All components have public properties for things like alpha, position, scale, etc. Constraints in GBs are simulated in the physics engine and they all have a couple basic properties like spatial1, spatial2, anchor1, anchor2, active, and ignore. The spatial1 and spatial2 properties are the basic properties needed to get a constraint working. Normally both spatials would be selected in the editor when setting up the constraint but in this case we need to be able to define a constraint at runtime between the object we want to swing back and forth and a stationary floating block when the player presses down on it.
Here is the final result of the pendulum behavior. Download the project source files and give it a try for your self. Click and hold on the squares in the scene below to see the distance constraint in action. Examine the components to see how easy this is to setup.
The above example primarily reduces the jointMax property of the Distance constraint to pull the ball toward a block. Each block has its own distance constraint to create a distance constraint between a block and the ball's spatial. On mouse down of the block we set the spatial2 property of the Distance constraint and also set the jointMax property to the current distance between the block and the ball. The distance is easily determined using the distance() helper method in an expression. On mouse up we clear the spatial2 property which essentially destroys the constraint. Another approach here could be to just activate and de-activate the constraint instead of destroying it. Just toggle the active property on the constraint at runtime.