2011-08-06 204 views
1

我有以下問題/ bug:按鈕不起作用

我做了一個自定義按鈕類(稱爲CustomBlitButton),其中定義了一個按鈕。按鈕的翻轉狀態在類CustomScreen中定義。這是因爲自定義屏幕會保存一個或多個按鈕。在使用CustomScreen-class的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.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); 
    } 

} 

} 

這是該按鈕定義代碼:

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

你的代碼很凌亂,但要指出的一些小問題,我發現,也許會讓你的代碼工作:

首先,當你在CustomScreen的單個實例創建按鈕,它們鏈接到同一個變量:CustomScreen.custButton

然後,custButton是一個誰公頃ve的聽衆,而不是鏈接的按鈕的引用:creditsButton和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

哇,我很高興看到我的回答對你有用,我希望有一天這個答案對別人有用:) –