Hey,
I’m playing around with a platform game. I had all the basic controls almost done when my monkey decided he didn’t want to jump anymore. Basically nothing happens when I now press UP. It doesn’t even go to the jumping-animation frame inside the player_mc.
I think something is saying he can’t jump, so the “canJump” thing doesn’t work.
Was following this tutorial: [URL=“http://www.youtube.com/watch?v=B8FLQVODzgcGreat”]http://www.youtube.com/watch?v=B8FLQVODzgc
Great if anyone wants to look at this. Here’s a picture for inspiration:
Anyway, here is the code I’ve got down:
import flash.events.KeyboardEvent;
var KeyThatIsPressed:uint;
var RightKeyIsDown:Boolean = false;
var LeftKeyIsDown:Boolean = false;
var UpKeyIsDown:Boolean = false;
var DownKeyIsDown:Boolean = false;
var playerSpeed:Number = 12;
var gravity:Number = 2;
var yVelocity:Number = 0;
var canJump:Boolean = false;
stage.addEventListener(KeyboardEvent.KEY_DOWN, PressAKey);
stage.addEventListener(KeyboardEvent.KEY_UP, ReleaseAKey);
// PressAKey function here
function PressAKey(event:KeyboardEvent):void
{
if(event.keyCode == Keyboard.RIGHT)
{
RightKeyIsDown = true;
}
if(event.keyCode == Keyboard.LEFT)
{
LeftKeyIsDown = true;
}
if(event.keyCode == Keyboard.UP)
{
UpKeyIsDown = true;
}
if(event.keyCode == Keyboard.DOWN)
{
DownKeyIsDown = true;
}
}
// ReleaseAKey function here
function ReleaseAKey(event:KeyboardEvent):void
{
{
RightKeyIsDown = false;
}
if(event.keyCode == Keyboard.LEFT)
{
LeftKeyIsDown = false;
}
if(event.keyCode == Keyboard.UP)
{
UpKeyIsDown = false;
}
if(event.keyCode == Keyboard.DOWN)
{
DownKeyIsDown = false;
}
}
player_mc.addEventListener(Event.ENTER_FRAME, moveThePlayer);
function moveThePlayer(event:Event):void
{
if(RightKeyIsDown)
{
player_mc.x += playerSpeed;
player_mc.scaleX = 1;
player_mc.gotoAndStop(2);
}
if(LeftKeyIsDown)
{
player_mc.x -= playerSpeed;
player_mc.scaleX = -1;
player_mc.gotoAndStop(2);
}
if(UpKeyIsDown && canJump)
{
yVelocity = -30;
canJump = false;
player_mc.gotoAndStop(3);
}
if(!RightKeyIsDown && !LeftKeyIsDown && !UpKeyIsDown)
{
player_mc.gotoAndStop(1);
}
}
yVelocity += gravity;
if(! floor_mc.hitTestPoint(player_mc.x, player_mc.y, true))
{
player_mc.y += yVelocity;
}
if(yVelocity > 20)
{
yVelocity = 20;
}
for (var i:int = 0; i<10; i++)
{
if (floor_mc.hitTestPoint(player_mc.x, player_mc.y, true))
{
player_mc.y--;
yVelocity = 0;
canJump = true;
}
}