我如何为该图片库中的按钮提供翻转效果,并为每个按钮分配单独的标签?

发布于 2024-08-04 11:12:32 字数 1558 浏览 4 评论 0原文

所以我试图让我的图片库中的这些“按钮”具有某种翻转效果(文本改变颜色),也不是说“Linus0”,“Linus1”,“Linus2”,我想为每个按钮分配它的自己的标签。我可以用数组来做到这一点吗?另外 - 我做错了什么,让它认为总共有六张图像?只有 5 个,我只需要 5 个按钮。提前致谢!到目前为止,这是我的代码:

var myPics:Array = new Array ("image0", "image1", "image2", "image3", "image4");

var totalItems: int = myPics.length;



function createLabels () : void {
for (var i:int=0; i<=totalItems; i++){
    var btn_label:TextField = new TextField ();
    this.addChild (btn_label);
    btn_label.name = "image"+i;

    var format:TextFormat = new TextFormat ();
    format.font = "Verdana";
    format.color = 0x000000;
    format.size = 12;


    btn_label.defaultTextFormat = format;

    btn_label.x = 55;
    btn_label.y = 50+ (i*20);

    btn_label.text = "Linus"+i;
}
};

function makeMenu ():void {
for (var i:int=0; i<=totalItems; i++) {
    var myBtn:button = new button;
    this.addChild (myBtn);
    myBtn.name = "image"+i;


    myBtn.x = 55;
    myBtn.y = 50+ (i*20);
    myBtn.addEventListener(MouseEvent.CLICK, btnPress);
    myBtn.addEventListener(MouseEvent.CLICK, btnPress);
trace(i);

    }
};

    createLabels ();
    makeMenu ();

    var myFrame: frame = new frame;
    this.addChild (myFrame);
    myFrame.name = "frame";
    myFrame.x = 200;
    myFrame.y = 70;

    var myLoader:Loader = new Loader ();

    function btnPress (event:MouseEvent):void{
        this.enabled = true;
        myLoader.load (new URLRequest("images/"+event.target.name+".jpg"));
        addChild(myLoader);
        myLoader.x = 201;
        myLoader.y = 71;
    };

So I am trying to make these "Buttons" in my image gallery have some sort of rollover effect (text change color) , also instead of saying "Linus0", "Linus1", "Linus2", I would like to assign each one its own label. Am I able to do this with an array? Also - what am I doing wrong thats making it think there are six images total? There are only 5, and I only need 5 buttons. Thanks in advance! Here is my code so far:

var myPics:Array = new Array ("image0", "image1", "image2", "image3", "image4");

var totalItems: int = myPics.length;



function createLabels () : void {
for (var i:int=0; i<=totalItems; i++){
    var btn_label:TextField = new TextField ();
    this.addChild (btn_label);
    btn_label.name = "image"+i;

    var format:TextFormat = new TextFormat ();
    format.font = "Verdana";
    format.color = 0x000000;
    format.size = 12;


    btn_label.defaultTextFormat = format;

    btn_label.x = 55;
    btn_label.y = 50+ (i*20);

    btn_label.text = "Linus"+i;
}
};

function makeMenu ():void {
for (var i:int=0; i<=totalItems; i++) {
    var myBtn:button = new button;
    this.addChild (myBtn);
    myBtn.name = "image"+i;


    myBtn.x = 55;
    myBtn.y = 50+ (i*20);
    myBtn.addEventListener(MouseEvent.CLICK, btnPress);
    myBtn.addEventListener(MouseEvent.CLICK, btnPress);
trace(i);

    }
};

    createLabels ();
    makeMenu ();

    var myFrame: frame = new frame;
    this.addChild (myFrame);
    myFrame.name = "frame";
    myFrame.x = 200;
    myFrame.y = 70;

    var myLoader:Loader = new Loader ();

    function btnPress (event:MouseEvent):void{
        this.enabled = true;
        myLoader.load (new URLRequest("images/"+event.target.name+".jpg"));
        addChild(myLoader);
        myLoader.x = 201;
        myLoader.y = 71;
    };

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泛滥成性 2024-08-11 11:12:32

你得到 6 而不是 5 的原因是你使用的是 <= 而不是 <。因为“i”从 0 开始,这意味着循环计数 6 次。

至于翻转效果,如果您只想更改文本颜色,那么应该这样做:

btn_label.addEventListener(MouseEvent.MOUSE_OVER, labelOver);


function labelOver(e:MouseEvent):void
{
    e.target.defaultTextFormat.color = 0xFF0000;
    e.target.defaultTextFormat = format;
    btn_label.addEventListener(MouseEvent.MOUSE_OUT, labelOut);
}

function labelOut(e:MouseEvent):void
{
    e.target.defaultTextFormat.color = 0x000000;
    e.target.defaultTextFormat = format;
    btn_label.removeEventListener(MouseEvent.MOUSE_OUT, labelOut);
}

the reason you are getting 6 instead of 5 is that you are use <= rather than <. Because 'i' starts at 0 this means the loop counts 6 times.

As for a rollover effect if you only want to change the text color then this should do:

btn_label.addEventListener(MouseEvent.MOUSE_OVER, labelOver);


function labelOver(e:MouseEvent):void
{
    e.target.defaultTextFormat.color = 0xFF0000;
    e.target.defaultTextFormat = format;
    btn_label.addEventListener(MouseEvent.MOUSE_OUT, labelOut);
}

function labelOut(e:MouseEvent):void
{
    e.target.defaultTextFormat.color = 0x000000;
    e.target.defaultTextFormat = format;
    btn_label.removeEventListener(MouseEvent.MOUSE_OUT, labelOut);
}
输什么也不输骨气 2024-08-11 11:12:32

+1 奥利弗

对此所说的话

另外,我不想说“Linus0”、“Linus1”、“Linus2”,而是为每个标签分配自己的标签。我可以用数组来做到这一点吗?

是的,您可以定义一个包含有效名称的字符串数组,然后用它填充按钮标签。

+1 what oliver said

as for this

also instead of saying "Linus0", "Linus1", "Linus2", I would like to assign each one its own label. Am I able to do this with an array?

yes you can define an array of strings containing valid names and then populate your button labels with that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文