Search code examples
actionscript-3flashheightwidthmovieclip

Use "originalWidth:int" and "originalHeight:int" several times on different objects


I'm attempting to create a "template" in which I´ll replace 4 images, each one with different sizes on different frames of 4 different buttons.

But each time I replace them the size,nor it's position nor it´s aspect ratio shouldn't be changed.

For that puropose I found this code:

var originalWidth:int = button1.width / button1.scaleX;
var originalHeight:int = button1.height / button1.scaleY;

which was useful for only one button.But when I do this:

var originalWidth:int = button1.width / button1.scaleX;
var originalHeight:int = button1.height / button1.scaleY;

var originalWidth:int = button2.width / button2.scaleX;
var originalHeight:int = button2.height / button2.scaleY;

var originalWidth:int = button3.width / button3.scaleX;
var originalHeight:int = button3.height / button3.scaleY;

var originalWidth:int = button4.width / button4.scaleX;
var originalHeight:int = button4.height / button4.scaleY;

It triggers the following errors:

  • main, Layer 'actions', Frame 1, Line 37 1151: A conflict exists with definition originalWidth in namespace internal.
  • main, Layer 'actions', Frame 1, Line 38 1151: A conflict exists with definition originalHeight in namespace internal.
  • main, Layer 'actions', Frame 1, Line 40 1151: A conflict exists with definition originalWidth in namespace internal.
  • main, Layer 'actions', Frame 1, Line 41 1151: A conflict exists with definition originalHeight in namespace internal.
  • main, Layer 'actions', Frame 1, Line 43 1151: A conflict exists with definition originalWidth in namespace internal.
  • main, Layer 'actions', Frame 1, Line 44 1151: A conflict exists with definition originalHeight in namespace internal.
  • ComponentShim (Compiled Clip), Line 1 5000: The class 'fl.core.ComponentShim' must subclass 'flash.display.MovieClip' since it is linked to a library symbol of that type.
  • ComponentShim (Compiled Clip), Line 1 5000: The class 'fl.controls.UIScrollBar' must subclass 'flash.display.MovieClip' since it is linked to a library symbol of that type.

PD: the solutions given only keps the original size of the first button but not the rest


Solution

  • The error is saying that you're trying to create variables named the same as variables you already created on this frame or object.

    // Variables on an object must have unique names
    var originalWidth0:int = button1.width / button1.scaleX;
    var originalHeight0:int = button1.height / button1.scaleY;
    
    var originalWidth1:int = button2.width / button2.scaleX;
    var originalHeight1:int = button2.height / button2.scaleY;
    
    // etc.
    

    If you have a lot of buttons, consider storing the original button sizes in an array. This avoids having to create all the 'original size' variables manually:

    // Instance name of first button should be button0, followed by button1 etc.
    // This is because we are using an array — and arrays are zero-based (first index is 0).
    
    var numberOfButtons = 4;
    var originalButtonSizes = new Array();
    
    for(var i = 0; i < numberOfButtons; i++)
    {
        var originalSize = new Object();
        var button = this["button" + i];
        originalSize.width = button.width / button.scaleX;
        originalSize.height = button.height / button.scaleY;
        originalButtonSizes[i] = originalSize;
    }
    
    // Get the first button's original size
    trace(originalButtonSizes[0].width);