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

How to make a vertical lift or elevator in a platform game

Guest
Dec 13, 2013 Dec 13, 2013

I am creating a side scrolling platform game using the book "Flash Game University" by Gary Rosenzweig.

My protagonist has an instance of "hero".


My current issue is trying to create a lift or elevator to carry the player up or down. The lift is not covered in the book. My thoughts were that the lift constantly moved up and down at a pace determined by a timer. I cannot get my lift to move. I have created a movie clip called "lift" and made it exported for action script. I can make it act similar to a floor movie clip so that I can stand on it, but I cannot get the lift to move on the y axis.


I tried creating a new function called lift() and using a for loop to count between 0 and 100 using a variable "i" and then make my lift move up 1 unit for each loopcycle. It didn't work.


lift.y -= lift.y - 1;


Does anyone have a better solution?


Thanks,

Alex

TOPICS
ActionScript
3.3K
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
Enthusiast ,
Dec 13, 2013 Dec 13, 2013

you probably want to rename your lift function to some other name then your lift instance name and use onEnterFrame to to call your lift function at frame intervals.

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
Guest
Dec 13, 2013 Dec 13, 2013

I added the following code, but it still does not work.

                                        stage.addEventListener(Event.ENTER_FRAME,moveLifts);

public function moveLifts()

                              {

  this.lift.y -= 105;                 

}

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
Enthusiast ,
Dec 13, 2013 Dec 13, 2013

lift.addEventListener(Event.ENTER_FRAME,moveLifts);

function moveLifts(e:Event)

{

          this.y -=  105;

}

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
Guest
Dec 13, 2013 Dec 13, 2013

I added the code you provided and while the game compiled without error, nothing changed with the lift movieclip.

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
LEGEND ,
Dec 13, 2013 Dec 13, 2013

You said in your first post that you were using a timer in your attempt to move the elevator. Can you show us that code? Do want this elevator to move constantly, so that when it gets to the top it starts down, and when it gets to the bottom, it starts back up again? If this is the case then what is the y position for the top and the bottom location?

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
Guest
Dec 14, 2013 Dec 14, 2013

Here is the lift code:

public function createLift()

                              {

                                        lift = new Object();

                                        lift.mc = gamelevel.lift;

 

                                        // reset lift to starting position in level if collision happens

                                        lift.startx = lift.mc.x;

                                        lift.starty = lift.mc.y;

 

                                        var abc:int = 10;

                                        var xyz:int = 20;

 

                                        //if (abc != xyz)

                                        //{

                                                  //if (lift.mc.y == lift.starty)

                                                  //{

                                                            for (var j:int=0; j>=100; j++)

                                                                      {

                                                                                lift.mc.y -= .001;

                                                                                trace(lift.mc.y);

                                                                                //liftObjects.push(liftObject);

                                                                                //liftObjects.push(mc);

 

                                                                      }

                                                            trace("I am at the top");

                                                            /*

                                                            for (var k:int=100; k<=0; k--)

                                                                      {

                                                                                lift.mc.y += .001;

                                                                                trace(lift.mc.y);

                                                                      }

                                                                      trace("I am at the bottom");*/

                                                  //}

                                        //}

                                        lift.mc.y = lift.mc.y + 100;

                              }

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
Guest
Dec 14, 2013 Dec 14, 2013

I have re-written my lift code. Although the lift mc does not move my trace output is telling me that it should be working. I've created a loop. The trace output loops through 100 "going up" and then 100 "going down" and repeating.

public function createLift()

                              {

                                        lift = new Object();

                                        lift.mc = gamelevel.lift;

 

                                        // lift position

                                        //lift.mc.x = 988.95;

                                        //lift.mc.y = 81.95;

 

                                        // reset lift to starting position in level if collision happens

                                        lift.startx = lift.mc.x;

                                        lift.starty = lift.mc.y;

 

                                        var bottom:int = 0;

 

                                        if (bottom == 0)

                                        {

                                                  for (var xyz:int=0; xyz <= 50; xyz++)

                                                  {

                                                            lift.mc.y = lift.mc.y - 1;

                                                            trace("going up");

                                                  }

                                                  bottom = 1;

                                                  //trace("hit me");

                                        }

 

                                        if (bottom == 1)

                                        {

                                                  for (var pdq:int=0; pdq <= 50; pdq++)

                                                  {

                                                            lift.mc.y = lift.mc.y + 1;

                                                            trace("going down");

                                                  }

                                                  bottom = 0;

                                                  //trace("with you best shot");

                                        }

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
LEGEND ,
Dec 15, 2013 Dec 15, 2013

Although I don't understand a lot of what you have here, the fundamental problem is, most likely, with the way that this function is executing. The for loops that you have to move the movieClip up and down are each executing continuously until it has finished. So the result is that the movieClip is "moving", its just that all of the movement happens before the frame updates.

What you really want to have happen is for the movieClip to move one step each time the function is called, not move all of the steps. You also don't want to instantiate the lift object each time you run the function. You only want to do this once. The same goes for gamelevelLift. Put your counting variable and the boolean variable outside of the function so that they hold their values regardless of the function.

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
Guest
Dec 16, 2013 Dec 16, 2013

Ok, this is helpful. Here are my changes:

I moved the following variables into the main constructor:

Private var bottom:int =0;

Private var xyz:int = 0;

Private var pdq:int =0;

Then added inside the startGameLevel() method I placed the creation of the lift = new Object();

In my gameLoop(event:Event) method I added createLift(lift, bottom);

I then modified the createLidt method as follows:

createLift(lift:Object, bottom:int)

{

lift.mc = gameLevel.lift;

if (bottom <= 50)

(

lift.mc.y = lift.mc.y - 1;

bottom += 1;

}

So now when I run the code I am getting an output error stating:

Error#1009 Cannot access a property or method of a null object reference at createLift()

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
LEGEND ,
Dec 16, 2013 Dec 16, 2013

That error is trying to tell you that some object is out of scope in the createLift() function. It could be lift or bottom or lift.mc. You could add a trace() directive to the function to see which one of those things is not understood by the function. Then you can go back through to see where that needs to be repaired.

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
Guru ,
Dec 18, 2013 Dec 18, 2013

"lift.mc" is no valid instance name.

I have created a movie clip called "lift"

the notation you used is reserved for properties.

createLift(lift:Object, bottom:int)

you are running into trouble because while createLift expects an Object you are giving it a MovieClip.

only certain types of object can have certain of properties.

For example while Array, Number, MovieClip all Objects only a MovieClip has an x or y property

If you are going through a tutorial be careful how you change its notation if you don`t know about the caveats.

1st step would now to "allow debugging" in your publishing options

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
Guest
Dec 20, 2013 Dec 20, 2013

Okay, I've been able to make some progress with all the suggestions. Although I have made some changes as shown below, I have a new problem. The good news is now the elevator moves. That is super. But, it will not move up and down and up and down. It only moves in a single direction forever.

What I wanted was for the mylift.mc to move up 10 pixels and then down 10 pixels, etc. in a loop. Unfortunately, the elevator mc just moves right off the screen and does not obey my conditions. I did create a "trace" and within the loop it outputs the correct loop variable integers. The variable bottom outputs the following loop integers:

10

9

8

7

6

5

4

3

2

1

1

2

3

4

5

6

7

8

9

10

it repeats over and over and over. I am happy and sad at the same time. I've gotten the elevator to move but now I can't get it to stop.

I have changed the instance name of my elevator mc from "lift" to "mylift".

I have also added the following variables as global variables: bottom, xyz, pdq, mylifty

private var bottom:int = 1;

                    private var xyz:int=0;

                    private var pdq:int=0;

                    private var mylifty:int;

public function startGameLevel()

                              {

                                        // create characters

                                        createHero();

                                        addEnemies();

 

                                        // examine level and note all objects

                                        examineLevel();

 

                                        // add listeners

                                        this.addEventListener(Event.ENTER_FRAME,gameLoop);

                                        stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownFunction);

                                        stage.addEventListener(KeyboardEvent.KEY_UP, keyUpFunction);

                                        mylift.addEventListener(Event.ENTER_FRAME,moveLifts);

 

                                        // set game state

                                        gameMode = "play";

                                        addScore(0);

                                        showLives();

                              }

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")

                                        {

                                                  //moveLifts();

                                                  moveCharacter(hero,timeDiff);

                                                  moveEnemies(timeDiff);

                                                  checkCollisions();

                                                  scrollWithHero();

                                                  createLift();

                                                  moveLift(bottom, pdq, mylifty);

                                        }

                              }

public function createLift()

                              {

                                        // create lift properties

                                        mylift = new Object();

                                        mylift.mc = gamelevel.mylift;

                                        mylifty = mylift.mc.y;

}

public function moveLift(bottom:int, pdq:int, mylifty:int)

                              {

if (bottom < 10)

                                        {

                                                  for (bottom; bottom <= 10; bottom++)

                                                  {

                                                            mylift.mc.y = mylift.mc.y - 1;

                                                            trace("bottom = " + bottom);

                                                  }

                                                  //bottom = bottom + 1;

                                                  //trace("hit me");

 

                                        }

 

                                        if (bottom > 1)

                                        {

                                                  for (bottom; bottom >=1; bottom--)

                                                  {

                                                            mylift.mc.y = mylift.mc.y + 1;

                                                            trace("bottom = " + bottom);

                                                  }

                                        }

}

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
LEGEND ,
Dec 21, 2013 Dec 21, 2013

Here's an example movie that may help you. This example shows a movieClip moving up and down using two fixed points.

http://www.ddg-designs.com/downloads/verticalRepeat.zip

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
New Here ,
Dec 28, 2013 Dec 28, 2013

[This is Donut Licker, I had to login with a different username.]

Ok, the example Rob gave is great, thank you. When I attempt to integrate it into my code I needed to change a number of things. However, the changes I have made make the operation of the elevator not work as expected. The "problem" happens when my character touches the elevator my hero stops in mid-air. I suppose the error is with the "hitTestObject". Also, when the hero jumps onto the elevator the hero will not travel up and down smoothly with the elevator. If the hero jumps onto the original y coordinate of the elevator and the elevator is not immedately under the hero's feet then the hero will stand on the original y value, hanging in mid-air, until the elevator actually makes contact with the hero's feet. Once the hero is standing on the elevator the hero will wiggle through through the elevator until falling below it while the elevator continues to travel upwards.

In the embeded image the pink square is the elevator and the hero is the vertical, blue rectangle with a smirk.

elevator_glitch.png

Here is my current code:

variables declared in my main constructor:

var topIndex:Number = 20;

var bottomIndex:Number = 160;

var stepDistance:Number = 10;

var movingUp:Boolean = true;

public function createLift()

                              {

                                        box = new Object();

                                        box.mc = gamelevel.box;

 

                              }

public function createLift()

                              {

                                        box = new Object();

                                        box.mc = gamelevel.box;

                              }

                              public function moveMyLift():void

                              {

                                        if (hero.mc.hitTestObject(box.mc))

                                        {

                                                  if (movingUp && box.mc.y > topIndex) {

                                                            box.mc.y -= stepDistance;

                                                            hero.mc.y -= stepDistance;

                                                  } else {

                                                            movingUp = false;

                                                  }

                                                  if(!movingUp && box.mc.y < bottomIndex) {

                                                            box.mc.y += stepDistance;

                                                            if (hero.mc.y <= 200)

                                                            {

                                                            hero.mc.y += stepDistance;

                                                            }

                                                  } else {

                                                            movingUp = true;

                                                  }

                                        }

                              }

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
LEGEND ,
Dec 29, 2013 Dec 29, 2013

You have the createLift() function listed twice. If your hero.mc's registration point is in the same relative position and the registration point of box.mc, say in the middle of the movieClip, then you can get the hero.mc to follow the box.mc by just setting the hero.mc.y property to be the same as the box.mc.y property.

Something like this:

if (movingUp && box.mc.y > topIndex) {

                                                            box.mc.y -= stepDistance;

                                                            hero.mc.y = box.mc.y;

                                                  } else {

                                                            movingUp = false;

                                                  }

                                                  if(!movingUp && box.mc.y < bottomIndex) {

                                                            box.mc.y += stepDistance;

                                                            if (hero.mc.y <= 200)

                                                            {

                                                            hero.mc.y = box.mc.y;

                                                            }

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
New Here ,
Jan 02, 2014 Jan 02, 2014

Rob,

This worked. Here is my current code ...

var topIndex:Number = 20;

                    var bottomIndex:Number = 160;

                    var stepDistance:Number = 10;

                    var movingUp:Boolean = true;

public function createLift()

                              {

                                        box = new Object();

                                        box.mc = gamelevel.box;

                              }

public function moveMyLift():void

                              {

                                        // lift 01

                                        if (movingUp && box.mc.y > topIndex)

                                        {

                                                  box.mc.y -= stepDistance;

                                        }

                                        else

                                        {

                                                  movingUp = false;

                                          }

                                          if (!movingUp && box.mc.y < bottomIndex)

                                        {

                                                  box.mc.y += stepDistance;

                                        }

                                        else

                                        {

                                                  movingUp = true;

                                          }

                                        if (hero.mc.hitTestObject(box.mc))

                                        {

                                                  hero.mc.y = box.mc.y;

                                        }

                              }

My lift works correctly. It moves up and down correctly and when I jump on it I stay moving on it. There are only 3 things that don't make sense.

1. When I am on the elevator I cannot jump. If I am not on the elevator I can jump normally.

2. When I touch any portion of the elevator, my character automatically repositions itself to stand on the elevator. It seems normal, but not something that should happen in my scenario. I only want to "climb" on the elevator if I touch the "top" of the elevator.

3. If I add a second elevator, even with its own function, the timing of the elevator math appears to synch instead of work independently. Why? The two elevators synch their movement and timing. Also the first elevator stutters as the synching of the two elevators match thier movement and then moves in unison with the second elevator. Why are they not operating separately?

Here is the code for the first and second elevator:

private var box:Object;

private var box02:Object;

var topIndex:Number = 20;

var bottomIndex:Number = 160;

var stepDistance:Number = 10;

var movingUp:Boolean = true;

var topIndexLift02:Number = 1;

                    var bottomIndexLift02:Number = 100;

                    var stepDistanceLift02:Number = 4;

public function createLift()

                              {

                                        box = new Object();

                                        box.mc = gamelevel.box;

 

                                        box02 = new Object();

                                        box02.mc = gamelevel.box02;

                              }

public function moveMyLift():void

                              {

                                        // lift 01

                                        if (movingUp && box.mc.y > topIndex)

                                        {

                                                  box.mc.y -= stepDistance;

                                        }

                                        else

                                        {

                                                  movingUp = false;

                                          }

                               

                                          if (!movingUp && box.mc.y < bottomIndex)

                                        {

                                                  box.mc.y += stepDistance;

                                        }

                                        else

                                        {

                                                  movingUp = true;

                                          }

 

                                        if (hero.mc.hitTestObject(box.mc))

                                        {

                                                  hero.mc.y = box.mc.y;

                                        }

                              }

 

                              public function moveMyLift02():void

                              {

                                        // lift02

                                        if (movingUp && box02.mc.y > topIndexLift02)

                                        {

                                                  box02.mc.y -= stepDistanceLift02;

                                        }

                                        else

                                        {

                                                  movingUp = false;

                                          }

                               

                                          if (!movingUp && box02.mc.y < bottomIndexLift02)

                                        {

                                                  box02.mc.y += stepDistanceLift02;

                                        }

                                        else

                                        {

                                                  movingUp = true;

                                          }

 

                                        if (hero.mc.hitTestObject(box02.mc))

                                        {

                                                  hero.mc.y = box02.mc.y;

                                        }

                              }

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

                                        }

                              }

Thank you,

Alex

Message was edited by: ajdove

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
LEGEND ,
Jan 02, 2014 Jan 02, 2014

1. When I am on the elevator I cannot jump. If I am not on the elevator I can jump normally.

You will have to look at the code that allows your charactor to jump. There is a variable somewhere that is changed when the charactor is in the elevator.

2. When I touch any portion of the elevator, my character automatically repositions itself to stand on the elevator. It seems normal, but not something that should happen in my scenario. I only want to "climb" on the elevator if I touch the "top" of the elevator.

I have no idea. Whatever you are doing to get the charactor into the elevator is being called when you touch the elevator, I'm guessing.

3. If I add a second elevator, even with its own function, the timing of the elevator math appears to synch instead of work independently. Why? The two elevators synch their movement and timing. Also the first elevator stutters as the synching of the two elevators match thier movement and then moves in unison with the second elevator. Why are they not operating separately?

Both elevators are run from the the function "gameloop". They both start at the same time and they both have the same movement increment, so they will both run in sync.

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
New Here ,
Jan 03, 2014 Jan 03, 2014
LATEST

In response to #2: I tried to add a new child mc called "foot" to my hero. I created this mc and placed it inside the hero mc. I also gave it an instance name of "foot". I then tried to call the hitTestObject of the elevator only when the foot of the hero collides with the elevator. It is not working but I think it should.

here is my code:

//Inside my constructor I added

private var foot:Object;

//Inside the createHero() function I added the instantiation of the foot mc using

foot = new Object();

foot.mc = gamelevel.foot;

//Inside the moveMyLift() function I changed the hitTestObject using

if (box.mc.hitTestObject(foot._root.mc))

{

hero.mc.y = box.mc. y;

}

Any ideas how to fix this?

Thanks

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