• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 181
  • Last Modified:

AS2.0 can't trace attribute

HELLO this is the inside of a class:  My question is, how come I can't trace target_mc from inside of the moveMe function (comes up as undefined) even though I can trace it anywhere else and its fine?      

// *** VARIABLES
      private var target_mc:MovieClip;
      private var moveAmount:Number;
      private var mask_mc:MovieClip;
      private var maskDepth:Number;
      private var imageSpacing:Number;
      private var maskWidth:Number;
      private var right_arrow:MovieClip;
      private var left_arrow:MovieClip;
      private var button_mc:MovieClip;
      private var maskHeight:Number;
      private var movePixels:Number;

      // *** CLASS PROPERTIES
      static var RIGHT_ARROW_DEPTH:Number = 1;
      static var LEFT_ARROW_DEPTH:Number = 2;
      static var BUTTON_DEPTH_ADD:Number = 1;
      static var MOVE_SPEED:Number = 25;
      
      // *** CONSTRUCTOR
      public function MoveStrip(target:MovieClip,
                              x:Number,
                              y:Number,
                              h:Number,
                              thumbWidth:Number,
                              moveSpaces:Number,
                              depth:Number,
                              borderWidth:Number) {
            // assign values
            maskDepth = depth;
            target_mc = target;
            imageSpacing = borderWidth;
            maskWidth = (thumbWidth+borderWidth)*moveSpaces + borderWidth;
            maskHeight = h;
            movePixels = thumbWidth * moveSpaces;
            // functions
            createStripMask(maskWidth, h);
            attachButtons(target_mc);
            
      
      }
      
      private function attachButtons(target:MovieClip):Void {
            var buttonDepth = maskDepth + BUTTON_DEPTH_ADD;
            button_mc = target._parent.createEmptyMovieClip("button_mc",buttonDepth);
            
            right_arrow = button_mc.attachMovie("right_arrow", "right_arrow" + RIGHT_ARROW_DEPTH, RIGHT_ARROW_DEPTH);
            left_arrow = button_mc.attachMovie("left_arrow", "left_arrow" + LEFT_ARROW_DEPTH, LEFT_ARROW_DEPTH);
            
            // align buttons
            right_arrow._x = maskWidth-right_arrow._width;
            right_arrow._y = maskHeight;
            left_arrow._y = maskHeight;
            
            // arrow assignments
            right_arrow.onPress = function() {
                  moveMe();
            }
            left_arrow.onPress = function() {
                  moveMe();
            }
            
            
      
       }

      // actual move function
      public function moveMe() {
            trace(target_mc)
      }
      


thanks in advance...
0
grzegorczyk
Asked:
grzegorczyk
  • 5
  • 4
1 Solution
 
negatyveCommented:
Does this class extends MovieClip?
0
 
grzegorczykAuthor Commented:
no it doesn't
0
 
negatyveCommented:
If I use it this way:

this.createEmptyMovieClip("mc", 1);
var test = new MoveStrip(mc, 10, 10, 100, 100, 20, 1);
test.moveMe();

it traces _level0.mc, which is correct...
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
grzegorczykAuthor Commented:
right but target_mc is not created in this class, its passed into it when MoveStrip is created... I can trace it fine in the constructor...
0
 
negatyveCommented:
So how do you creat an instance of this class and pass it that parameter?
0
 
grzegorczykAuthor Commented:
     // create empty movie clip that will contain all other movie clips
      private function createStripHolder (x:Number,y:Number,h:Number,w:Number,bgColor:Number):Void {
            strip_mc = target_mc.createEmptyMovieClip("strip_mc" + rootDepth, rootDepth);
            strip_mc._x = x;
            strip_mc._y = y;
            // draw background
            strip_mc.moveTo(0,0);
            strip_mc.beginFill(bgColor);
            strip_mc.lineTo(w,0);
            strip_mc.lineTo(w,h);
            strip_mc.lineTo(0,h);
            strip_mc.lineTo(0,0);
            strip_mc.endFill();
            }
....


....
var moveStrip:MoveStrip = new MoveStrip(strip_mc, strip_mc._x, strip_mc._y, strip_mc._height,thumbWidth,4,moveStripDepth, stripBorder);
0
 
negatyveCommented:
You are not giving me enough informations. Using these classe:

// ========================================
// MoveStrip Class
// ========================================
class MoveStrip
{
// *** VARIABLES
     private var target_mc:MovieClip;
     private var moveAmount:Number;
     private var mask_mc:MovieClip;
     private var maskDepth:Number;
     private var imageSpacing:Number;
     private var maskWidth:Number;
     private var right_arrow:MovieClip;
     private var left_arrow:MovieClip;
     private var button_mc:MovieClip;
     private var maskHeight:Number;
     private var movePixels:Number;

     // *** CLASS PROPERTIES
     static var RIGHT_ARROW_DEPTH:Number = 1;
     static var LEFT_ARROW_DEPTH:Number = 2;
     static var BUTTON_DEPTH_ADD:Number = 1;
     static var MOVE_SPEED:Number = 25;

     // *** CONSTRUCTOR
     public function MoveStrip(target:MovieClip,
                         x:Number,
                         y:Number,
                         h:Number,
                         thumbWidth:Number,
                         moveSpaces:Number,
                         depth:Number,
                         borderWidth:Number) {
          // assign values
          maskDepth = depth;
          target_mc = target;
          imageSpacing = borderWidth;
          maskWidth = (thumbWidth+borderWidth)*moveSpaces + borderWidth;
          maskHeight = h;
          movePixels = thumbWidth * moveSpaces;
          // functions
          createStripMask(maskWidth, h);
          attachButtons(target_mc);


     }

     private function attachButtons(target:MovieClip):Void {
          var buttonDepth = maskDepth + BUTTON_DEPTH_ADD;
          button_mc = target._parent.createEmptyMovieClip("button_mc",buttonDepth);

          right_arrow = button_mc.attachMovie("right_arrow", "right_arrow" + RIGHT_ARROW_DEPTH, RIGHT_ARROW_DEPTH);
          left_arrow = button_mc.attachMovie("left_arrow", "left_arrow" + LEFT_ARROW_DEPTH, LEFT_ARROW_DEPTH);

          // align buttons
          right_arrow._x = maskWidth-right_arrow._width;
          right_arrow._y = maskHeight;
          left_arrow._y = maskHeight;

          // arrow assignments
          right_arrow.onPress = function() {
               moveMe();
          }
          left_arrow.onPress = function() {
               moveMe();
          }



      }
      function createStripMask()
      {
      }
     // actual move function
     public function moveMe() {
          trace(target_mc)
     }
}
// ========================================


and

// ========================================
// AnotherClass Class
// ========================================
class AnotherClass
{
      // create empty movie clip that will contain all other movie clips
      var rootDepth:Number = 1;
      var strip_mc:MovieClip;
      var target_mc:MovieClip;
      
      function AnotherClass(tmc:MovieClip)
      {
            target_mc = tmc;
            createStripHolder(10, 10, 10, 10, 0xFF0000);
      }
      private function createStripHolder (x:Number,y:Number,h:Number,w:Number,bgColor:Number):Void
      {
            strip_mc = target_mc.createEmptyMovieClip("strip_mc" + rootDepth, rootDepth);
            strip_mc._x = x;
            strip_mc._y = y;
            // draw background
            strip_mc.moveTo(0,0);
            strip_mc.beginFill(bgColor);
            strip_mc.lineTo(w,0);
            strip_mc.lineTo(w,h);
            strip_mc.lineTo(0,h);
            strip_mc.lineTo(0,0);
            strip_mc.endFill();
      }
      public function returnMeStripMc()
      {
            return strip_mc;
      }
}
// ========================================

your code works fine:

// ========================================
// EXAMPLE
// ========================================
this.createEmptyMovieClip("willBeAnotherClassTargetMc", 1);
var anotherClassInstance:AnotherClass = new AnotherClass(willBeAnotherClassTargetMc);
var strip_mc:MovieClip = anotherClassInstance.returnMeStripMc();

var moveStrip:MoveStrip = new MoveStrip(strip_mc, strip_mc._x, strip_mc._y, strip_mc._height,thumbWidth,4,moveStripDepth, stripBorder);
moveStrip.moveMe(); // traces _level0.willBeAnotherClassTargetMc.strip_mc1
0
 
grzegorczykAuthor Commented:
right ok, but I'm trying to activate that function via onPress:

// arrow assignments
            right_arrow.onPress = function() {
                  moveMe();
            }

in the MoveStrip class but it traces undefined...
0
 
negatyveCommented:
ok, then try:

            right_arrow.target = this;
            right_arrow.onPress = function()
            {
                  this.target.moveMe();
            };
            left_arrow.target = this;
            left_arrow.onPress = function()
            {
                  this.target.moveMe();
            };
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now