ForumsProgramming ForumPlatformers...

9 3229
Gfitz07
offline
Gfitz07
203 posts
Nomad

I am making a platform game where its the side view. When i do it the character never stays on the top of the platform, he sinks in it a bit. here is the script on the character:
onClipEvent (load) {
speed = 3;
gravity = 7;
jumping = false;
jump = 0;
fall = false;
}
onClipEvent (enterFrame) {
if (!_root.platform.hitTest(this._x, this._y, true) && !jumping) {
_y += gravity;
}
if (_root.platform.hitTest(this._x, this._y, true) && falling) {
jump = 8;
falling = false;
jumping = false;
}
}
onClipEvent (enterFrame) {
if (Key.isDown(65)) {
_x -= speed;
gotoAndStop(2);
_xscale = -100;
}
else if (Key.isDown(68)) {
_x += speed;
gotoAndStop(2);
_xscale = 100;
} else {
gotoAndStop(1);
}
if (Key.isDown(87) && jumping == false) {
jumping = true;
}
if (jumping) {
this._y -= jump;
jump -= .5;
if(jump<0) {
falling = true;
}
if(jump <-10){
jump =-10;
}
}
}

this is AS2 btw.

  • 9 Replies
Gfitz07
offline
Gfitz07
203 posts
Nomad

Oh the ground is called &quotlatform" frame 1 is idle and frame 2 is walking.

Programpro
offline
Programpro
562 posts
Nomad

since the guy has a y velocity, he isn't just moving...he's skipping down at that rate. So, when he hits the platform, you tell him to stop, but he has still skipped, so he isn't perfectly on top...maybe 3 pixels on or something. Anyway, add code that says that if he is on one, position him on top.

onClipEvent (enterFrame) {
if (!_root.platform.hitTest(this._x, this._y, true) && !jumping) {
_y += gravity;
} else {
jump = 8;
falling = false;
jumping = false;
_y = _root.platform._y

}
}


This is assuming that the guy's center is at his feet and the platform's is at it's top-left corner. The else is just more powerful coding I added to help.

Btw, great to see another AS2 programmer! But, I'm soon going to make the jump to 3...<sigh>.

Hope this helps!

Gfitz07
offline
Gfitz07
203 posts
Nomad

Thanks but it didnt work. the character keeps jumping up and down. i think its because of this: _y = _root.platform._y

its one of those games where you jump from platform to platform. like FPA (fancy pants adventures)

Thanks anyways!

Klaushouse
offline
Klaushouse
2,770 posts
Nomad

Lol platformers.. I have no clue where to start. Always have problems with the walls that's it! :P

Programpro
offline
Programpro
562 posts
Nomad

sorry, I should have said "_root.platform._y + 1". That way, it's guaranteed to be overlapping the platform so the hitTest will come false and it won't fall again.

I hope that THIS now helps!

Programpro
offline
Programpro
562 posts
Nomad

hitTest will come false


i mean true...sorry!
Gfitz07
offline
Gfitz07
203 posts
Nomad

thanks but it STILL isn't working somehow. he still sinks in the ground and instead of jumping he will fly.

HAXALLNOOBS
offline
HAXALLNOOBS
19 posts
Nomad

try this code on the character:

onClipEvent (load) {
scale = _xscale;
friction = 2;
airresist = 1;
gravity = 2;
jumpamount = 17;
speed = 12;
onfloor = true;

}
onClipEvent (enterFrame) {
if (Key.isDown(Key.LEFT)) {
_xscale = -scale;
_x -= walkSpeed;
this.gotoAndStop(2);
}
if(Key.isDown(Key.UP) and onfloor){
jump = true;
onfloor = false;
yvel = -jumpamount;
}
if (Key.isDown(Key.RIGHT)) {
_xscale = +scale;
_x += walkSpeed;
this.gotoAndStop(2);
}
}
onClipEvent (keyUp) {
this.gotoAndStop(1);
walkSpeed = 25;
}
onClipEvent(enterFrame){
if(xvel < 0){
if(onfloor){
xvel += friction
} else {
xvel += airresist
}
} else if(xvel > 0){
if(onfloor){
xvel -= friction
} else {
xvel -= airresist
}
}

_root.guy._y += yvel
if(!_root.platforms.hitTest(_x,_y+1,true)){
yvel += gravity;
} else {
while(_root.platforms.hitTest(_x,_y+1,true)){
_root.guy._y -= 1;
}
onfloor = true;
yvel = 0;
}
}

onClipEvent (enterFrame) {
if (this._x>=434) { //this is customizable to fit screen
this._x = 430;
}
if (this._x<=142) { //this is customizable to fit screen
this._x = 145;
}
}

Zrb
offline
Zrb
8 posts
Nomad

There's no need for all of those codes, just go in your character movie clip and drag the stuff inside above the registration point.

OR

Instead of this code:
if (!_root.platform.hitTest(this._x, this._y, true) && !jumping) {
_y += gravity;
}
if (_root.platform.hitTest(this._x, this._y, true) && falling) {
jump = 8;
falling = false;
jumping = false;
}
Write this:
if (!_root.platform.hitTest(this._x, this._y+3, true) && !jumping) {
_y += gravity;
}
if (_root.platform.hitTest(this._x, this._y+3, true) && falling) {
jump = 8;
falling = false;
jumping = false;
}

IF YOUR CHARACTER KEEPS SINKING IN, REPLACE THE 3 BY SOMETHING BIGGER.

Showing 1-9 of 9