Copy link to clipboard
Copied
Hello! I am very new to using flash/actionscript 3 and I have started creating a very simple game, however I am really stuck on getting my character to move while jumping. At the moment she can move and jump but not at the same time. I have tried looking into this but I cannot seem to get anything to work. Again, I am very new so any help would be super appreciated! This is the coding I have so far stripped back to just character movement.
stop();
//gravity 'weight'
var gravity:Number = 10;
//jump 'power'
var jumpPower:Number =10;
//is the player already jumpting?
var isJumping:Boolean = false;
//placement of the ground
var ground:Number = 377 - thief_mc.height;
// listen for key press
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPress);
// listen for key release
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyRelease);
// update stage
stage.addEventListener(Event.ENTER_FRAME, update);
//on key press
function onKeyPress(evt:KeyboardEvent):void {
//if you press left, the character walks left
if(evt.keyCode==Keyboard.LEFT){
thief_mc.gotoAndStop("walkleft");
thief_mc.x -=10;
}
//if you press right, the character walks right
else if(evt.keyCode==Keyboard.RIGHT) {
thief_mc.gotoAndStop("walkright");
thief_mc.x +=10;
isJumping = true;
}
//press spacebar to jump
if(evt.keyCode==Keyboard.SPACE){
if(!isJumping){
jumpPower = 30;
isJumping = true;
}
}
}
//on key release
function onKeyRelease(evt:KeyboardEvent):void{
//if left arrow released face forward
if(evt.keyCode==Keyboard.LEFT){
thief_mc.gotoAndPlay("idle");
}
//if releasing right face forward
else if(evt.keyCode==Keyboard.RIGHT){
thief_mc.gotoAndPlay("idle");
}
}
//calculation on how high thief_mc jumps as well as where it jumps too.
function update(evt:Event):void {
if(isJumping){
thief_mc.y -= jumpPower;
jumpPower -= 2;
}
if(thief_mc.y + gravity < ground)
thief_mc.y += gravity;
else{
thief_mc.y = ground;
isJumping = false;
}
}
If you treat the walking the same way as you d the jumping it should be easier to manage. Something like...
//gravity 'weight'
var gravity:Number = 10;
//jump 'power'
var jumpPower:Number =10;
//is the player already jumpting?
var isJumping:Boolean = false;
//placement of the ground
var ground:Number = 377 - thief_mc.height;
var isWalking:Boolean = false;
var direction:Number = 1;
// listen for key press
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPress);
// listen for key rel
...Copy link to clipboard
Copied
If you treat the walking the same way as you d the jumping it should be easier to manage. Something like...
//gravity 'weight'
var gravity:Number = 10;
//jump 'power'
var jumpPower:Number =10;
//is the player already jumpting?
var isJumping:Boolean = false;
//placement of the ground
var ground:Number = 377 - thief_mc.height;
var isWalking:Boolean = false;
var direction:Number = 1;
// listen for key press
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPress);
// listen for key release
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyRelease);
// update stage
stage.addEventListener(Event.ENTER_FRAME, update);
//on key press
function onKeyPress(evt:KeyboardEvent):void {
//if you press left, the character walks left
if(evt.keyCode==Keyboard.LEFT){
thief_mc.gotoAndStop("walkleft");
direction = -1;
isWalking = true;
}
//if you press right, the character walks right
else if(evt.keyCode==Keyboard.RIGHT) {
thief_mc.gotoAndStop("walkright");
direction = 1;
isWalking = true;
}
//press spacebar to jump
if(evt.keyCode==Keyboard.SPACE){
if(!isJumping){
jumpPower = 30;
isJumping = true;
}
}
}
//on key release
function onKeyRelease(evt:KeyboardEvent):void{
//if left arrow released face forward
if(evt.keyCode==Keyboard.LEFT){
thief_mc.gotoAndPlay("idle");
isWalking = false;
}
//if releasing right face forward
else if(evt.keyCode==Keyboard.RIGHT){
thief_mc.gotoAndPlay("idle");
isWalking = false;
}
}
//calculation on how high thief_mc jumps as well as where it jumps too.
function update(evt:Event):void {
if(isWalking){
thief_mc.x += direction*10;
}
if(isJumping){
thief_mc.y -= jumpPower;
jumpPower -= 2;
}
if(thief_mc.y + gravity < ground)
thief_mc.y += gravity;
else{
thief_mc.y = ground;
isJumping = false;
}
}
Copy link to clipboard
Copied
Thank you. I never thought about doing that! I was just hitting my head against a wall with this. I feel a little bit silly now haha, I appreciate the fix!
Copy link to clipboard
Copied
You're welcome
Find more inspiration, events, and resources on the new Adobe Community
Explore Now