I have given you examples of how to use forces and impulses to move physics objects around, but I haven't covered joints yet. Joints are used to join two objects together so that the distance between them is the same. We will make a bridge using joints, so, create a new layout and make it look like the one shown in the following screenshot:
The sprites for the bridge and the fence objects are located under the Tiles
folder. Before continuing, check the bridge's collision polygon and fix it so that no lines will intersect each other, just like the buttonBlue
object. You want to be careful when adding physics objects, as small sprites tend to have similar collision polygons.
The fences and bridges are physics objects, but only the fences are immovable; the bridges are still movable, but we won't let them fall to the bottom. We will tie the bridge to the fence using joints. One thing about joints is that you can only make joints between object types, not between different instances of the same object. This is why we have a bridge object and a fence object here, because we can't just joint a bridge with another bridge.
Using joints, we join objects based on their image points; if an object doesn't have an image point, then it will be based on its origin point, which, by default, is the middle of the sprite. Now, we don't want to join the bridge by its origin point, because if you look at it, the origin point is floating above the bridge sprite. So, we will make an image point in the middle of the sprite; this will be imagepoint 1
.
After this, we'll prevent the bridges from rotating by setting their Prevent rotation property to Yes. Switch over to the layout 4
event sheet, and we'll join them together. You should only create joints on triggers, not on the normal events that can have the value of true
over a long time. In our example, we'll create a joint at the start of the layout. Making a joint only needs one action, but as we have two bridges, we'll use the same action twice.
That's it! We have created a joint. Copy and paste the code to make the character move from Layout 3
and test out this layout. You can walk on top of the bridge if you want to.