使用 MouseEvent 获取对象的公共变量

发布于 2024-12-02 18:17:35 字数 2272 浏览 0 评论 0原文

我有一个简单的问题,目前并不容易。这是一个文本字段类,它将文本字段添加到影片剪辑(从根类传递),并在其公共变量中保存(缓冲)一些数据(如 xml 文件名):

package src {
import flash.display.Shape;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
public class menuitem00 extends Shape {
    public var activateFlag:Boolean = false;
    public var name00:String = "";
    public var xml00:String = "";
    public var txt00:TextField = new TextField();

    private var OM:MovieClip;
    private var id00:Number = 0;

    public function menuitem00(n:int ,OE:MovieClip ,xmlf:String):void {
        trace (" Text Field Object with buffer ");
        OM = OE;    id00 = n;   xml00 = xmlf;
    }
    public function init():void{
        // textfield

        txt00.selectable = false;
        txt00.autoSize = TextFieldAutoSize.LEFT;
        txt00.defaultTextFormat = new TextFormat("Impact", 36,0x66AAFF);
        txt00.text = name00;
        txt00.border = true;
        txt00.borderColor = 0x00FF00;
        txt00.sharpness = 100;
        txt00.x = 0;
        txt00.y = (id00 * txt00.height) + 1;
        txt00.z = 0;
        OM.addChild(txt00);
    }
}

} ..现在我使用 for 循环将该类的实例添加到舞台上的影片剪辑中:

for (var i:int = 0; i<Bunker[0]["trans0size"]; i++) {
    menuData[i] = new menuitem00(i, menu_mc, Bunker[0]["transfile0" + i]);// pass i , a movieclip named menu_mc and an xml file name 
    menuData[i].name00 = Bunker[0]["transtitle0" + i]; // pass text 
    menuData[i].activateFlag = true; // set actveFlag to true
    menuData[i].init(); // run init() inside the instance. it adds textfield to the menu_mc and also set name00 as Text for the textField
}

我还添加了用于单击舞台的鼠标事件,

stage.addEventListener(MouseEvent.CLICK, clk, false, 0, true);
public function clk(evt:MouseEvent):void{   //// mouse 
    trace (" Name: " + evt.target.text);
    //trace (evt.target.xml00);
    }

这是我的问题 -->我想通过鼠标单击从舞台上的该实例获取“xml00”var,但是 trace (evt.target.xml00); 不起作用..还有 trace (evt.target. name00); 不起作用。我可以从 txt00 获取 .alpha.text 等所有内容,但不能获取对象中的其他变量。有什么想法吗?

I have a simple problem which is not easy at the moment. this is a text field class which add a text field to a movieclip (passed from root class) and also save(buffer) some data (like xml file name) in it`s public variables:

package src {
import flash.display.Shape;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
public class menuitem00 extends Shape {
    public var activateFlag:Boolean = false;
    public var name00:String = "";
    public var xml00:String = "";
    public var txt00:TextField = new TextField();

    private var OM:MovieClip;
    private var id00:Number = 0;

    public function menuitem00(n:int ,OE:MovieClip ,xmlf:String):void {
        trace (" Text Field Object with buffer ");
        OM = OE;    id00 = n;   xml00 = xmlf;
    }
    public function init():void{
        // textfield

        txt00.selectable = false;
        txt00.autoSize = TextFieldAutoSize.LEFT;
        txt00.defaultTextFormat = new TextFormat("Impact", 36,0x66AAFF);
        txt00.text = name00;
        txt00.border = true;
        txt00.borderColor = 0x00FF00;
        txt00.sharpness = 100;
        txt00.x = 0;
        txt00.y = (id00 * txt00.height) + 1;
        txt00.z = 0;
        OM.addChild(txt00);
    }
}

}
.. now I use a for loop to add instance of that class to a movie clip on stage:

for (var i:int = 0; i<Bunker[0]["trans0size"]; i++) {
    menuData[i] = new menuitem00(i, menu_mc, Bunker[0]["transfile0" + i]);// pass i , a movieclip named menu_mc and an xml file name 
    menuData[i].name00 = Bunker[0]["transtitle0" + i]; // pass text 
    menuData[i].activateFlag = true; // set actveFlag to true
    menuData[i].init(); // run init() inside the instance. it adds textfield to the menu_mc and also set name00 as Text for the textField
}

also I add mouse event for clicking on the stage,

stage.addEventListener(MouseEvent.CLICK, clk, false, 0, true);
public function clk(evt:MouseEvent):void{   //// mouse 
    trace (" Name: " + evt.target.text);
    //trace (evt.target.xml00);
    }

HERE IS MY PROBLEM --> I want to get "xml00" var from that instance on the stage by mouse click but, trace (evt.target.xml00); is not working .. also trace (evt.target.name00); is not working. I can get everything like .alpha or .text from txt00 but not other variables in my object. Any idea ?

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

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

发布评论

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

评论(1

你的他你的她 2024-12-09 18:17:35

不要将点击侦听器添加到 STAGE 中,而是添加到您的对象中。

menuData[i].addEventListener(MouseEvent.CLICK, clk, ...);

并将以下行添加到 menuitem00 类中:

this.mouseChildren = false;

这样您就可以确定 evt.target 是此类的对象,而不是子对象(如文本字段或其他对象)。

编辑

如果您想保留舞台侦听器,请尝试以下操作:

stage.addEventListener(MouseEvent.CLICK, clk, ...);

private function clk (evt:MouseEvent):void
{
  if (evt.currentTarget is menuitem00)
  {
    var item:menuitem00 = evt.currentTarget as menuitem00;
    trace(item.xml00);  // or any other public variable 
  }
}

但仍将 mouseChildren = false; 添加到您的类中。

edit2

使 menuitem00 类成为精灵(并重命名它):

public class MenuItem extends Sprite {
  private var _activateFlag:Boolean;
  private var _xml:String;
  private var _txt:TextField;
  private var _id:Number;

  public function MenuItem (n:int, xmlf:String) {
    trace (" Text Field Object with buffer ");
    _id = n;
    _xml = xmlf;
    _activeFlag = true;

    this.mouseChildren = false;

    // create txt
    _txt = new TextField();
    // do the formating here ...
    this.addChild(_txt);
  }

  public function getXml():String {
    return _xml;
  }
}

在 for 循环中你会这样做:

for (var i:int = 0; i<Bunker[0]["trans0size"]; i++) {
  var item:MenuItem = new MenuItem(i, Bunker[0]["transfile0" + i]);
  item.addEventListener(MouseEvent.CLICK, clk, false, 0, true);
  menu_mc.addChild(item);
  menuData[i] = item;
}

private function clk (evt:MouseEvent):void {
  var item:MenuItem = evt.target as MenuItem;
  if (item != null) {
    trace(item.getXml());  // use a getter and don't access the variable directly
  }
}

并且请重新阅读你的动作脚本(或编程)书籍,你正在做一些你的代码确实很糟糕,你不应该这样做。

don't add the click-listener to the STAGE but to you object.

menuData[i].addEventListener(MouseEvent.CLICK, clk, ...);

and add the following line to your menuitem00 class:

this.mouseChildren = false;

so you can be sure that the evt.target is an object of this class and not a child (like the textfield or something else).

edit

if you want to keep the stage-listener, try this:

stage.addEventListener(MouseEvent.CLICK, clk, ...);

private function clk (evt:MouseEvent):void
{
  if (evt.currentTarget is menuitem00)
  {
    var item:menuitem00 = evt.currentTarget as menuitem00;
    trace(item.xml00);  // or any other public variable 
  }
}

but still add the mouseChildren = false; to your class.

edit2

make the menuitem00 class a sprite (and rename it pls):

public class MenuItem extends Sprite {
  private var _activateFlag:Boolean;
  private var _xml:String;
  private var _txt:TextField;
  private var _id:Number;

  public function MenuItem (n:int, xmlf:String) {
    trace (" Text Field Object with buffer ");
    _id = n;
    _xml = xmlf;
    _activeFlag = true;

    this.mouseChildren = false;

    // create txt
    _txt = new TextField();
    // do the formating here ...
    this.addChild(_txt);
  }

  public function getXml():String {
    return _xml;
  }
}

in the for loop you would do sth like this:

for (var i:int = 0; i<Bunker[0]["trans0size"]; i++) {
  var item:MenuItem = new MenuItem(i, Bunker[0]["transfile0" + i]);
  item.addEventListener(MouseEvent.CLICK, clk, false, 0, true);
  menu_mc.addChild(item);
  menuData[i] = item;
}

private function clk (evt:MouseEvent):void {
  var item:MenuItem = evt.target as MenuItem;
  if (item != null) {
    trace(item.getXml());  // use a getter and don't access the variable directly
  }
}

and pls re-read your actionscript (or programming) books, you're doing some really bad things i your code that you shouldn't.

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