2011-08-06 9 views
1

다음 문제/버그가 있습니다 :버튼이 작동하지 않습니다.

버튼이 정의 된 사용자 정의 버튼 클래스 (CustomBlitButton)를 만들었습니다. 버튼의 롤 오버 상태는 CustomScreen 클래스 내부에서 정의됩니다. 이는 사용자 정의 화면에 하나 이상의 버튼이 있기 때문입니다. CustomScreen 클래스의 createButton 함수를 사용할 때 버튼을 만들 수 있습니다.

그래서 즉, 메뉴, 여러 버튼을 화면을 만들고 싶어 나는 (이것은 단지 발췌 한 것입니다) 그런 다음을 수행하십시오!

private var buttonOff:ButtonOff = new ButtonOff(0, 0); 
private var buttonOver:ButtonOver = new ButtonOver(0, 0); 

private var buttonOff2:ButtonOff = new ButtonOff(0, 0); 
private var buttonOver2:ButtonOver = new ButtonOver(0, 0); 

private var creditsButton:CustomBlitButton; 
private var settingsButton:CustomBlitButton; 


titleScreen = new CustomScreen(FrameWorkStates.STATE_SYSTEM_TITLE,  stageWidth, stageHeight, 0, 0, testPic, 
              false, 0x000000);    

titleScreen.createButton(creditsButton, buttonOff, buttonOver, "Credits", new Point(centerX - 50, stageHeight/2 + 25), 100, 20, 
            screenButtonFormat, 2); 

titleScreen.createButton(settingsButton, buttonOff2, buttonOver2, "Settings", new Point(centerX - 50, stageHeight/2 + 50), 100, 20, 
            screenButtonFormat, 2); 

그러나 그것은 작동하지 않습니다 :(화면에 표시 몇 개의 버튼과 각 버튼에는 고유 한 eventListener가 있지만 마우스로 움직이면 마지막으로 만든 버튼 만 색을 바꿉니다. 다른 버튼은 마우스로 움직일 때 색상이 변하지 않습니다.

, 누군가가이 문제로 나를 도울 수 있습니까? 감사합니다. :)

사용자 정의 단추의 롤오버 상태가있는 사용자 정의 화면이 정의되는 코드입니다.

package com.framework_mod.src 
{ 
import flash.display.*; 
import flash.text.*; 


public class CustomBlitButton extends Sprite 
{ 
    public static const OFF:int = 1; 
    public static const OVER:int = 2; 

    private var offBackGroundBD:BitmapData; 
    private var overBackGroundBD:BitmapData; 

    private var imageBg:BitmapData; 

    private var positionOffset:Number; 
    private var tempText:TextField = new TextField(); 

    private var buttonBackGroundBitmap:Bitmap; 
    private var buttonTextBitmapData:BitmapData; 
    private var buttonTextBitmap:Bitmap; 

    public function CustomBlitButton(imageOff:BitmapData, imageOver:BitmapData, x:Number, y:Number, width:Number, 
            height:Number, text:String, textformat:TextFormat, positionOffset:Number = 0) 
    { 
     this.positionOffset = positionOffset; 
     this.x = x; 
     this.y = y; 


     offBackGroundBD = imageOff; 
     overBackGroundBD = imageOver; 

     buttonBackGroundBitmap = new Bitmap(offBackGroundBD); 

     tempText.embedFonts = true; 
     tempText.blendMode = BlendMode.LAYER; 
     tempText.autoSize = TextFieldAutoSize.LEFT; 
     tempText.defaultTextFormat = textformat; 
     tempText.selectable = false; 
     tempText.setTextFormat(textformat); 
     tempText.text = text; 

     buttonTextBitmapData = new BitmapData(tempText.textWidth + positionOffset, tempText.textHeight 
               + positionOffset,true,0x00000000); 

     buttonTextBitmapData.draw(tempText); 
     buttonTextBitmap = new Bitmap(buttonTextBitmapData); 
     buttonTextBitmap.x = ((buttonBackGroundBitmap.width - int(tempText.textWidth))/2)-positionOffset; 
     buttonTextBitmap.y = ((buttonBackGroundBitmap.height - int(tempText.textHeight))/2)-positionOffset; 

     addChild(buttonBackGroundBitmap); 
     addChild(buttonTextBitmap); 
     this.buttonMode = true; 
     this.mouseChildren = false; 
     this.useHandCursor = true; 
    } 

    public function changeBackGroundColor(typeval:int):void 
    { 
     if (typeval == CustomBlitButton.OFF) 
     { 
      buttonBackGroundBitmap.bitmapData = offBackGroundBD; 
     } 
     else 
     { 
      buttonBackGroundBitmap.bitmapData = overBackGroundBD; 
     } 
    } 


} 

} 
+1

내가 여기에 누군가가 코드 및 디버그 위에 갈 의향이 될 것입니다 의심한다. 숙제를해야합니다 :). 문제를 찾을 수 없지만 범위를 좁힐 수 있어야합니다. 간단한 구성으로 몇 가지 테스트를 수행하고 작동하는 기본 상황을 얻은 다음 중단 될 때까지 추가하십시오. 희망이 도움이됩니다. – PatrickS

+0

믿거 나 말거나, 코드가 많아도 도움이되는 사람도 있습니다. 그리고 이것은 많은 코드가 아닙니다. 어떤 사람들은 당신이 거의 코드를 게시하지 않을 때 불평하고 어떤 사람들은 그것이 "너무 많이"라고 불평을합니다. 항상 오래된 이야기입니다. – drpelz

+0

나는 혼자서 문제를 해결하려고 노력할 것이다. 어쨌든 고마워. – drpelz

답변

1

코드가 매우 지저분하지만, 어쩌면 당신의 코드가 작동 할 것이라는 점을 나는 발견 된 일부 결함을 지적 할 :

package com.framework_mod.src 
{ 
import flash.display.*; 
import flash.events.*; 
import flash.geom.Point; 
import flash.text.*; 



public class CustomScreen extends Sprite { 

    private var displayText:TextField = new TextField(); 
    private var backgroundBitmapData:BitmapData; 
    private var backgroundBitmap:Bitmap; 
    private var okButton:SimpleBlitButton; 
    private var custButton:CustomBlitButton; 
    private var settingsButton:SimpleBlitButton; 
    private var creditsButton:SimpleBlitButton; 
    private var instructionsButton:SimpleBlitButton; 
    private var highscoreButton:SimpleBlitButton; 
    private var levelButton:SimpleBlitButton; 
    private var testButton:CustomBlitButton; 


    private var id:int; 


    public function CustomScreen(id:int, width:Number, height:Number, xPos:int, yPos:int, image:BitmapData, 
           isTransparent:Boolean, color:uint) { 
     this.id = id; 

     backgroundBitmap = new Bitmap(image); 
     this.x = xPos; 
     this.y = yPos; 
     addChild(backgroundBitmap); 
    } 



    public function createDisplayText(text:String, width:Number, location:Point, textFormat:TextFormat):void { 
     displayText.y = location.y; 
     displayText.x = location.x; 
     displayText.width = width; 
     displayText.defaultTextFormat = textFormat; 
     displayText.text = text; 
     displayText.selectable = false; 
     displayText.mouseEnabled = true; 
     displayText.embedFonts = true; 
     displayText.blendMode = BlendMode.LAYER; 
     displayText.alwaysShowSelection = false; 
     addChild(displayText); 
    } 



    public function createButton(button:CustomBlitButton, btnOff:ButtonOff, btnOver:ButtonOver, text:String, location:Point, width:Number, 
            height:Number, textFormat:TextFormat, positionOffset:Number = 0):void { 

     custButton = button; 

     custButton = new CustomBlitButton(btnOff, btnOver, location.x, location.y, width, height, text, 
             textFormat, positionOffset); 
     addChild(custButton); 

     custButton.addEventListener(MouseEvent.MOUSE_OVER, buttonOverListener, false, 0, true); 
     custButton.addEventListener(MouseEvent.MOUSE_OUT, buttonOffListener, false, 0, true); 
     custButton.addEventListener(MouseEvent.CLICK, buttonClickListener, false, 0, true); 


    } 


    public function setDisplayText(text:String):void { 
     displayText.text = text; 
    } 



    public function buttonClickListener(e:MouseEvent):void { 
     dispatchEvent(new CustomEventButtonId(CustomEventButtonId.BUTTON_ID, id)); 
    } 


    private function buttonOverListener(e:MouseEvent):void { 
     custButton.changeBackGroundColor(CustomBlitButton.OVER); 
    } 


    private function buttonOffListener(e:MouseEvent):void { 
     custButton.changeBackGroundColor(CustomBlitButton.OFF); 
    } 

} 

} 

이 버튼은 정의 된 코드입니다

먼저 CustomScreen의 단일 인스턴스에서 단추를 만들면 해당 단추가 동일한 변수에 연결됩니다. CustomScreen.custButton

그런 다음 custButton은 연결된 버튼의 참조가 아니라 청취자를 참조하십시오. 연결된 버튼의 참조가 아니라 청취자를 참조하십시오. creditButton 및 settingsButton

그래서 버튼을 만들 때마다 custButton을 다른 객체에 연결하고 리스너를 연결하며 이전 버튼이 연결되지 않고 청취되지 않습니다. 이벤트에 대한

난 ... 당신이 모든 버튼이 "어머니"클래스에서 파생되어야 겠다는 Button 클래스 느릅 나무를 만드는 데 자신의 청취자와 당신이 좋아하는 마우스 이벤트에 대한 파견 이벤트의 구현해야한다 추천

이것은 내가하는 일입니다.

먼저 버튼 인터페이스를 만듭니다. 다음

package buttons 
{ 
    import flash.events.MouseEvent; 

    /** 
    * ... 
    * @author Joe Cabezas 
    */ 
    public interface IButton 
    { 
     function onClick(e:MouseEvent):void; 
     function onRollOver(e:MouseEvent):void; 
    } 

} 

버튼의 모든 종류에서 파생해야하는 버튼 클래스 느릅 나무 만들 : 쿵푸 어떤 순서를 원하는

package buttons 
{ 
    import flash.display.Sprite; 
    import flash.events.MouseEvent; 
    /** 
    * ... 
    * @author Joe Cabezas 
    */ 
    public class Button extends Sprite implements IButton 
    { 

     public function Button() 
     { 
      this.agregarListeners(); 
     } 

     private function agregarListeners():void 
     { 
      this.addEventListener(MouseEvent.CLICK, onClick); 
      this.addEventListener(MouseEvent.ROLL_OVER, onRollOver); 
     } 

     /* INTERFACE botones.IButton */ 

     public function onClick(e:MouseEvent):void 
     { 

     } 

     public function onRollOver(e:MouseEvent) :void 
     { 

     } 

    } 

} 

다음, 당신은 버튼을 생성 할 때마다, 그냥 버튼 클래스를 확장 그리고 이미 자동으로 자체 리스너를 갖게됩니다. Button 클래스를 확장 한 MenuButton의이 예제를 참조하십시오.

package buttons 
{ 
    import com.as3joelib.generators.TextFieldGenerator; 
    import flash.display.Sprite; 
    import flash.text.TextField; 
    import flash.text.TextFieldAutoSize; 
    import flash.text.TextFormatAlign; 
    /** 
    * ... 
    * @author Joe Cabezas 
    */ 
    public class BotonMenuSuperior extends Button 
    { 
     //constantes de diseño 
     private const padding:Number = 10; 

     private var fondo:Sprite; 
     private var color:uint; 

     private var text_string:String 
     private var text_field:TextField; 

     public function BotonMenuSuperior(text:String, color:uint) 
     { 
      super(); 

      this.text_string = text; 
      this.color = color; 

      this.setup(); 
      this.dibujar(); 
     } 

     private function setup():void 
     { 
      //crear textfield 
      this.text_field = TextFieldGenerator.crearTextField(this.text_string, { 
       //border:true, 
       size:15, 
       embedfonts:false, 
       color:0xffffff 
      }); 
      this.text_field.x = this.padding*0.75; 
      this.text_field.y = this.padding*0.75; 

      //crear fondo 
      this.fondo = new Sprite(); 

      this.fondo.graphics.beginFill(this.color); 
      this.fondo.graphics.drawRect(0, 0, this.text_field.textWidth + 2*this.padding, this.text_field.textHeight + 2*this.padding); 
      this.fondo.graphics.endFill(); 
     } 

     private function dibujar():void 
     { 
      this.addChild(this.fondo); 
      this.addChild(this.text_field); 
     } 


     //overriding functions to create the custom behavior of this button when mouse events happens 
     override public function onClick (e:MouseEvent):void{ 
      //do here whatever you like when user clicks this button, like: 
      this.scaleX = this.scaleY = 1.5; 
     } 


    } 

} 

당신이 볼 수 있듯이,이 클래스는 정의하지 않은/이미 가지고 있기 때문에 생성이 청취자는, 당신은 ...이 이벤트의 버블에 modufy Button 클래스의 사용자 정의 이벤트를 만들 수 있습니다

이것이 당신을 도울 수 있기를 바랍니다!또한

, 팁, 코드에서 :

public class CustomScreen extends Sprite { 

    ... 

    public function createButton(button:CustomBlitButton, btnOff:ButtonOff, btnOver:ButtonOver, text:String, location:Point, width:Number, 
           height:Number, textFormat:TextFormat, positionOffset:Number = 0):void { 

    custButton = button; //<-- this 

    custButton = new CustomBlitButton(btnOff, btnOver, location.x, location.y, width, height, text, 
            textFormat, positionOffset); 
    addChild(custButton); 

    ... 
    } 
} 

는 첫 번째 매개 변수는 코드를 유용 얼마나?

이 같은입니다 :

public funcion(a:Number, b:number):Number{ 
    var xa:Number = a; 

    xa = 2; 

    return xa + b; 
} 

, 그것은

안녕 코드를 더욱 질서 것, 인터페이스를 만드십시오!

PD : 내가 미안 해요, 스페인어, 그렇다면 나쁜 영어

+0

와우! 당신의 도움을 주셔서 대단히 감사합니다! 고마워, 친구! 니가 내 목숨을 구했어! – drpelz

+0

와우, 나는 내 대답을 읽어 주셔서 대단히 유용합니다. 나는 언젠가이 대답이 다른 누군가에게 유용 할 것입니다. :) –

관련 문제