Be a Supporter!

[AS3] character jump code help

  • 905 Views
  • 6 Replies
New Topic Respond to this Topic
ABCD01
ABCD01
  • Member since: Apr. 21, 2007
  • Offline.
Forum Stats
Member
Level 04
Artist
[AS3] character jump code help Aug. 8th, 2012 @ 07:34 PM Reply

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');
}

}
}

egg82
egg82
  • Member since: Jun. 24, 2006
  • Offline.
Forum Stats
Member
Level 05
Game Developer
Response to [AS3] character jump code help Aug. 9th, 2012 @ 07:14 PM Reply

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
MiloticMaster
MiloticMaster
  • Member since: Oct. 26, 2010
  • Offline.
Forum Stats
Member
Level 12
Blank Slate
Response to [AS3] character jump code help Aug. 9th, 2012 @ 08:51 PM Reply

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.

MiloticMaster
MiloticMaster
  • Member since: Oct. 26, 2010
  • Offline.
Forum Stats
Member
Level 12
Blank Slate
Response to [AS3] character jump code help Aug. 9th, 2012 @ 08:57 PM Reply

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.

egg82
egg82
  • Member since: Jun. 24, 2006
  • Offline.
Forum Stats
Member
Level 05
Game Developer
Response to [AS3] character jump code help Aug. 9th, 2012 @ 11:18 PM Reply

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
ABCD01
ABCD01
  • Member since: Apr. 21, 2007
  • Offline.
Forum Stats
Member
Level 04
Artist
Response to [AS3] character jump code help Aug. 10th, 2012 @ 09:50 AM Reply

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.

MiloticMaster
MiloticMaster
  • Member since: Oct. 26, 2010
  • Offline.
Forum Stats
Member
Level 12
Blank Slate
Response to [AS3] character jump code help Aug. 11th, 2012 @ 04:49 PM Reply

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.