2

I'm trying to program a main menu with buttons in it. I have a class called MenuAchievementsButton, and in its constructor, it is passed down the stage width and stage height from the document class. When I trace this, it is the correct width/ height.

I'm trying to get the button's height to be the stage height / 100. I've tried doing this.height = stage height / 100, but whenever I do so, it gives me 4x the result I want, even when I enter in a number like 5 and measure the px in Photoshop.

Also, when I try to move the object, a similar problem exists: When I want it to be, say, 20px right and down, it turns out to be 80px left and down.

Any help is appreciated!

    package menus {

import flash.display.MovieClip;


public class MenuAchievementsButton extends MovieClip {
    private var _stageWidth, _stageHeight:int;

    public function MenuAchievementsButton(stageWidth:int, stageHeight:int) {
        _stageWidth = stageWidth;
        _stageHeight = stageHeight;

        alpha = 1;
        rescaleMe();
        repositionMe();
        trace(_stageWidth, _stageHeight);
    }

    private function rescaleMe():void {
        var oldWidth = this.width;
        var oldHeight = this.height;

        this.height = _stageHeight/100;
        this.width = (this.height * oldWidth) / oldHeight;
    }

    private function repositionMe():void {
        this.x = 20;
        this.y = 20;
        trace(x,y,width,height);
        trace("Stage height is: ",_stageHeight,"Stage height divided by 100 is: ", _stageHeight/100, "And the object's height, which should be stageheight / 100 is:", this.height);
        //Gives Stage height is:  800 Stage height divided by 100 is:  8 And the object's height, which should be stageheight / 100 is: 8
        //Actually, though, it's 36px!
    }
}

}

1 Answers1

0

The code seems correct. No issues there.

Is the instance of the class added on stage? or on some other object?

See if you don't change the scale of a parent, something like this:

var parentMC:MovieClip=new MovieClip();
addChild(parentMC);

var rectangle:Shape = new Shape; // initializing the variable named rectangle
rectangle.graphics.beginFill(0xFF0000); // choosing the colour for the fill, here it is red
rectangle.graphics.drawRect(0, 0, 100,100); // (x spacing, y spacing, width, height)
rectangle.graphics.endFill(); // not always needed but I like to put it in to end the fill
parentMC.addChild(rectangle); // adds the rectangle to the stage

trace(rectangle.width) // result 100

parentMC.scaleX=2;
trace(rectangle.width) // result 100 altough it looks like it has 200
moKS
  • 41
  • 5