00:00
00:00
Newgrounds Background Image Theme

PalmVoe just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

Foss: Ground Code

1,787 Views | 7 Replies
New Topic Respond to this Topic

Foss: Ground Code 2005-11-22 23:21:33


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 >:(

Response to Foss: Ground Code 2005-11-23 00:36:23


nice. archer guys pissing me off a little, but some great as. this game should be very cool.

Response to Foss: Ground Code 2005-11-24 07:59:08


I shortened the code a bit =P

onClipEvent (enterFrame) {
_y += 10*(_root.ground.hitTest(_x, _y, true) ? false : true);
for (q=1; q<=5; q++) _y -= q*_root.ground.hitTest(_x, _y-q, true);
_x += 5*(Key.isDown(Key.RIGHT)-Key.isDown(Key.LE
FT));
}


BBS Signature

Response to Foss: Ground Code 2005-11-24 08:21:14


I've used a hitTest inside a while loop in the past to prevent any ground sinking, because The ground was pretty graphics-intensive, so a plain onEnterFrame didnt work as well, but looks fine on yours, no ground pattern or anything so doesnt lag.

You should change still the guy's attack animation like i suggested in the preview thread you made for this, that little poke he does just looks a bit pussy-ish :D But its gonna be cool when finished.

Response to Foss: Ground Code 2005-11-24 11:10:45


At 11/24/05 08:21 AM, T-H wrote:

Hehe, maybe I will, and maybe I won't.
I used to have a while loop, but it lagged like a bitch, so I created this code.
Thanks Rantzien for shortening it.

Response to Foss: Ground Code 2005-11-24 11:44:32


Ah, that right-click pause thing was cool, how'd you do it?

Also, I would suggesty moving the astle in the background as well, only slower

Response to Foss: Ground Code 2005-11-24 11:47:56


At 11/24/05 11:10 AM, frostedmuffins wrote:
At 11/24/05 08:21 AM, T-H wrote:
Hehe, maybe I will, and maybe I won't.
I used to have a while loop, but it lagged like a bitch,

really? weird...

Response to Foss: Ground Code 2005-11-24 11:58:09


At 11/24/05 11:47 AM, T-H wrote:
At 11/24/05 11:10 AM, frostedmuffins wrote:
At 11/24/05 08:21 AM, T-H wrote:
Hehe, maybe I will, and maybe I won't.
I used to have a while loop, but it lagged like a bitch,
really? weird...

well, think about it, it's running the action tons of times in a split second, and that's for each object that it's needed on. That would be up to 12 enemies in my game, the character, and the horse. 14 objects that are having actions run tons of times in a short period of time, bad lag.
Thanks fwe, I was pondering on making it bigger as you go further, but it looked funny, so I'll just make it move.