我想访问Object的孩子! 例如: 我们在k1对象中有一个孩子(名字是a1)。 我怎样才能访问它(我的意思是在k1中的a1)
var k1:keypad=new keypad();
k1.x=7.05;
k1.y=229.20;
add Child(k1);
this.k1.a1.addEventListener(Mouse Event.CLICK,ts);
function ts(event:Mouse Event):void{
trace("OK");
};
谢谢。
答案 0 :(得分:0)
事件具有属性目标。这个是被点击的孩子。
function ts(event:MouseEvent):void{
trace(event.target);
};
答案 1 :(得分:-2)
你做错了。您声明了一个局部变量 k1 :
var k1:keypad
不是此对象的成员,您无法访问它
this.k1
所以你的代码可能会这样工作:
// Bad class name: lowercase.
var k1:keypad = new keypad;
k1.x = 7.05;
k1.y = 229.20;
// This is addChild() method, it is spelled without space.
addChild(k1);
// Then you access k1 just as you did above, without this.
// This will work if you designed your keypad with
// Adobe Flash IDE on default publish settings.
k1.a1.addEventListener(Mouse Event.CLICK, onA1);
// This will work too if a1's instance name is a1:
// k1.getChildByName("a1").addEventListener(Mouse Event.CLICK, onA1);
// Event handler. MouseEvent is a class name,
// it has no space inside.
function onA1(e:MouseEvent):void
{
trace("OK");
}
// Normally you don't need ; after function body.