在不同对象上多次使用 "originalWidth:int" 和 "originalHeight:int"

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

我正在尝试创建一个 "template",我将在其中 替换 4 张图片,每张图片都不同4 个不同按钮的不同框架上的尺寸。

但每次我更换它们时,尺寸、位置和纵横比都不应该改变。

为此,我找到了这段代码:

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

当我这样做时,它只对一个 button.But 有用:

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;

它会触发以下错误:

PD: 给出的解决方案只保留第一个按钮的原始大小,但不保留其余部分

错误是说您正在尝试创建与您已经在此框架或对象上创建的变量同名的变量。

// 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.

如果您有很多按钮,请考虑将原始按钮尺寸存储在一个数组中。这避免了必须手动创建所有 'original size' 变量:

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