2011-08-06 68 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

哇,我很高兴看到我的回答对你有用,我希望有一天这个答案对别人有用:) –