Difference between revisions of "Flappy Tim - Part One"

From Contraptionmaker Wiki
Jump to: navigation, search
Line 36: Line 36:
 
   FlappyTim.addForce(CM.DIR_RIGHT,5);
 
   FlappyTim.addForce(CM.DIR_RIGHT,5);
 
  }
 
  }
<code>
+
</code>
  
 
This time when you hit play FlappyTim will jump up and to the right, and possibly fall off the screen if the floor isn’t long enough.
 
This time when you hit play FlappyTim will jump up and to the right, and possibly fall off the screen if the floor isn’t long enough.

Revision as of 23:35, 4 September 2014

If you are just getting started, be sure to read Contraption Maker Modding Guide first.

The very first mod we at Spotkin decided to make is the new “Hello, World” of games - a clone of the game “Flappy Bird”. We were amazed at how little code it took. Let’s walk through the construction of “Flappy Tim” step by step.

The first thing you need to do is create a simple level with a Toolman Tim and a wall piece as a floor. Then you will need to give Toolman Tim a script name so that we can access him from the script. You do this by clicking the script button after selecting Toolman Tim.

Selecttoolman.png

Clicking that button brings up a dialog where you can enter the name. We’ll call him “FlappyTim”.

Nameflappy.png

Contraption Maker will automatically create the script object “FlappyTim” so that it can be accessed with the modding script. Say for example we wanted to make FlappyTim jump when the level first starts. We could add a force to him like this:

function onStart()
{
  FlappyTim.addForce(CM.DIR_UP,5);
}

There a few new things introduced in this one line of code that you should notice.

We are calling a method on a script object that we named in the Make editor. That object’s name is FlappyTim. We are calling a method on that object called “addForce” and passing in two parameters: a direction in which to apply the force and the size of the force. Technically, in physics terms, we are actually adding an impulse. The direction is a property on a global object “CM”. You will frequently use this object’s properties and methods when writing mods. This global object is always available from any function. For a complete list of properties and methods on the CM object, see the Javascript API Documentation: [link]

Try entering this script and hitting play. You should see FlappyTim jump up in the air!

Just for fun, try adding another force to the right:

function onStart()
{
  FlappyTim.addForce(CM.DIR_UP,5);
  FlappyTim.addForce(CM.DIR_RIGHT,5);
}

This time when you hit play FlappyTim will jump up and to the right, and possibly fall off the screen if the floor isn’t long enough.