2012-03-30 1 views
0

저는 Sprite에서 단순히 확장 한 자체 버튼을 만들었습니다. 타이머를 사용하여 3 가지 다른 텍스트 페이드 인 및 페이드 아웃을 표시 할 수 있습니다. 내 드로잉 함수에서 나는 먼저 단추의 배경을 나타내는 Sprite 그래픽 개체로 사각형을 그립니다.AS3가 설정 한 것보다 더 큰 스프라이트를 그립니다 (패딩/여백?)

버튼 폭을 설정하는 기능이 추가되었습니다. 이 속성은 사각형을 그리는 데 사용됩니다. Sprite의 크기가 사각형 그리기 후에 업데이트된다는 것을 알고 있습니다. 하지만 스프라이트의 너비가 내 "setButtonWidth"함수를 통해 설정 한 것보다 더 큰 이유가 있습니다.

이제 간단한 스프라이트를 그리는 graphics.drawRectangle 파트가있는 버튼으로 작동하는 간단한 스프라이트가 있습니다. 와 500px 너비를 말할 수 있습니다. 그러나 그 버튼의 너비를 추적하면 항상 약 10 % 더 길어집니다. 이 10 %는 어디에서 오는 것입니까?

validateNow()를 호출하는 방법에 대해 읽었습니다. 그러나 이것은 레이블 또는 확인란에만 해당됩니다. 어떤 이유로 라이브러리 레이블에 액세스 할 수 없습니다. 이것은 TextField에서 어떻게 든 작동해야합니다. 그러나 어떻게?

// this function is part of MyButtonClass.as file 
function drawButton() 
{ 
    this.graphics.clear(); 
    this.graphics.beginFill(currentBackColor); 
    this.graphics.drawRoundRect(0, 0, int(this.buttonWidth)+20, 30, 10, 10); 
    this.graphics.endFill(); 
} 

// this code is in main action code frame 
// the buttonWidth is set this way 
stage.addEventListener(Event.RESIZE, updateBtn); 
function updateBtn(event:Event) 
{ 
    // access the container in which the buttons are in like ... 
    for(var i = 0; i < buttonContainer.numChildren; i++) { 
    var btn = buttonContainer.getChildAt(i) as MyButtonClass; 
    // MyButtonClass is the class which extends from Sprite drawing my button 
    // and using buttonWidth to draw the backgrounds width. 
    btn.setButtonWidth((stage.stageWidth/2) - 20); 
    // i set half of stageWidth because i have two columns with a list of buttons 
    // after setButtonWidth is called in MyButtonClass, the draw function is called 
    // which does the graphics stuff above. 
    } 
} 

this.buttonWidth는 500으로 설정됩니다. 해당 스프라이트에는 특별한 작업이 없습니다. 추가 할 어린이가 없으며이 그림 만 있습니다. 그러나 이것은 스프라이트의 너비를 550이나 뭔가로 설정합니다.

+0

같은 인스턴스를 생성하고 당신은 테두리/텍스트의 폭을 누락하지 않는 필드/낮은 알파 콘텐츠? – Marty

+0

'this.buttonWidth'가 어떻게 설정되는지주의해야합니까? 이 코드는 너비가'buttonWidth' + 20 픽셀이라는 변수에서 계산된다는 것을 보여 주며, 이것이 여러분이 가지고있는 문제를 일으키는 지 의심 스럽습니다. – Daniel

+0

'buttonWidth' 설정 방법은 무엇입니까? 'buttonWidth'의 타입은 무엇이고'int'를 사용한 타입 캐스팅의 사용은 무엇입니까? – Diode

답변

0

package { 

    import flash.display.Sprite; 

    public class Button extends Sprite { 

     private var _buttonWith: int = 0; 
     private var _buttonHeight: int = 0; 

     public function Button() { 
      // constructor code 
     } 

     public function set buttonWidth(w: int): void { 
      _buttonWith = w; 
      updateButton(); 
     } 

     public function get buttonWidth(): int { 
      return _buttonWith; 
     } 

     public function set buttonHeight(h: int): void { 
      _buttonHeight = h; 
      updateButton(); 
     } 

     public function get buttonHeight(): int { 
      return _buttonHeight; 
     } 

     protected function updateButton() { 
      graphics.clear(); 
      graphics.beginFill(0xff00ff); 
      graphics.drawRoundRect(0, 0, buttonWidth, buttonHeight, 10, 10); 
      graphics.endFill(); 
     } 
    } 
} 

같은 Button을 정의하고 특정 있습니까 버튼 폭이 실제로 500 픽셀 넓은 것을이

var button:Button = new Button(); 
addChild(button); 

button.buttonWidth = 100; 
button.buttonHeight = 30; 
+0

이것은 바로 내 버튼 클래스가 수행하는 작업입니다. 높이 설정을 제외하고는, 나는 단지 문제가 될 정적 높이를 사용하지 않습니다. 하지만 여전히 setButtonWidth를 호출 한 후 버튼 폭은 내가 설정 한 것 이상입니다. buttonWidth 매개 변수와 스프라이트 객체에서 오는 버튼 자체의 폭 매개 변수를 모두 추적했습니다. 너비는 항상 내가 설정 한 buttonWidth보다 큽니다. – NovumCoder

관련 문제