在AS3任何选定的文本框的变化形式? [英] Change format of any selected textfield in AS3?

查看:164
本文介绍了在AS3任何选定的文本框的变化形式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建多个文本框时,点击事件被触发。现在我想改变任何选定的文本框的文本格式。但是,格式只是应用到最后创建的文本框。我试过如下:

 函数_txtbtn(五:*):无效
{
    =会将myText新的TextField();
    MC3 =新的MovieClip();
    myText.text =文字...;
    myFormat.font =宋体;
    myFormat.color = txt_color()
    myText.setTextFormat(myFormat);
    mc3.addChild(会将myText);
    的addChild(MC3);
    mc3.x = _can.x;
    mc3.y = P;
    P = mc3.y + mc3.height + 10;
    mc3.addEventListener(的MouseEvent.MOUSE_DOWN,_select)
}

功能_select(E:MouseEvent)方法:无效
{
    tool_stage.combo.addEventListener(Event.CHANGE,_font)
}

功能_font(五:事件):无效
{
    format.font = tool_stage.combo.selectedLabel;
    myText.setTextFormat(格式);
}
 

解决方案

这是正确的,因为会将myText变量指的是最后一个对象。

取而代之的是

您可以从事件目前的TextField对象。 每个事件都有currentTarget当前值,这是指已被触发事件的对象。 然后,您可以投currentTarget当前你的类型,做的动作吧。

不幸的是,我没有你的整个code,这就是为什么我有我自己的版本。 看看吧,我觉得它可以帮助你。

// Main.as

 包
{
进口的flash.display.MovieClip;
进口flash.display.Sprite;
进口flash.events.MouseEvent;
进口API元素flash.text.TextField;
进口flash.text.TextFormat用于;

公共类主要扩展Sprite
{
    私人VAR键:雪碧;
    私人VAR号码:INT = 50;
    私人VAR X0:INT = 20;

    公共函数main()
    {
        在里面();
    }

    私有函数的init():无效
    {
        按钮=新的Sprite();

        button.graphics.beginFill(0xFFCC00);
        button.graphics.drawRect(0,0,80,20);
        button.graphics.endFill();

        button.addEventListener(MouseEvent.CLICK,onBtnClick);

        this.addChild(按钮);
    }

    私有函数onBtnClick(五:*):无效
    {
        VAR myFormat:的TextFormat =新的TextFormat();

        变种会将myText:文本字段=新的TextField();
        VAR MC3:影片剪辑=新的MovieClip();
        myText.text =文字...;
        myFormat.font =宋体;
        myFormat.color = 0x000000处;
        myText.setTextFormat(myFormat);

        mc3.addChild(会将myText);

        的addChild(MC3);
        mc3.x = X0;
        mc3.y = P;

        P = mc3.y + mc3.height + 10;

        myText.addEventListener(MouseEvent.CLICK,onTextClick)
    }

    私有函数onTextClick(EVT:MouseEvent)方法:无效
    {
        VAR newFormat:的TextFormat =新的TextFormat();
        newFormat.size = 30;
        newFormat.font =宋体;
        (evt.currentTarget为文本字段).setTextFormat(newFormat);
    }
}
}
 

I create several textfields when the click event is fired. Now I want to change the text format of any selected textfield. But the format is just applied to the last created textfield. I tried the following:

function _txtbtn(e:*):void
{
    myText = new TextField();
    mc3 = new MovieClip();
    myText.text = "text...";
    myFormat.font = "Arial";
    myFormat.color = txt_color()
    myText.setTextFormat(myFormat);
    mc3.addChild(myText);
    addChild(mc3);
    mc3.x = _can.x;
    mc3.y = p;
    p= mc3.y+mc3.height+10;
    mc3.addEventListener(MouseEvent.MOUSE_DOWN,_select)
}

function _select(e:MouseEvent):void
{
    tool_stage.combo.addEventListener(Event.CHANGE,_font)
}

function _font(e:Event):void
{
    format.font = tool_stage.combo.selectedLabel;
    myText.setTextFormat(format);
}

解决方案

It is right, because the variable myText refers to the last Object.

Instead of this you can get the current TextField object from the Event. Each event has currentTarget value, which refers to the Object that has fired the Event. You then can cast the currentTarget to your type and do the action with it.

Unfortunately I don't have your whole code, that is why I have my own version. Have a look at it, I think it can help you.

//Main.as

package
{
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFormat;

public class Main extends Sprite
{
    private var button:Sprite;
    private var p:int = 50;
    private var x0:int = 20;

    public function Main()
    {
        init();
    }

    private function init():void
    {
        button = new Sprite();

        button.graphics.beginFill(0xFFCC00);
        button.graphics.drawRect(0, 0, 80, 20);
        button.graphics.endFill();

        button.addEventListener(MouseEvent.CLICK, onBtnClick);

        this.addChild(button);
    }

    private function onBtnClick(e:*):void
    {
        var myFormat:TextFormat = new TextFormat();

        var myText:TextField = new TextField();
        var mc3:MovieClip = new MovieClip();
        myText.text = "text...";
        myFormat.font = "Arial";
        myFormat.color = 0x000000;
        myText.setTextFormat(myFormat);

        mc3.addChild(myText);

        addChild(mc3);
        mc3.x = x0;
        mc3.y = p;

        p= mc3.y+mc3.height+10;

        myText.addEventListener(MouseEvent.CLICK, onTextClick)
    }

    private function onTextClick(evt:MouseEvent):void
    {
        var newFormat:TextFormat = new TextFormat();
        newFormat.size = 30;
        newFormat.font = "Verdana";
        (evt.currentTarget as TextField).setTextFormat(newFormat);
    }
}
}

这篇关于在AS3任何选定的文本框的变化形式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆