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:
PD: the solutions given only keps the original size of the first button but not the rest
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);