as3 按钮被点击按钮下方 50 像素

as3 button getting clicked 50 px below button

我有一个问题。我已经缩小到我的按钮 class,如果我点击按钮精灵区域下方 50-100 像素,就会触发 mouse.click 事件侦听器。

有一次我认为可能是 sprite 子项的嵌套问题导致了它,但即使直接将按钮添加到舞台上也有同样的效果。

我从头开始重写了按钮 class,同样的事情发生了。 我不确定这是否会发生,因为我正在用 flex 框架或其他东西编写纯 as3。这是按钮代码的 class:

package org.project.ui 
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.display.Graphics;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
import flash.display.Shape;
import flash.display.SimpleButton;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
import org.project.controller.IController;
import flash.utils.getQualifiedClassName; 
/**
 * 
 * 
 */
public class CustomButton2 extends CustomSprite 
{
    protected var _button:Sprite;
    protected var _buttonFunction:Function = null;
    protected var _label:String = "";

    protected var _textField:TextField;
    protected var _textFormat:TextFormat;
    protected var _font:String = "Arial";
    protected var _fontSize:int = 12;
    protected var _fontColor:uint = 0x000000;

    protected var _buttonWidth:int = 110;
    protected var _buttonHeight:int = 30;

    public function CustomButton2(label:String, id:String, w:int, h:int, buttonFunction:Function = null) 
    {
        super(id, content);
        _label = label;         
        _buttonWidth = w;
        _buttonHeight = h;      
        _button = makeButton(label, w, h, buttonFunction);
        addButtonListeners();
        addChild(_button);
    }


    protected function makeButton(label:String, w:int, h:int, buttonFunction:Function = null):Sprite {
        _button = new Sprite();
        _button = drawButton(_button);
        _button.useHandCursor = true;
        _button.buttonMode = true;
        _button.mouseChildren = false;


        _button = addTextField(label, _button);
        return _button;
    }

    protected function addTextField(label:String, button:Sprite):Sprite {
        _textFormat = new TextFormat(_font, _fontSize, _fontColor, false, null, null, null, null, TextFormatAlign.CENTER);
        _textField = new TextField();
        _textField.defaultTextFormat = _textFormat;
        _textField.name = "textField_" + id;
        _textField.text = _label;
        _textField.selectable = false;
        button.addChild(_textField);

        return button;
    }

    protected function addButtonListeners():void {
        _button.addEventListener(MouseEvent.CLICK, mouseClick);
        _button.addEventListener(MouseEvent.ROLL_OVER, mouseRollover);
        _button.addEventListener(MouseEvent.ROLL_OUT, mouseRollOut);
    }

    protected function mouseClick(e:MouseEvent) {
        e.stopPropagation();
        if (_buttonFunction) {
            _buttonFunction();
        }
        changeButtonDisplay();

        trace("click");

    }

    protected function mouseRollover(e:MouseEvent) {
        changeButtonDisplay(0xFF0000);
    }

    protected function mouseRollOut(e:MouseEvent) {
        changeButtonDisplay(0xFFCC00);
    }

    protected function changeButtonDisplay(u:uint = 0xFF0000, w:int = 110, h:int = 30):void {
        _button = drawButton(_button, w, h, u);
    }


    protected function drawButton(button:Sprite, w:int = 110, h:int = 30, u:uint = 0xFFCC00):Sprite{
        button.graphics.beginFill(u);
        button.graphics.lineStyle(1);
        button.graphics.drawRoundRect(0, 0, w, h, 20);
        button.graphics.endFill();
        button = addTextField(_label, button);
        return button;
    }
}

}

如有任何建议,我们将不胜感激。

很可能您的 TextField 超出了按钮的其余部分。

它看起来不像是您设置文本字段的边界,因此它将是默认值(通常是 100x100 正方形)。

我建议明确设置文本字段的宽度和高度以匹配您的按钮。

_textField.width = _buttonWidth;
_textField.height = _buttonHeight;

或者,如果您希望所有文本无论如何都可见,您可以使用 autoSize 属性 使文本字段边界与实际文本匹配。

_textField.autoSize = TextFieldAutoSize.LEFT;