Make a bridge to cross water barrier

1

Attached Files

The following files have been attached to this tutorial:

.capx

bridge-over-water-demo.capx

Download now 102.22 KB

Stats

2,252 visits, 3,494 views

Tools

Translations

This tutorial hasn't been translated.

License

This tutorial is licensed under CC BY 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.

I thought this might help some newer users understand sometimes it takes a combination of steps to achieve a desired result. There will always be multiple ways to perform a result in any form of programming. This is just one that I can see.

Obvious to do this we need some basic items ( if you need help with setting this up please read the first tutorial for beginners and then come back).

I like layers they keep things locked and neat. I made a layer for the background then on that layer created a simple non animated grass Tiled Background object. Made it the same size as my project window in the sample 640 x 480

Then I create a main layer this is where all of my moving objects are.

First a player using a sprite object we will set to the 8Direction movement. In my sample I just made the player to be a white square.

We will need water this can be a simple square (make certain to add behavor solid so it is a barrier) . in my demo I added some quick animating lines for waves. Then added an effect to make it wavier using effect wave ripple. To add effects just click on your water object then in properties panel click add effect select for this wave/ripple. Then in properties for the water object I toned it down so the waves were not outside of my bridge size. that took some playing around to learn the adjustments but then I settled on Frequency 1, Amplitude 1.5 and then speed -3.

The bridge is a little tricky it is three parts which I will explain later these are all sprite objects. See them in the first picture separated we will use events to bring these together. In the left and right sides I set collision to be a rectangle that covered the width and height of those parts. For the main bridge section I used a rectangle to cover only the area we will walk on. When I drew this I started with a drawing of the full bridge to get my size for the all of the parts

I then copied the main bridge and erased all but the needed sides once for left and once for right. I added behavior Solid to these. Now I have all the parts it is time to code in the events.

In this demo things are simple, movement is player with key controls.

Bump into either side of bridge to pick it up and move it.

Pressing space drops it.

First event is to assemble the bridge.

This keeps bridge together every tick the ends follow the main.

When I created my player I gave it an instance variable to determine if bridge is to move or not. This is a numeric value called Bridge_Move I like to use the underscore instead of running words together for readability. Bridge move is changed in three ways contact with an edge of the bridge left or right and cleared (reset to 0) with press of space (this is to drop or place the bridge). Notice the events where there is an X in them this is an inverted event. In my game the event checks to see if player is in contact with main bridge inverting this means player is not touching the main bridge. This is for later use but important to be included here.

If the bridge move is no longer 0 then the program will make bridge follow the player position with some offset for left and right as well as one side from top other from bottom. This enable us to move the bridge after we have crossed it. So the program checks the Bridge _Move value if it is 1 or 2 then the bridge is moved 1 for left side from top and 2 for right side at bottom.

Now is a good time to see how your doing if you have followed well you program should move and grab the bridge from left or right and drop it at the press of space. Okay now that we can move the bridge to cross the water we need to tell the program to check for us crossing and allow us to cross the water which is uncross-able without the bridge since it is a solid object.

The main part of the bridge is not solid so we can walk over it while we are on it the program disables the water collision so it does not stop us. When we are off the bridge the program turns water collisions back on So we cant cross them without the bridge.

Earlier in the image where we were checking for collision with the bridge sides we have two conditions the first tells us we are touching the bridge side the second confirms we are not on the bridge. This way we don't accidentally pick up the bridge we are trying to cross.

This is it not too much to do such an important task. You can build onto this to code into your games things like tunneling trough walls or perhaps make the bridge into a raft to carry the player over wider water steered to another shore. Imagination is all that holds us back in programming the less you have pre-built for you the more you need to be able to devise a way to get there. This was shown in a top down view but could be worked into a sidescroller as well.

Thanks and happy coding to ya!

.CAPX

bridge-over-water-demo.capx

Download now 102.22 KB
  • 0 Comments

  • Order by
Want to leave a comment? Login or Register an account!