Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How to jump on a spring in a platform game

New Here ,
Jan 02, 2014 Jan 02, 2014

I am creating a platform game in which I have created springs that should make my hero fly upwards like a trampoline. The problem I am having is as follows:

1. When I am touching the spring the spring mc remains in the "down" position and does not return to the "up" position

2. After touching the spring my hero character does not propell into the air

3. When touching the spring I cannot "jump" when pressing the spacebar to jump

Here is a screenshot of my spring and hero. The hero character is standing in the middle of the spring. On top of the ledge above my character I have 2 movie clips of the states of the spring: an up and a down position. The spring I am touching has a timeline with 3 label states: up, down, done

platform_spring.png

public function gameLoop(event:Event)

                              {

                                        // get time difference

                                        if (lastTime == 0) lastTime = getTimer();

                                        var timeDiff:int = getTimer() - lastTime;

                                        lastTime += timeDiff;

 

                                        // only perform tasks if in play mode

                                        if (gameMode == "play")

                                        {

                                                  moveCharacter(hero,timeDiff);

                                                  moveEnemies(timeDiff);

                                                  checkCollisions();

                                                  scrollWithHero();

                                                  createLift();

                                                  moveMyLift();

                                                  //moveMyLift02();

                                        }

                              }

public function moveCharacter(char:Object, timeDiff:Number)

                              {

                                        if (timeDiff < 1) return;

 

                                        // assume the character is pulled down by gravity

                                        var verticalChange:Number = char.dy*timeDiff + timeDiff*gravity;

                                        if (verticalChange > 15.0) verticalChange = 15.0;

                                        char.dy += timeDiff*gravity;

 

                                        // react to changes from key presses

                                        var horizontalChange = 0;

                                        var newAnimState:String = "stand";

                                        var newDirection:int = char.direction;

 

                                        if (char.moveUp)

                                        {

                                                  horizontalChange = 0;

                                                  newAnimState = "climb";

                                                  if (hero.mc.hitTestObject(ladderObjects))

                                                  {

                                                            //for (var i:int = 0; i < fixedObjects.length; i++)

                                                            //for (var xyz:int = 0; xyz < ladderObjects.height; xyz--)

                                                            //{

                                                                      //hero.mc.y -= 1;

                                                                      trace("Hungry");

                                                                      verticalChange = 0;

                                                            //}

                                                  }

                                        }

 

                                        if (char.moveDown)

                                        {

                                                  horizontalChange = 0;

                                                  newAnimState = "climb";

                                        }

 

                                        if (char.moveLeft)

                                        {

                                                  // walk left

                                                  horizontalChange = -char.walkSpeed*timeDiff;

                                                  newAnimState = "walk";

                                                  newDirection = -1;

                                        }

                                        else if (char.moveRight)

                                        {

                                                  // walk right

                                                  horizontalChange = char.walkSpeed*timeDiff;

                                                  newAnimState = "walk";

                                                  newDirection = 1;

                                        }

 

                                        if (char.jump)

                                        {

                                                  // start jump

                                                  char.jump = false;

                                                  char.dy = -char.jumpSpeed;

                                                  verticalChange = -char.jumpSpeed;

                                                  newAnimState = "jump";

                                        }

 

                                        if (hero.springSpeed == true)

                                        {

                                                  if (char.springSpeed)

                                                  {

                                                            // start jump

                                                            char.springSpeed = false;

                                                            char.dy = -char.springSpeed;

                                                            verticalChange = -char.springSpeed;

                                                            newAnimState = "doublejump";

                                                  }

                                        }

                                        // assume no wall hit, hanging in air

                                        char.hitWallRight = false;

                                        char.hitWallLeft = false;

                                        char.inAir = true;

 

                                        // hit ladder

                                        char.hitLadderRight = false;

                                        char.hitLadderLeft = false;

 

                                        // find new vertical position

                                        var newY:Number = char.mc.y + verticalChange;

 

                                        // loop through all fixed objects to see if character has landed

                                        for (var i:int = 0; i < fixedObjects.length; i++)

                                        {

                                                  if ((char.mc.x+char.width/2 > fixedObjects.leftside) && (char.mc.x-char.width/2 < fixedObjects.rightside))

                                                  {

                                                            if ((char.mc.y <= fixedObjects.topside) && (newY > fixedObjects.topside))

                                                            {

                                                                      newY = fixedObjects.topside;

                                                                      char.dy = 0;

                                                                      char.inAir = false;

                                                                      break;

                                                            }

                                                  }

                                        }

 

 

                                        // find new horizontal position

                              var newX:Number = char.mc.x + horizontalChange;

 

                              // loop through all objects to see if character has bumped into a wall

                              for(i=0;i<fixedObjects.length;i++) {

                                        if ((newY > fixedObjects.topside) && (newY-char.height < fixedObjects.bottomside)) {

                                                  if ((char.mc.x-char.width/2 >= fixedObjects.rightside) && (newX-char.width/2 <= fixedObjects.rightside)) {

                                                            newX = fixedObjects.rightside+char.width/2;

                                                            char.hitWallLeft = true;

                                                            break;

                                                  }

                                                  if ((char.mc.x+char.width/2 <= fixedObjects.leftside) && (newX+char.width/2 >= fixedObjects.leftside)) {

                                                            newX = fixedObjects.leftside-char.width/2;

                                                            char.hitWallRight = true;

                                                            break;

                                                  }

                                        }

                              }

 

                              // loop through ladders to see if character has touched a ladder

                              for(i=0;i<ladderObjects.length;i++) {

                                        if ((newY > ladderObjects.topside) && (newY-char.height < ladderObjects.bottomside)) {

                                                  if ((char.mc.x-char.width/2 >= ladderObjects.rightside) && (newX-char.width/2 <= ladderObjects.rightside)) {

                                                            newX = ladderObjects.rightside+char.width/2;

                                                            char.hitLadderLeft = true;

                                                            break;

                                                  }

                                                  if ((char.mc.x+char.width/2 <= ladderObjects.leftside) && (newX+char.width/2 >= ladderObjects.leftside)) {

                                                            newX = ladderObjects.leftside-char.width/2;

                                                            char.hitLadderRight = true;

                                                            break;

                                                  }

                                        }

                              }

                                        // set the position of the character

                                        char.mc.x = newX;

                                        char.mc.y = newY;

 

                                        // set animation state

                                        if (char.inAir)

                                        {

                                                  newAnimState = "jump";

                                        }

 

                                        char.animstate = newAnimState;

 

                                        // move along walk cycle

                                        if (char.animstate == "walk")

                                        {

                                                  char.animstep += timeDiff/60;

                                                  if (char.animstep > char.walkAnimation.length)

                                                  {

                                                            char.animstep = 0;

                                                  }

                                                  char.mc.gotoAndStop(char.walkAnimation[Math.floor(char.animstep)]);

                                        }

                                        // not walking, show standing or jump state

                                        else

                                        {

                                                  char.mc.gotoAndStop(char.animstate);

                                        }

 

                                        // change direction

                                        if (newDirection != char.direction)

                                        {

                                                  char.direction = newDirection;

                                                  char.mc.scaleX = char.direction;

                                        }

                              }

public function spring()

                              {

                                        trace("I sprung");

 

                                        hero.springSpeed = true;

                                                  //hero.dy = -hero.springSpeed;

                                                  //this.verticalChange = -hero.springSpeed;

                                                  //newAnimState = "doublejump";

                                        //SpringAction(parent).gotoAndPlay("down");

                              }

// player collides with objects - not enemies

                              public function getObject(objectNum:int)

                              {

                                        // award points for treasure

                                        if (otherObjects[objectNum] is Treasure)

                                        {

                                                  //var pb:PointBurst = new PointBurst(gamelevel, 100, otherObjects[objectNum].x, otherObjects[objectNum].y);

                                                  gamelevel.removeChild(otherObjects[objectNum]);

                                                  otherObjects.splice(objectNum, 1);

                                                  addScore(200);

                                        }

 

                                        // got the key, add to inventory

                                        else if (otherObjects[objectNum] is Key)

                                        {

                                                  //pb = new PointBurst(gamelevel, "Got Key", otherObjects[objectNum].x, otherObjects[objectNum].y);

                                                  playerObjects.push("Key");

                                                  gamelevel.removeChild(otherObjects[objectNum]);

                                                  otherObjects.splice(objectNum, 1);

                                        }

 

                                        // hit the door, end level if hero has the key

                                        else if (otherObjects[objectNum] is Door)

                                        {

                                                  if (playerObjects.indexOf("Key") == -1) return;

                                                  if (otherObjects[objectNum].currentFrame == 1)

                                                  {

                                                            otherObjects[objectNum].gotoAndPlay("open");

                                                            levelComplete();

                                                  }

                                        }

 

                                        // got the chest, game won

                                        else if (otherObjects[objectNum] is Chest)

                                        {

                                                  otherObjects[objectNum].gotoAndStop("open");

                                                  gameComplete();

                                        }

 

                                        // touched spring

                                        else if (otherObjects[objectNum] is SpringAction)

                                        {

                                                  otherObjects[objectNum].gotoAndPlay("down");

                                                  //hero.y -= 5;

                                                  // start jump

                                                  spring();

 

                                        }

                              }

Thank you,

Alex

TOPICS
ActionScript
319
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
no replies

Have something to add?

Join the conversation