00:00
00:00
Newgrounds Background Image Theme

nuggetior 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!

[AS3] character jump code help

1,544 Views | 6 Replies
New Topic Respond to this Topic

[AS3] character jump code help 2012-08-08 19:34:12


This is one of the most common questions out there but one of the most difficult to understand when it comes to noobs like me. I've been working on this for days and still I haven't made any progress. So here is the problem. I have my character moving and have all these animations. He runs facing right. He runs facing left. He jumps up and comes back down but here is what I want him to do: Jump once every key pressed down. He's like a kangaroo whenever the key is held down. So here it is (out of frustration), how do you code a jump once code? Here's my code if anyone out there would like to take a look:
jeffHero_mc.gotoAndStop('idle');

var Key:KeyObject = new KeyObject(stage);

var dy:Number = 0;
var gravity:Number = 1;
var canjump:Boolean = false;

stage.addEventListener(Event.ENTER_FRAME, onenter);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);

function keyPressed(event:KeyboardEvent):void
{
if(Key.isDown(Key.SPACE) && canjump == true)
{
canjump = false;
jump();
}
}

}
function keyReleased(event:KeyboardEvent):void
{
if(Key.isDown(Key.SPACE))
{
canjump = false;
}
}

function onenter(e:Event):void
{

dy+=gravity;

if(jeffHero_mc.y>=300)
{
dy=0;
canjump=true;
}

jeffHero_mc.y+=dy;

for(var i:int = 0; i<.03; i++)
{
if(floor_mc.hitTestPoint(jeffHero_mc.x, jeffHero_mc.y, true))
{
dy=0;
canjump=false;
}
}

if(Key.isDown(Key.RIGHT))
{
jeffHero_mc.x+=5;
jeffHero_mc.scaleX=0.3;
jeffHero_mc.gotoAndStop('walking');

}else if(Key.isDown(Key.LEFT))
{
jeffHero_mc.x-=5;
jeffHero_mc.scaleX=-0.3;
jeffHero_mc.gotoAndStop('walking');

}else if(Key.isDown(Key.SPACE))
{
jeffHero_mc.y-=15;
jeffHero_mc.scaleX-=0.3;
jeffHero_mc.scaleX+=0.3;
jeffHero_mc.gotoAndStop('jumping');
}
{
if(!Key.isDown(Key.RIGHT) && !Key.isDown(Key.LEFT) && !Key.isDown(Key.SPACE))
{
jeffHero_mc.gotoAndStop('idle');
}

}
}

Response to [AS3] character jump code help 2012-08-09 19:14:10


you should use the code tags, it preserves formatting and makes things easier to read.

I would make up a code snippet and put it up, but after attempting to do so I realized that i'm just too damn tired and sick to think that hard.

Damn flu...


Programming stuffs (tutorials and extras)

PM me (instead of MintPaw) if you're confuzzled.

thank Skaren for the sig :P

BBS Signature

Response to [AS3] character jump code help 2012-08-09 20:51:55


Wait you dont want him to jump like a kangaroo? Your msg is a little confusing~
Anyway, I see that you make 'canjump' true when he hits the ground, meaning that as soon as he hits the ground,
the key code with activate again and cause your kangaroo effect. Technical explanation is the KeyboardEvent keyDown acts like onEnterFrame (in my experience, trying tracing it with your EnterFrame and you'll see).
The problem is simple, and there are multiple solutions.
-You could make two booleans, 'canjump' and 'keyreleased', keyreleased inside the keyReleasedEvent only becoming true if canjump is true.

function keyPressed(event:KeyboardEvent):void {
if(Key.isDown(Key.SPACE)){
canjump = false;
}
}
function keyReleased(event:KeyboardEvent):void {
if(Key.isDown(Key.SPACE)&&canjump) {
      keyrelased = true;
      }
}

Another solution is a brute force method- remove the keyPressed eventListener until keyReleased activates. I'll suggest the 2 Booleans though.
PS If you're wondering why keyPressed continuously activates while keyReleased doesnt, its so u can hold down keys in text boxes like thisssssssssssssssssssssssssssssssssssssssssssssssssssssssss ssssssssssss
PPS Also, how r u using Key.isDown in AS3?
PPPS Get Well Soon, egg82


There are redundant words here.

Response to [AS3] character jump code help 2012-08-09 20:57:20


At 8/9/12 08:51 PM, MiloticMaster wrote: function keyPressed(event:KeyboardEvent):void {
if(Key.isDown(Key.SPACE)){
canjump = false;
}
}

Sry, my code is a little off here-

function keyPressed(event:KeyboardEvent):void {
 if(Key.isDown(Key.SPACE)&&canjump&&keyreleased){
 canjump = false;
jump();
 }
 }
//btw
if(canjump)
//Is equal to
if(canjump==true)

There are redundant words here.

Response to [AS3] character jump code help 2012-08-09 23:18:51


At 8/9/12 08:51 PM, MiloticMaster wrote: PPPS Get Well Soon, egg82

thankies :)

here's my code. It's not finished, but i'm pretty sure I had a point with it:

private var grate = 0.07;
		private var gmin = 1;
		private var gmax = 20;
		private var jrate = 0.07;
		private var jmin = 1;
		private var jmax = 10;
		
		public function jump(mc:MovieClip, gravity:int, jump:Boolean):void {
			if (!jump && gravity == 0) {
				jump = true;
				
			}
		}

gmin and jmin are just there so the multiplication works (0*0.07 is 0)
if you hit ground, just set the gspeed to 0;

jspeed = jump speed
gspeed = gravity speed

That's a basic outline of something I would do to solve your problem. Multiplication is your friend when it comes to realistic physics.


Programming stuffs (tutorials and extras)

PM me (instead of MintPaw) if you're confuzzled.

thank Skaren for the sig :P

BBS Signature

Response to [AS3] character jump code help 2012-08-10 09:50:25


At 8/9/12 08:57 PM, MiloticMaster wrote:
At 8/9/12 08:51 PM, MiloticMaster wrote: function keyPressed(event:KeyboardEvent):void {
if(Key.isDown(Key.SPACE)){
canjump = false;
}
}
Sry, my code is a little off here-

function keyPressed(event:KeyboardEvent):void {
if(Key.isDown(Key.SPACE)&&canjump&&keyreleased){
canjump = false;
jump();
}
}
//btw
if(canjump)
//Is equal to
if(canjump==true)

@MiloticMaster it didn't work on both solutions. and also on ur question about the Key.isDown? i'm just a noob. please take it easy on me. i'm only following this guy in youtube called Senocular. i've tried emailing him about this too. there is still no response. but still, i thank u for responding to this. i am very grateful to u and egg82.

@egg82 even though u stated it's not finished yet, i'm gonna try to figure it out. what line to put where it goes. and again thank u. i will make sure to put u both on the credits.

Response to [AS3] character jump code help 2012-08-11 16:49:41


At 8/10/12 09:50 AM, ABCD01 wrote: @MiloticMaster it didn't work on both solutions. and also on ur question about the Key.isDown? i'm just a noob. please take it easy on me. i'm only following this guy in youtube called Senocular. i've tried emailing him about this too. there is still no response. but still, i thank u for responding to this. i am very grateful to u and egg82.

@egg82 even though u stated it's not finished yet, i'm gonna try to figure it out. what line to put where it goes. and again thank u. i will make sure to put u both on the credits.

It didnt work? Awwwww... Anyways egg82's version is better and more complex. I just suggested the double Boolean method cause I dunno how what 'jump()' does, and I didnt want to suggest too much since it seems like you're coding on the frame. As long as understand what I was doing, (using a second Boolean to check if the key was released before allowing jump to activate again) its fine.
As for the Key.isDown; dont worry about it, its just an as2 thing and Senocular probably made an object to help with the transition.


There are redundant words here.