Flash adobe flex中对子对象的函数调用
我已经用AdobeFlex中名为“addButton()”的函数生成了父(标签)对象及其子对象(按钮)。现在我想在父对象上调用名为“lable()”的函数,在子对象上调用名为“btn()”的函数。但每次单击我的脚本都会调用相同的函数“lable()”Flash adobe flex中对子对象的函数调用,flash,apache-flex,actionscript,Flash,Apache Flex,Actionscript,我已经用AdobeFlex中名为“addButton()”的函数生成了父(标签)对象及其子对象(按钮)。现在我想在父对象上调用名为“lable()”的函数,在子对象上调用名为“btn()”的函数。但每次单击我的脚本都会调用相同的函数“lable()” <fx:Script> <![CDATA[ import mx.graphics.SolidColor; import spark.components.Button; import spark
<fx:Script>
<![CDATA[
import mx.graphics.SolidColor;
import spark.components.Button;
import spark.components.Group;
import spark.primitives.Rect;
public function addButton():void {
//Child Object
var myButton:Button = new Button();
myButton.id = "dd";
myButton.label="X";
myButton.width = 40;
myButton.height = 20;
myButton.depth =1;
myButton.x=50;
myButton.setStyle("color",'red');
myButton.addEventListener(MouseEvent.CLICK, btn);
//Parent Object
var lble:Group = new Group();
var solidColor:SolidColor = new SolidColor(0xFF0000);
var rec:Rect = new Rect();
rec.fill = solidColor;
rec.percentWidth = 100;
rec.percentHeight = 100;
lble.width = 127;
lble.height = 34;
lble.depth =0;
lble.addElement(rec);
lble.addEventListener(MouseEvent.CLICK, lable);
lble.addElement(myButton);
myGroup.addElement(lble);
}
private function btn(e:Event):void {
e.stopPropagation();
jj.text = 'Text For Button';
}
private function lable(e:Event):void {
kk.text = "Text For Label";
}
]]>
</fx:Script>
<s:HGroup id="myGroup">
<s:Button width="126" height="34" click="addButton();" label="Click" skinClass="spark.skins.spark.ButtonSkin"/>
</s:HGroup>
<s:Label id="jj" x="14" y="150" width="1200" height="50" backgroundColor="gray" text="Button"/>
<s:Label id="kk" x="14" y="69" width="1200" height="50" backgroundColor="gray" text="Label"/>
请帮助我由于您的按钮位于标签内,两位听众都被呼叫。只需调用
stopPropagation
:
private function btn(e:Event):void {
e.stopPropagation();
jj.text = 'Text For Button';
}
<fx:Script>
<![CDATA[
import mx.graphics.SolidColor;
import spark.components.Button;
import spark.components.Group;
import spark.primitives.Rect;
public function addButton():void {
//Child Object
var myButton:Button = new Button();
myButton.id = "dd";
myButton.label="X";
myButton.width = 40;
myButton.height = 20;
myButton.depth =1;
myButton.x=50;
myButton.setStyle("color",'red');
myButton.addEventListener(MouseEvent.CLICK, btn);
//Parent Object
var lble:Group = new Group();
var solidColor:SolidColor = new SolidColor(0xFF0000);
var rec:Rect = new Rect();
rec.fill = solidColor;
rec.percentWidth = 100;
rec.percentHeight = 100;
lble.width = 127;
lble.height = 34;
lble.depth =0;
lble.addElement(rec);
lble.addEventListener(MouseEvent.CLICK, lable);
lble.addElement(myButton);
myGroup.addElement(lble);
}
private function btn(e:Event):void {
e.stopPropagation();
jj.text = 'Text For Button';
}
private function lable(e:Event):void {
kk.text = "Text For Label";
}
]]>
</fx:Script>
<s:HGroup id="myGroup">
<s:Button width="126" height="34" click="addButton();" label="Click" skinClass="spark.skins.spark.ButtonSkin"/>
</s:HGroup>
<s:Label id="jj" x="14" y="150" width="1200" height="50" backgroundColor="gray" text="Button"/>
<s:Label id="kk" x="14" y="69" width="1200" height="50" backgroundColor="gray" text="Label"/>
您可以尝试在下面的代码中获得类似的行为,而不是在标签中添加按钮。
<fx:Script>
<![CDATA[
import mx.graphics.SolidColor;
import spark.components.Button;
import spark.components.Group;
import spark.primitives.Rect;
public function addButton():void {
//Child Object
var myButton:Button = new Button();
myButton.id = "dd";
myButton.label="X";
myButton.width = 40;
myButton.height = 20;
myButton.depth =1;
myButton.x=50;
myButton.setStyle("color",'red');
myButton.addEventListener(MouseEvent.CLICK, btn);
//Parent Object
var lble:Group = new Group();
var solidColor:SolidColor = new SolidColor(0xFF0000);
var rec:Rect = new Rect();
rec.fill = solidColor;
rec.percentWidth = 100;
rec.percentHeight = 100;
lble.width = 127;
lble.height = 34;
lble.depth =0;
lble.addElement(rec);
lble.addEventListener(MouseEvent.CLICK, lable);
lble.addElement(myButton);
myGroup.addElement(lble);
}
private function btn(e:Event):void {
e.stopPropagation();
jj.text = 'Text For Button';
}
private function lable(e:Event):void {
kk.text = "Text For Label";
}
]]>
</fx:Script>
<s:HGroup id="myGroup">
<s:Button width="126" height="34" click="addButton();" label="Click" skinClass="spark.skins.spark.ButtonSkin"/>
</s:HGroup>
<s:Label id="jj" x="14" y="150" width="1200" height="50" backgroundColor="gray" text="Button"/>
<s:Label id="kk" x="14" y="69" width="1200" height="50" backgroundColor="gray" text="Label"/>
据我所知,在向组中添加元素时,您需要在标签顶部添加按钮。根据我的经验,我使用下一个为子元素创建主容器,然后将其添加到HGroup。请参见下面我对addButton()方法的实现
<fx:Script>
<![CDATA[
import mx.graphics.SolidColor;
import spark.components.Button;
import spark.components.Group;
import spark.primitives.Rect;
public function addButton():void {
//Child Object
var myButton:Button = new Button();
myButton.id = "dd";
myButton.label="X";
myButton.width = 40;
myButton.height = 20;
myButton.depth =1;
myButton.x=50;
myButton.setStyle("color",'red');
myButton.addEventListener(MouseEvent.CLICK, btn);
//Parent Object
var lble:Group = new Group();
var solidColor:SolidColor = new SolidColor(0xFF0000);
var rec:Rect = new Rect();
rec.fill = solidColor;
rec.percentWidth = 100;
rec.percentHeight = 100;
lble.width = 127;
lble.height = 34;
lble.depth =0;
lble.addElement(rec);
lble.addEventListener(MouseEvent.CLICK, lable);
lble.addElement(myButton);
myGroup.addElement(lble);
}
private function btn(e:Event):void {
e.stopPropagation();
jj.text = 'Text For Button';
}
private function lable(e:Event):void {
kk.text = "Text For Label";
}
]]>
</fx:Script>
<s:HGroup id="myGroup">
<s:Button width="126" height="34" click="addButton();" label="Click" skinClass="spark.skins.spark.ButtonSkin"/>
</s:HGroup>
<s:Label id="jj" x="14" y="150" width="1200" height="50" backgroundColor="gray" text="Button"/>
<s:Label id="kk" x="14" y="69" width="1200" height="50" backgroundColor="gray" text="Label"/>
public function addButton():void
{
//First Child Object
var myButton:Button = new Button();
myButton.id = "dd";
myButton.label = "X";
myButton.width = 20;
myButton.height = 20;
myButton.depth = 1;
myButton.x = 105;
myButton.setStyle("color",'red');
myButton.addEventListener(MouseEvent.CLICK, btn);
//Second Object
var lble:Label = new Label();
lble.width = 127;
lble.height = 34;
lble.depth = 0;
lble.setStyle("backgroundColor",'red');
lble.addEventListener(MouseEvent.CLICK, lable);
//Parent Object
var grp:Group = new Group();
grp.addElement(lble);
grp.addElement(myButton);
myGroup.addElement(grp);
}
谢谢你的回复。我尝试了你的代码,但它不起作用。你在按钮侦听器中添加了
trace()
语句了吗?谢谢你的回复。我找到了另一个解决方案。为什么要使用addChild()
作为火花按钮?您应该对Spark组件使用addElement()
。谢谢您的回复。你的解决方案100%有效。再次感谢。您好,由于某些原因,我需要使用旧版本的adobe flex(flex 4.5与flex 3兼容模式)和“spark.components.Group”在我的项目中不起作用。请告诉我“Group()”对象的其他替代对象,欢迎!您可以使用mx:Box容器-。另外,如果使用Flex3的向后兼容性,则只需要使用MX组件。因为Spark组件在Flex 3.hi中不可用,由于某些原因,我需要使用旧版本的adobe Flex(Flex 4.5与Flex 3兼容模式)和“Spark.components.Group”在我的项目中不起作用。请告诉我“Group()”对象的其他替代项