0 Replies Latest reply on Aug 22, 2008 6:54 AM by logeye

    OnEnterFrame losing variables

    logeye Level 1
      I am creating a game for pilots. It is in profile view. 6 conflicting aircraft (traffic) come from the right and 6 from the left. The pilot has to correctly use the autopilot to control the aircraft in the middle and avoid the 12 other aircraft. Initially each of the 12 aircraft has it's own onEnterFrame. I thought this was kinda old school so I decided to learn something new in Flash 7.

      I have dynamically created a dozen movieclips on the stage and loaded graphics of aircraft into the clips. The speed is randomized and added to the clip <eval("T"+i).speed> These aircraft appear correctly and the _x, _y properties corectly position the 12 AC on the stage.

      function populateLeftTraffic() {
      this.createEmptyMovieClip("leftTraffic", this.getNextHighestDepth());
      for (i=1; i<=6; i++) {
      duplicateMovieClip(leftTraffic, "T"+i, this.getNextHighestDepth());
      eval("T"+i).loadMovie("aircraft.swf");
      with (eval("T"+i)) {
      _yscale *= .08;
      _xscale *= .08;
      _y = (45+((i-1)*80));
      _x=random(1000);
      eval("T"+i).speed = random(10)/10;
      }
      }
      }
      Similar code is used for the other 6 aircraft.

      To get them to move, I am using one onEnterFrame as attached below:

      this.onEnterFrame = function() {
      for (i=1; i<=12; i++) {
      with (eval("T"+i)) {
      _x += eval("T"+i).speed;
      }
      trace(eval("T"+i).speed);
      eval("T"+i)._x = eval("T"+i)._x+eval("T"+i).speed;

      }
      };

      The problem: The first time through the for (i=1; i<=12; i++) { loop the <eval("T"+i).speed> indicates correctly, on subsequent runs through (the second frame of the onEnterFrame) the <eval("T"+i).speed> comes up as undefined.

      plus the aircraft 'jump' on screen. I suppose I'm asking too much on the onEnterFrame command.


      My question:
      1) why is the <eval("T"+i).speed> vairable changed to undefined after the first for loop?
      2) is there a more efficient way to create this movement and not make it jumpy?

      Thanks for your help.