Skip to main content
Amemarson
Participant
May 10, 2015
Question

Side Scrolling help

  • May 10, 2015
  • 1 reply
  • 204 views

Hello, I am currently working on a little game and have come across a problem which has held me up for a while now, My game is a side scrolling game, I have a 4800px wide background image. I have tried changing my coding so that the backgound moves and the character stays in the same place which worked quite well except then that kind of broke her jump. I also followed a few bits of coding I have found online but I didn't understand it enough to fix the positioning of the character and keep the UI in place. I am also looking to make the character stop when she hits the edge of the background so you do not just see white. any help appreciated. Here is my current coding:

stop();

  //initial lives

  var lives:int=3;

  //initial score

  var score:int=0;

  //gravity 'weight'

  var gravity:Number = 10;

  //jump 'power'

  var jumpPower:Number =30;

  //is the player already jumpting?

  var isJumping:Boolean = false;

  //placement of the ground

  var ground:Number = 536 - thief_mc.height;

    //is player walking?

  var isWalking:Boolean = false;

  //direction

  var direction:Number = 1;

  //background scrolling speed

  var xScrollSpeed:int = 1;

  //sky scrolling speed

  var xFarscroll:int = 2;

  //tree scrolling speed

  var xTreescroll:int = 5;

  // 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);

  // show score in score box

  score_txt.text=String(score);

  // show lives in life box

  lives_txt.text=String(lives);

  //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;

  bg_mc.x += xScrollSpeed;

  trees_mc.x += xFarscroll;

  grass_mc.x += xTreescroll;

}

  //if you press right, the character walks right

  else if(evt.keyCode==Keyboard.RIGHT) {

  thief_mc.gotoAndStop("walkright");

  direction = 1;

  isWalking = true;

  bg_mc.x -= xScrollSpeed;

  trees_mc.x -= xFarscroll;

  grass_mc.x -= xTreescroll;

  }

 

  //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;

}

  }

This topic has been closed for replies.

1 reply

kglad
Community Expert
Community Expert
May 10, 2015

remove this from the jumping code:

  if(isWalking){

  thief_mc.x += direction*10;

}

Amemarson
AmemarsonAuthor
Participant
May 10, 2015

I have removed the code and that almost does what I want it too but now when my character jumps she only jumps up and if you continue to press left or right there is no direction in her jump and she does not continue walking, So just kind of walks on the spot.

Thank you for the help

kglad
Community Expert
Community Expert
May 10, 2015

why doesn't your background continue to scroll?  fix that.