The Armor Games website will be down for maintenance on Monday 10/7/2024
starting at 10:00 AM Pacific time. We apologize for the inconvenience.

ForumsProgramming ForumProblem with my Gravity and jump code

9 4775
Midmenj
offline
Midmenj
216 posts
Nomad

Here is the code:


RightIsDown = function ()
{
return (Key.isDown(Key.RIGHT) && !Key.isDown(Key.LEFT));
};
LeftIsDown = function ()
{
return (Key.isDown(Key.LEFT) && !Key.isDown(Key.RIGHT));
};
UpIsDown = function ()
{
return (Key.isDown(Key.UP) && !Key.isDown(Key.RIGHT));
return (Key.isDown(Key.UP) && !Key.isDown(Key.LEFT));
};
Char.accel = 0;
Char.gravity= 0;

onEnterFrame = function ()
{
if (RightIsDown())
{
Char.gotoAndStop(2);
Char.accel += 1;
Char._xscale = 100;
}
}
if (LeftIsDown())
{
Char.gotoAndStop(2);
Char.accel -= 1;
Char._xscale = -100;
}
else if (LeftIsUP())
{
Char.gotoAndStop(3)
}

else if (Char.ground.hitTest(_x + Char.accel, _y + Char.gravity, true)) {
while (!Char.ground.hitTest( _x,_y, true)){
_y++
}
gravity=0
if (Math.abs(Char.accel) < Char.accel ) { Char.accel = 0; gotoAndStop(1) } else { gotoAndStop(2) }
} else {
gravity ++;
_y += gravity;
if (_y > 1000) { Char.gravity = 0; Char.speed = 0; _y = 200; _x = 200
} else{
if (Char.accel != 0)
{
Char.accel -= Char.accel / Math.abs(Char.accel);
}
Char.gotoAndStop(1);
}
trace(Char.accel);
Char._x += Char.accel;
}

//(if you take out the stuff talking about jump and gravity the left and right move fine.)
anyone who can help i would appreciate it. and also tell me what you did.

  • 9 Replies
master565
offline
master565
4,104 posts
Nomad

A. whats wrong with it

B. as3 or as2

Midmenj
offline
Midmenj
216 posts
Nomad

A. It isn't jumping or have gravity, it just auto animates and stays in place.

B. AS2

master565
offline
master565
4,104 posts
Nomad

well sorry, i could help you if it was as3

gaboloth
offline
gaboloth
1,612 posts
Peasant

Well, it's a pretty messy solution in my opinion. Also, I don't get the point of those returns. If you delete those leftisdown and rightisdown functions, it would be better. Just write
If ( Key.isDown(Key.LEFT))
{
        Char.gotoAndStop(2);
        Char.accel -= 1;
        Char._xscale = -100;
}

simplegoogly
offline
simplegoogly
4 posts
Nomad

IMO you should make some booleans which go true if key is pressed and false on key up. also keep a boolean for hitTest with ground that way you dont have to calculate it every time. I dunno how you are trying to jump here is some code that might help

jumpInter = setInterval(jumpAction, 40);
}

public function jumpAction()
{
char.y += 20;
if (char.y >= 110)
{
char.y = 110;
clearInterval(jumpInter);
jumpInter = setInterval(stopJump, 40);
}
}

public function stopJump()
{
char.y -= 20;
if (char.y <= -50)
{
char.y = -50;
clearInterval(jumpInter);
jumpInter = -1;
}
}

In here I set a setInterval to jump up and then I come down. you can use your gravity thing with this and in stop jump you can check hitTest with ground to stop the character going down. Hope this helps.

gaboloth
offline
gaboloth
1,612 posts
Peasant

That's as3, googly :S
And however, I think that will be like all way up, hitting something invisible and all way down. If you want a more realistic effect you should write
if (Key.isDown(Key.UP))
{
jumping = true
}
if (jumping == true)
{
char._y -= jumpspeed
jumpspeed -= 1
}
if (!Char.ground.hitTest( _x,_y, true))
{
jumping = false
jumpspeed = the starting value
}
so, when jumpspeed become 0 the character will stop in the air for a moment, then jumpspeed will become a negative value and the character will start falling

simplegoogly
offline
simplegoogly
4 posts
Nomad

hey gabo... I meant to give the logic only, I never said will give the code in as2... its just logic.... if you didnt read my post let me say again "IMO you should make some booleans which go true if key is pressed and false on key up. also keep a boolean for hitTest with ground that way you dont have to calculate it every time." which is what u have done.... I was just giving a substitute code...

gaboloth
offline
gaboloth
1,612 posts
Peasant

Yep, you're right about the hitTest part, a Boolean would have simplified stuff. Sorry, i forgot about it.

took95
offline
took95
438 posts
Nomad

this is the code i use in as2, its easy to change up and i just have it saved in a text doc, change what you need


onClipEvent (load) {
var ground:MovieClip = _root.ground;
var grav:Number = 0;
var gravity:Number = 2;
var speed:Number = 15;
var maxJump:Number = -20;
var touchingGround:Boolean = false;
}
onClipEvent (enterFrame) {
_y += grav;
grav += gravity;
while (ground.hitTest(_x, _y, true)) {
_y -= gravity;
grav = 0;
}
if (ground.hitTest(_x, _y+5, true)) {
touchingGround = true;
} else {
touchingGround = false;
}
if (Key.isDown(Key.RIGHT)) {
_x += speed;
}
if (Key.isDown(Key.LEFT)) {
_x -= speed;
}
if (Key.isDown(Key.UP) && touchingGround) {
grav = maxJump;
}
if (ground.hitTest(_x+(_width/2), _y-(_height/2), true)) {
_x -= speed;
}
if (ground.hitTest(_x-(_width/2), _y-(_height/2), true)) {
_x += speed;
}
if (ground.hitTest(_x, _y-(height), true)) {
grav = 3;
}
}

Showing 1-9 of 9