如何跟踪“section1.btnback”被点击?AS3 - 获取按钮实例名称和路径
section1.btnback.addEventListener(MouseEvent.CLICK, getBack)
function getBack(event:MouseEvent):void
{
// trace: "section1.btnback"
}
谢谢。 Uli
如何跟踪“section1.btnback”被点击?AS3 - 获取按钮实例名称和路径
section1.btnback.addEventListener(MouseEvent.CLICK, getBack)
function getBack(event:MouseEvent):void
{
// trace: "section1.btnback"
}
谢谢。 Uli
我不确定你在问什么。如果我的建议不适合你,请原谅。如果你写
trace(event.target)
你会看到按钮实例的完整名称。
我猜你想要这样说: “Button was clicked!”每次按下按钮时进行控制台控制?
trace(“Section1.btnback was clicked!”);
我相信。 (真的不知道你问的什么)中的注释
- 频谱扩展如下─
如果你想在“完整路径”像meddlingwithfire在他的评论中提出的3lionz的答案,那么你可以使用一个我创建的类是为你做的。该类称为DOPath(显示对象路径的缩写)。
Main.as(文档类)::
package
{
import com.example.display.DOPath;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
public class Main extends Sprite
{
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}// end function
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
var container1:Sprite = new Sprite();
container1.name = "container1";
var container2:Sprite = new Sprite();
container2.name = "container2";
var square:Square = new Square();
square.name = "square";
square.addEventListener(MouseEvent.CLICK, onSquareClick);
addChild(container1);
container1.addChild(container2);
container2.addChild(square);
}// end function
private function onSquareClick(e:MouseEvent):void
{
var square:Square = e.target as Square;
var doPath:DOPath = new DOPath(square);
trace(doPath); // output: stage.root1.container1.container2.square
}// end function
}// end package
}// end package
import flash.display.Sprite;
internal class Square extends Sprite
{
public function Square()
{
graphics.beginFill(0xFF0000);
graphics.drawRect(0, 0, 100, 100);
graphics.endFill();
}// end function
}// end class
DOPath.as:
package com.example.display
{
import flash.display.DisplayObject;
import flash.display.DisplayObjectContainer;
import flash.display.Stage;
public class DOPath
{
private var _parents:Vector.<DisplayObjectContainer>;
private var _d:DisplayObject;
public function DOPath(d:DisplayObject):void
{
_d = d;
init();
}// end function
private function init():void
{
_parents = new Vector.<DisplayObjectContainer>;
pushParent(_d);
}// end function
private function pushParent(d:DisplayObject):void
{
if(d.parent)
{
_parents.push(d.parent);
pushParent(d.parent);
}// end if
}// end function
public function toString():String
{
var path:String = _d.name;
for (var i:uint = 0; i < _parents.length; i++)
{
var name:String = (_parents[i] is Stage) ? "stage" : _parents[i].name;
path = name + "." + path;
}// end for
return path;
}// end function
}// end class
}// end package
正在使用它的一个例子,以得到一个显示对象的完整路径以下
您可以使用这样一个很简单的递归函数:
private function getDOname(dob : DisplayObject) : String
{
var result : String = dob.name;
if (dob is UIComponent)
{
result = UIComponent(dob).id;
}
if(dob.parent && dob.parent is DisplayObjectContainer)
{
result = getDOname(dob.parent) + "." + result;
}
return result;
}
你可以通过event.target
的功能和获得被点击的按钮。如果您正在将这些技术用于调试目的,您可以简单地将点击事件添加到事件监听器,或者将事件监听器添加到舞台或SandboxRoot本身,如果您不确定实际点击的是哪个视觉元素。
我正在寻找这样的事情:** trace(event.currentTarget.name)** - 问题是这只给了我“btnback” - 但我需要完整的按钮路径。 – Uli
试试这里:http://www.actionscript.org/forums/showthread.php3?t=142581或这里:http://stackoverflow.com/questions/4680323/help-me-to-find-out-the-对象路径在闪光灯 – 3lionz
你问显示列表为“完整的路径”? – meddlingwithfire