Skip to main content
ajdove
Known Participant
January 2, 2014
Question

How to move up and down a ladder in a platform game

  • January 2, 2014
  • 0 replies
  • 415 views

I am creating a platform game and want to move up and down ladders. I had created a previous post about creating elevators at http://forums.adobe.com/message/5974173#5974173

I cannot get the movement up and down ladders right. What I have so far is calculating the height of the ladder and then move the character vertical using the hero.y coordinates. This is not smooth and very jerky. What I need is to have the character move in a smooth motion up and down the ladder. I am not sure what I am doing wrong?

Here is a screenshot of my ladder. It is 113.3 pixels in height.

Here is the code I have so far:

public function keyDownFunction(event:KeyboardEvent)

                              {

                                        // don't move until in play mode

                                        if (gameMode != "play") return;

 

                                        if (event.keyCode == 38)  // 38 is keycode for up arrow

                                        {

                                                  hero.moveUp = true;

                                        }

 

                                        if (event.keyCode == 40) // 40 is keycode for down arrow

                                        {

                                                  hero.moveDown = true;

                                        }

 

                                        if (event.keyCode == 37) // 37 is keycode for left arrow

                                        {

                                                  //trace("Push Left");

                                                  hero.moveLeft = true;

                                        }

                                        else if (event.keyCode == 39) // 39 is keycode for right arrow

                                        {

                                                  //trace("Push Right");

                                                  hero.moveRight = true;

                                        }

                                        else if (event.keyCode == 32) // 32 is keycode for spacebar

                                        {

                                                  if (!hero.inAir)

                                                  {

                                                            hero.jump = true;

                                                  }

                                        }

                              }

public function checkCollisions()

                              {

                                        for (var i:int = enemies.length-1;i >= 0; i--)

                                        {

                                                  if (hero.mc.hitTestObject(enemies.mc))

                                                  {

                                                            // is the hero jumping down onto the enemy?

                                                            if (hero.inAir && (hero.dy > 0))

                                                            {

                                                                      enemyDie(i);

                                                            }

                                                            else

                                                            {

                                                                      heroDie();

                                                            }

                                                  }

                                        }

 

                                        // items

                                        for (i=otherObjects.length-1; i>=0;i--)

                                        {

                                                  if (hero.mc.hitTestObject(otherObjects))

                                                  {

                                                            getObject(i);

                                                  }

                                        }

 

                                        // pit

                                        for (i=evilObjects.length-1; i>=0; i--)

                                        {

                                                  if (hero.mc.hitTestObject(evilObjects))

                                                  {

                                                            heroDie();

                                                  }

                                        }

                                        // climb ladder

                                        for (i=ladderObjects.length-1; i>=0; i--)

                                        {

                                                  if (hero.mc.hitTestObject(ladderObjects))

                                                  {

                                                            //climbLadder();

                                                            //trace(ladderObjects.y);

                                                            /*

                                                            if (hero.mc.y < ladderObjects.y-1)

                                                            {

                                                                      hero.mc.y -= 1;

                                                            }

                                                            */

                                                            if (hero.moveUp)

                                                            {

                                                                      for (var h:int = 0; h < (ladderObjects.height-1); h++)

                                                                      {

                                                                                hero.mc.y -= 1;

                                                                      }

                                                            }

                                                  }

                                        }

 

                              }

My thought was to use the checkCollisions() function. The code uses the following conditional operations:

1. is the hero touching a ladder?

2. is the player pushing the up arrow on the keyboard? If so, then move the hero.y up the height of the ladder mc.

What can I do to fix this to make the hero move smoothly? Also, I had to move the ladder down slightly so that when the hero is standing on a ledge above the ladder the hero does not touch the ladder mc. I have some coding error that makes the hero not able to jump when touching it. This same inability to jump also happens if the hero is touching an elevator or a spring.

Thanks,

Alex

This topic is closed to new replies. Start a new post to keep the conversation going.