2013-08-19 52 views
2

我正在嘗試使用按鈕編輯主菜單。我有一個名爲MenuAchievementsButton的類,在它的構造函數中,它從文檔類傳遞到舞臺寬度和舞臺高度。當我跟蹤這個時,它是正確的寬度/高度。as3 MovieClip不能正確縮放

我試圖讓按鈕的高度爲舞臺高度/ 100.我試過做這個.height =舞臺高度/ 100,但是每當我這樣做,它給我4倍的結果我想要的,即使當我輸入像5這樣的數字並在Photoshop中測量像素。

此外,當我嘗試移動該對象時,存在類似的問題:當我希望它是20px的正確和下來,結果是80px左右。

任何幫助表示讚賞!

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! 
    } 
} 

}

回答

0

的代碼似乎是正確的。那裏沒有問題。

這個類的實例是在舞臺上添加的嗎?或在其他物體上?

見,如果你不改變父母的規模,這樣的事情:

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 
+0

當我嘗試做的scaleX = 1,它使更大!我檢查過,物體不在舞臺上或另一個物體上...任何想法? – DefinitelyNotAPlesiosaur

+1

我找到了解決方案 - 我從主菜單背景創建了按鈕,這些按鈕已被縮放。謝謝您的幫助! :D – DefinitelyNotAPlesiosaur

+1

@ user2696186是的,縮放比例總是相對於父代,而不是全局。 – Vesper