View Full Version : Jumping and sidescrollers
zeldafreak
February 21st, 2007, 11:25 AM
Hey guys, I have been working on a sidescroller system and I have the left and right movements done, but I am at a loss as to how I can have him jump and have the gravity kick in. Here is the current code.
onClipEvent (load) {
fallspeed = 8;
platforms = 1;
speed = 5;
falldec = 1.95;
}
onClipEvent (enterFrame) {
fallspeed += falldec;
this._y += fallspeed;
for (i=1; i<=platforms; i++) {
ground = _root["g"+i];
if (this.hitTest(ground)) {
this._y -= fallspeed;
}
}
if (Key.isDown(Key.SPACE) && jumping != true) {
jumping = true;
}
if (jumping == true) {
//What to put here?
}
if(Key.isDown(Key.LEFT)){
this._x -= speed;
this._xscale =-100;
this.gotoAndStop("run");
//_root._x += speed;
}
else if(Key.isDown(Key.RIGHT)){
this._x += speed;
this._xscale =100;
this.gotoAndStop("run");
//_root._x -= speed;
}
else{
this.gotoAndStop("idle");
}
}
The comments about the Root x position are simply to move the stage later on, not sure about them currently though.
I can upload the Fla later If need be, any help is appreciated. Thanks guys :D
Nich
February 21st, 2007, 03:38 PM
Set the fall speed to something negative.
if, say, you set it to -4, for the next few frames the movement will be (approx)
y-4 (up)
y-2 (up)
y+0 (stopped, in the air)
y+2 (down)
y+4 (down)
zeldafreak
February 22nd, 2007, 10:04 AM
actually I changed the code, falldec is no longer a part of it because it made it so when you hit the ground, you fall through very quickly about 3 seconds later. The first part of the code actually looks like this.
onClipEvent (load) {
fallspeed = 8;
platforms = 1;
speed = 5;
}
onClipEvent (enterFrame) {
this._y += fallspeed;
for (i=1; i<=platforms; i++) {
ground = _root["g"+i];
if (this.hitTest(ground)) {
this._y -= fallspeed;
}
}
Nich
February 22nd, 2007, 05:02 PM
Now I don't know if it's possible to jump, You kind of need the fallDec variable for my method to work. And the jump looks better
but the reason you shot through the floor is that fallspeed still got increasingly larger as you hit the floor. Eventually, you were able to move down without triggering the hittest, causing you to shoot through the floor at high speeds. The simple solution would be to set the fallspeed to 0 when you hit, after you've corrected your position.
zeldafreak
February 25th, 2007, 05:12 PM
Well, I used n99's tutorial and made a makeshift jumping and gravity system. But every time he runs off a platform, he does not fall unless he jumped off.
onClipEvent (enterFrame) {
if (Key.isDown(Key.SPACE) && !jumping) {
jumping = true;
}
if (jumping) {
fight = false;
walking = false;
//checks if running first (But still does not work)
if (Key.isDown(Key.LEFT) || Key.isDown(Key.RIGHT)) {
this.gotoAndStop("jump_run");
} else {
this.gotoAndStop("jump");
}
//resumes
this._y -= jump;
jump -= .9;
if (jump<0) {
falling = true;
}
if (jump<-15) {
jump = -15;
}
}
for (i=1; i<=platforms; i++) {
ground = _root["g"+i];
if (ground.hitTest(this._x, this._y, true) && jumping) {
jump = 15;
jumping = false;
falling = false;
}
//This is where the major problem is, How can I get him to fall when he runs off the platform?
if (ground.hitTest(this._x, this._y, false) && !jumping) {
trace("hit");
this._y += fallspeed;
}
}
SacrificialLamb
February 25th, 2007, 08:01 PM
I made it so it's always falling and you have to tell it to stop every frame. I also redid a lot of the code so I could understand it more, I think it dose mostly the same things + being fixed
onClipEvent (load) {
fallspeed = 8;
platforms = 1;
speed = 5;
falldec = 1.95;
fall = 0;
}
onClipEvent (enterFrame) {
fall+=2;
if (walking) {
if (Key.isDown(Key.LEFT)) {
xspeed = -speed
this._xscale = -100;
this.gotoAndStop("run");
} else if (Key.isDown(Key.RIGHT)) {
xspeed = speed
this._xscale = 100;
this.gotoAndStop("run");
} else {
xspeed = 0
this.gotoAndStop("idle");
}
}
for (i=1; i<=platforms; i++) {
ground = _root["g"+i];
if (ground.hitTest(this._x, this._y+1, true)) {
fall = 0;
walking = true;
} else if (ground.hitTest(this._x, this._y+fall, true)) {
while (ground.hitTest(this._x, this._y+fall, true)) {
trace("tap");
fall--;
}
walking = true;
}
}
if (Key.isDown(Key.SPACE) && fall<1) {
fall = -20;
walking = false;
}
this._y += fall;
this._x += xspeed;
}
zeldafreak
February 26th, 2007, 10:15 AM
Wow,that's great man, I wanted to have the character lock his x position when he jumped anyway. The only problem is that if you hold space he will continually jump up, and right now I cannot find a way to stop that...:stare:
SacrificialLamb
February 26th, 2007, 02:28 PM
sorry theought i had that make "fall<1" "fall==0"
zeldafreak
February 26th, 2007, 07:49 PM
wait, what am I supposed to change here?
RidingDirty
February 26th, 2007, 08:52 PM
I believe he is reffering to the 6th line from the bottom....
if (Key.isDown(Key.SPACE) && fall<1) {
And that your supposed to change it to:
if (Key.isDown(Key.SPACE) && fall==1) {
But thats just what I think. Try it out, if it doesent work its easy to change it back ;)
zeldafreak
February 26th, 2007, 09:00 PM
Hmm, that Didn't do anything different. He just jumped is beats rather than one fluid high jump.:puzzle:
SacrificialLamb
February 27th, 2007, 12:15 AM
Really I thought that would fix it (should have tested it).
But really could you not work that out by yourself, if you move the SPACE in to the ground.hitTest so it'll only work when you are touching the ground
onClipEvent (load) {
fallspeed = 8;
platforms = 1;
speed = 5;
falldec = 1.95;
fall = 0;
}
onClipEvent (enterFrame) {
fall += 2;
if (walking) {
if (Key.isDown(Key.LEFT)) {
xspeed = -speed;
this._xscale = -100;
this.gotoAndStop("run");
} else if (Key.isDown(Key.RIGHT)) {
xspeed = speed;
this._xscale = 100;
this.gotoAndStop("run");
} else {
xspeed = 0;
this.gotoAndStop("idle");
}
}
for (i=1; i<=platforms; i++) {
ground = _root["g"+i];
if (ground.hitTest(this._x, this._y+1, true)) {
fall = 0;
walking = true;
if (Key.isDown(Key.SPACE)) {
fall = -20;
walking = false;
}
} else if (ground.hitTest(this._x, this._y+fall, true)) {
while (ground.hitTest(this._x, this._y+fall, true)) {
trace("tap");
fall--;
}
walking = true;
}
}
this._y += fall;
this._x += xspeed;
}
zeldafreak
February 27th, 2007, 09:18 AM
Awesome man, thank you so much, I can't believe I didn't see something as basic as moving it into the hittest! :P Thanks again, I may post with more questions.
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.