+ -
当前位置:首页 → 问答吧 → 请教大师SimpleButton做了一个例子老报错

请教大师SimpleButton做了一个例子老报错

时间:2010-12-09

来源:互联网

如下代码老报:文件不能有一个以上外部可见的定义:TestBtn,Mybn1
复制内容到剪贴板
代码:
package
{
    import flash.display.Shape;
    import flash.display.SimpleButton;
    import flash.display.Sprite;
    import flash.events.MouseEvent;
    import flash.text.TextField;
    import flash.text.TextFieldType;
    import flash.text.TextFieldAutoSize;
    
    public class TestBtn extends Sprite
    {
        private var btn1:MyBtn1;
        private var btn2:MyBtn2;
        private var input:TextField;
        public function TestBtn()
        {
            initMc();
        }
        private function initMc()
        {
            //btn1
            btn1 = new MyBtn1("myBtn1",150,20,"我的文字有不同的颜色哦");
            btn1.x = 10;
            btn1.y = 10;
            addChild(btn1);
            //btn2
            btn2 = new MyBtn2("myBtn2",150,20,"把我的按钮文字换成:");
            btn2.x = 180;
            btn2.y = 10;
            addChild(btn2);
            btn2.addEventListener(MouseEvent.CLICK,onClickBtn2);
            //input
            input = new TextField();
            input.border = true;
            input.borderColor = 0xff66ff;
            input.type = TextFieldType.INPUT;
            input.x = 340;
            input.y = 10;
            input.width = 150;
            input.height = 20;
            input.text = "在此输入要换成的文字";
            addChild(input);
        }
        public function onClickBtn2(event:MouseEvent):void
        {
            var txt:TextField = btn2.getChildByName("btnTxt");
            txt.text = input.text;
        }
    }
internal class MyBtn1 extends SimpleButton{
    private var upColor:uint = 0x000066;
    private var overColor:uint = 0xffccff;
    private var downColor:uint = 0x660000;
    public function MyBtn1(name:String,w:uint,h:uint,txt:String)
    {
        this.name = name;
        downState = new BtnStatusShape1(downColor,w,h,txt);
        overState = new BtnStatusShape1(overColor,w,h,txt);
        upState = new BtnStatusShape1(upColor,w,h,txt);
        hitTestState = upState;
    }
}
internal class BtnStatusShape1 extends Sprite{
public function BtnStatusShape1(bgColor:uint,w:uint,h:uint,txt:String)
{
    graphics.lineStyle(1,0x000000,0.8);
    graphics.beginFill(bgColor,0.8);
    graphics.drawRoundRect(0,0,w,h,8);
    graphics.endFill();
    addChild(new BtnTxt(w,h,txt,0xffffff - bgColor));
}
}
internal class MyBtn2 extends Sprite{
private var upColor:uint = 0x660000;
private var overColor:uint = 0x333300;
private var downColor:uint = 0x000066;
private var btn:SimpleButton;
public function MyBtn2(name:String,w:uint,h:uint,txt:String)
{
this.name = name;
btn = new SimpleButton();
btn.name = "btn";
btn.downState = new BtnStatusShape2(downColor,w,h);
btn.overState = new BtnStatusShape2(overColor,w,h);
btn.upState = new BtnStatusShape2(upColor,w,h);
btn.hitTestState = btn.upState;
addChild(btn);
addChild(new BtnTxt(w,h,txt,0xffffff));
}
public override function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0,useWeakReference:Boolean = false):void{
btn.addEventListener(type,listener);
}
}
internal class BtnStatusShape2 extends Shape{
public function BtnStatusShape2(bgColor:uint,w:uint,h:uint)
{
graphics.lineStyle(1,0x000000,0.8);
graphics.beginFill(bgColor,0.8);
graphics.drawRoundRect(0,0,w,h,8);
graphics.endFill();
}
}
internal class BtnTxt extends TextField{
public function BtnTxt(w:uint,h:uint,txt:String,bgColor:uint)
{
name = "btnTxt";
width = w;
height = h;
textColor = bgColor;
text = txt;
autoSize = TextFieldAutoSize.CENTER;
selectable = false;
mouseEnabled = false;
mouseWheelEnabled = false;
}
}
}

作者: gaowenzhen   发布时间: 2010-12-09

package內不能寫多個類定義。你寫成包外類吧。

作者: enc0717   发布时间: 2010-12-09