我有以下問題/ 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;
}
}
}
}
我懷疑有人在這裏將是願意去在你的代碼和調試。你需要做你的功課:)。您可能無法找到問題,但您應該能夠縮小範圍。 用一個更簡單的配置做一些測試,得到一個正在工作的基本情況,添加到它中斷。 希望這有助於! – PatrickS
不管你信不信,但有些人即使有很多代碼也能幫上忙。這不是很多代碼。有些人抱怨說,當你發佈幾乎沒有代碼時,有些人抱怨說,如果它「太多」。它始終是舊的故事。 – drpelz
我會嘗試自己解決問題。不管怎麼說,還是要謝謝你。 – drpelz