http://newgrounds.co../topic.php?id=314338
FOSS: Main
Ok, I suck at explaining things, and this is my first FOSSy thingy, I still don't know what it means. Ok, well, here it goes!
Many people have fancy ways of doing platforms in games, that are ok, but take up lots of code and don't work in all instances, and you have to name all of your platforms and if you want to add to the ground, you have to change lots of stuff. Well, this is where shapeFlag comes in! The fancy hitTest!
draw your ground, put it as a movie clip and place it on the main timeline. Now, name it "ground". Make your character, a circle or whatever, for tests purposes. Make that a movie clip and place it on the main timeline. Time for some coding! Yay
Place this on the circle MC:
onClipEvent(load) {
//speed of gravity, change as you will
fallspeed = 10
//speed you move left and right of
movespeed = 5
}
onClipEvent(enterFrame) {
//checks to see if the character is hitting the ground. It tests the whole shape of the ground versus one point, one coordinate, the _x and _y of the circle.
if (_root.ground.hitTest(_x, _y, true)) {
//the character is not in the air
inair = false
} else {
//the character is in the air
inair = true
}
if(inair == true) {
//if the character is in the air, it drops according to the fallspeed variable
_y += fallspeed
}
//loops q to a number 1-5, you can change the number 5 to a higher number to make it a better code, but the higher the number, the laggier it will be
for (q=1; q<=5; q++) {
//checks to see if the land hits the coordinates of the circle, minus q for the _y coordinate. Example, if you are hitting the ground at under 5 pixels, it will run the following code 5 times
if (_root.ground.hitTest(_x, _y-q, true) ) {
//basically, no matter what q is, it will return _y -= 1, wait, why didn't I just make it _y -= 1? ah, who knows, this looks cooler. If the code is run 5 times for 5 pixels under, then it will be risen 5 pixels, to the top of the ground. If it is run 3 times, for 3 pixels under, it will be risen 3 pixels, get it?
_y -= q-(q-1);
}
}
//need I explain the following?
if(Key.isDown(Key.RIGHT)) {
_x += movespeed
}
if(Key.isDown(Key.LEFT)) {
_x -= movespeed
}
}
Ok, done, test it out.
Most platform codes are long and laggy and still don't work on slopes and such. This will make sure that you are always above the ground, so it works! Only problem is with big slopes, but you can avoid that by making a wall code, I'll leave that to another FOSS.
Example: (I've been plugging this game everywhere)
http://media.putfile.com/Sworded74
All the things in that game that follow the ground (ie: main character, enemies, horse) were all done using that code
This is my happiest creation, and most useful that I've made entirely, so I'm happy with it.
Love me now >:(