|
UPDATE 3
MAKING YOUR FIRST PLATFORMER (PART 1)
Functions used in this update
| CODE | require(lua file) makeSpirit() makeBodyRectangle(offset_x,offset_y,size_x,size_y) makeImage() setMother(mothertype, mother) setImage(imagepath) setCenter(x,y)
|
DescriptionTeaches you how to use the script editor and make a player that you can control. MainThe main focus on MechaSource is not only to play others games, but also make your own! I this update we'll teach you how you start your development and end up with a nice platformer! First, we have to start the development of it. This is done by entering your Developer Section.  A popup will ask you alot of information about your upcoming game such as name, developers, website, genre etc.. All of those information can be changed at any time, however, once your game has been released it's name cannot be changed. Anyways we'll call our new game for "Platformer Game". It should appear in "Your Games" in the Developer Section.  You'll see that it's icon is currently a questionmark. We want to change this. For this demonstration I'll be using this image for my game icon:  To do this we go to the "Game Icons" tab in our Developer Section, and change the icon. The image has to be a .png and of size 96x96 pixels.  The icon has nothing to do with the ingame things, but it shows up in the owners Game section instead of a standard questionmark, so changing it is a must for a professional game. Now we want to actually script our game. This we do by using yet another tab called "Manage Your Game". Click it.  which will lead you to the Script Editor!  This is where we will be spending most of our time. Basically it's just like notepad or any other text editors but it highlights stuff so they're easier to use. Note: You can edit the files manually in your Develop folder, however, this Script Editor provides an easier interface. Now, before we start any scripting, we'll just launch our thing here for a second to see what it actually contains at default. You run your game by double-clicking it in the Developer Section.  As you can see, it already contains alot of stuff by default such as a platformer engine. However, we want to remake that, so we'll just empty the "physics" folder inside your Develop\game folder. Now, return to your Script editor. You'll notice as soon as the script editor opens, it'll open up main.lua. Main.lua is the file that gets executed in the begging of your game. You'll see how it uses the require() function to load the "physics/player" lua file at line 16. Remove that line and save. We also have to remove one of the always loops that are used in the default script. Open up "always.txt" in your Develop game root and remove "playermovementupdate()". Lets start the actual making of our platformer. First we'll make a script. Doubleclick somewhere above the script box and a new file should appear. Save it as "player.lua". Here we'll have to create the player Spirit. A Spirit is what controls the player, his velocity, his positions etc.. We do this using the code Note that case does matter at all times in MechaSource. This creates a spirit, however, we need to use this spirit for later, so we'll have to name it first. We do this by adding
| CODE | | player = makeSpirit() |
This tells lua that the variable called "player" is the name of makeSpirit().
Next up we'll have to make a body for the spirit. A body is the collision of the spirit, basically. A spirit in itself cannot collide with anything. This is all quite easy as we can use the code
| CODE | | player:makeBodyRectangle(offset_x,offset_y,size_x,size_y) |
This basically makes a body for the spirit "player". Where the offsets are the offset position of the body compared to the spirits position, and the sizes are the size of the rectangled body. In our example we'll use 0,0,40,65.
Now this doesn't really show anything, what we need to do is create an image to use for our spirit. First, we have to make the image. We do this by using
and ofcourse we have to name it in order to control it. We'll call it playerImage. This is done as above by using
| CODE | | playerImage = makeImage() |
Next up we'll have to position our image relative to the spirit. There are many different ways of doing this. The most obvious one would be setPosition(x,y), however, MechaSource has smarter methods. We can use the functiom
| CODE | | playerImage:setMother(mothertype, mother) |
This sets our playerImage to a child of player (which was our spirit). A child does whatever the mother does, it's position and rotation is the same as the mothers. In "mothertype" we use "spirit" as we are trying to do hook onto a spirit, and "mother" is "player" because we wanna hook onto the spirit named "player". Result is
| CODE | | playerImage:setMother("spirit", player) |
Note that player does not contain quotations. This is because it's a variable.
All left to do is load a picture onto our image. The command for that is
| CODE | | setImage(image path) |
incase you haven't figured out yet, you add "playerImage:" in front of that so that MechaSource knows its for that image. In this demonstration we'll use this image:  (you might be asking "why the wierd shape?", we'll get to that later in part 2) I have this image located in my "images" folder and the image is called "playerbody.png". Note that all files should be located inside your games Develop folder. As a last thing, we need to set our images center. This is the point where the image rotates around. The code for this is: where x is the position in pixels of the horizontal center, and y the same but vertical. Now, we could specify the center in pixels, but if we use 100000, it will automatically figure out the center of the image. If we use 110000, it'll automatically figure out the left/bottom side. Let's do playerImage:setCenter(100000,100000). Now what your script should look like is this:  One thing we need to do though! Currently our script won't run as it isn't connected with Main.lua. Open up main.lua and add this function where "file path" will be "player" (require("player")) as we need to load our player.lua. That being said, let's try and run it!  ... that was very... exciting. What just happened was our player that fell from his standard position (0,0) and to the ground. He starts at 0,0 since we didn't set the spirits position. Next we're gonna make him reacht when certain keys are being pressed. We'll do that in part 2!
--------------------
|