-
Need Help troubleshooting a ball jumping
My ball jumps fine and lands fine but if im jumping to the side and i let go of the space he just stops going left or right and drops to the ground i need him to be able to keep going in the direction i pressed even if i release the jump button this is the code i have
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class PlayerJumping extends Sprite
{
private var ball:Ball;
private var vx:Number = 0;
private var vy:Number = 0;
private var ax:Number = 3;
private var ay:Number = 20;
private var gravity: Number = 2;
private var floor:Number = 300;
private var jumping: Boolean;
public function PlayerJumping()
{
init();
}
private function init():void
{
ball = new Ball ;
addChild(ball);
ball.x = stage.stageWidth / 2;
ball.y = stage.stageHeight / 2;
addEventListener(Event.ENTER_FRAME,onEnterFrame);
stage.addEventListener(KeyboardEvent.KEY_DOWN,onKe yDown);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
}
private function onEnterFrame(event:Event):void
{
vy += gravity;
ball.x += vx;
ball.y += vy;
onFloor();
}
private function onKeyDown(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.LEFT :
vx = - ax;
break;
case Keyboard.RIGHT :
vx = + ax;
break;
case Keyboard.SPACE :
if(jumping == false)
{
vy = - ay;
jumping = true;
}
break;
default :
break;
}
}
private function onKeyUp(event: KeyboardEvent):void
{
vx = 0;
}
private function onFloor(): void
{
if (ball.y > floor)
{
ball.y = floor;
jumping = false;
}
}
}
}
-
M.D.
delete this
Code:
private function onKeyUp(event: KeyboardEvent):void
{
vx = 0;
}
-
Yeah when i delete that then when i press left and right it wont stop moving if i let it go
-
Senior Member
Get rid of the keyup and add this to your enterframe
vx *= .97 (or whatever number feels good)
vy *= .97
-
Senior Member
Just check if you're jumping in the KeyUp function. If you're jumping, don't set vx to 0. Then in the onFloor function set vx to 0, and afterwards it should all go good again.
WIP-ZOMBIES
I love vegetarians! More meat for the rest of us!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|