Actionscript 3 按钮不起作用
我有以下问题/错误: 我创建了一个自定义按钮类(称为CustomBlitButton),其中定义了一个按钮。按钮的翻滚状态在CustomScreen类中定义。这是因为自定义屏幕包含一个或多个按钮。我可以在使用CustomScreen类的createButton函数时创建一个按钮 因此,如果我想制作一个带有菜单的屏幕,即几个按钮,我会这样做(这只是一个摘录): 但它不起作用!:(屏幕上出现多个按钮,每个按钮都有自己的eventListener,但当我将鼠标移到最后一个按钮上时,只有我创建的最后一个按钮会更改其颜色。当鼠标移到其他按钮上时,所有其他按钮都不会更改其颜色 请问,有人能帮我解决这个问题吗?非常感谢。:) 这是定义自定义按钮滚动状态的自定义屏幕的代码Actionscript 3 按钮不起作用,actionscript-3,Actionscript 3,我有以下问题/错误: 我创建了一个自定义按钮类(称为CustomBlitButton),其中定义了一个按钮。按钮的翻滚状态在CustomScreen类中定义。这是因为自定义屏幕包含一个或多个按钮。我可以在使用CustomScreen类的createButton函数时创建一个按钮 因此,如果我想制作一个带有菜单的屏幕,即几个按钮,我会这样做(这只是一个摘录): 但它不起作用!:(屏幕上出现多个按钮,每个按钮都有自己的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;
}
}
}
}
您的代码非常混乱,但我想指出一些我发现的可能会使您的代码正常工作的小故障: 首先,在CustomScreen的单个实例中创建按钮时,它们链接到同一个变量:CustomScreen.custButton 然后,custButton是具有侦听器的按钮,而不是链接按钮的引用: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类,它就会自动设置自己的侦听器,请参见扩展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;
}
}
}
正如您所看到的,这个类没有定义/创建它的侦听器,因为已经有了它,然后您可以创建自定义事件,例如使用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);
...
}
}
请创建接口,它将使您的代码更加有序
再见
PD:我会说西班牙语,所以,如果英语不好,很抱歉您的代码非常混乱,但我想指出一些我发现的可能会使您的代码正常工作的小故障: 首先,在CustomScreen的单个实例中创建按钮时,它们链接到同一个变量:CustomScreen.custButton 然后,custButton是具有侦听器的按钮,而不是链接按钮的引用: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类,它就会自动设置自己的侦听器,请参见扩展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;
}
}
}
正如您所看到的,这个类没有定义/创建它的侦听器,因为已经有了它,然后您可以创建自定义事件,例如使用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);
...
}
}
请创建接口,它将使您的代码更加有序
再见
PD:我会说西班牙语,所以,如果英语不好,我很抱歉我怀疑这里是否有人愿意检查您的代码并调试它。你需要做作业:)。你可能无法找到问题,但你应该能够缩小范围。用一个更简单的配置做一些测试,得到一个工作的基本情况,添加到它,直到它崩溃。希望这有帮助!信不信由你,但有些人会帮忙,即使代码很多。这不是很多代码。有些人抱怨你几乎没有发布代码,有些人则抱怨“太多”。这总是老一套,我会自己解决这个问题。无论如何,谢谢。这里没有人抱怨,这只是一条建议,看起来你的问题没有太多答案。。。在编写代码时,学习调试是必不可少的。如果你能缩小你的问题范围,你的问题会得到更好的答案。我怀疑这里的任何人都不会愿意检查你的代码并调试它。你需要做作业:)。你可能无法找到问题,但你应该能够缩小范围。用一个更简单的配置做一些测试,得到一个工作的基本情况,添加到它,直到它崩溃。希望这有帮助!信不信由你,但有些人会帮忙,即使代码很多。这不是很多代码。有些人抱怨你几乎没有发布代码,有些人则抱怨“太多”。这总是老一套,我会自己解决这个问题。无论如何,谢谢。这里没有人抱怨,这只是一条建议,看起来你的问题没有太多答案。。。在编写代码时,学习调试是必不可少的。如果你能缩小你的问题范围,你的问题会得到更好的答案。哇!非常感谢你的帮助!它正在工作!:)谢谢,伙计!你救了我的命!哇,我很高兴看到我的答案对你有用,我希望有一天这个答案会对其他人有用:)哇!非常感谢。