单击actionscript上的变量

时间:2013-03-04 14:54:14

标签: actionscript-3 actionscript

我想知道如何使用actionscript更改点击变量。

我有:

     private var test:int   = 0;

     public function thisIsTest():void{
         test = test + 1;
        }

     <mx:Image left="10" bottom="10" source="@Embed(source='Assets/blabla.png')" click="thisIsTest()" buttonMode="true"/>

每次点击'blabla'按钮,我想在变量测试中加1。

问题是它只能运作一次。

感谢您的帮助

1 个答案:

答案 0 :(得分:2)

最简单的方法是使用MouseEvent侦听器。您将侦听器附加到您想要单击的任何内容,并告诉侦听器在触发事件时要执行哪个函数:

var test:int = 0;

image.addEventListener(MouseEvent.CLICK, thisIsTest);
// Will 'listen' for mouse clicks on image and execute thisIsTest when a click happens

public function thisIsTest(e:MouseEvent):void
{
    test = test + 1;
    trace(test);
}

// Output on subsequent clicks
// 1
// 2
// 3
// 4

这意味着您要附加侦听器的图像需要是一个显示对象,如精灵或动画片段,但如果您使用Flash,这应该不是问题。

编辑:评论中注明了进一步的行动。

将图像导入Flash并使用它生成SpriteMovieclip并为其提供一个Actionscript链接ID(如类名):

Import image into library

// Add the image to the stage
var img:myImage = new myImage();
addChild(img);

// Assign the mouse event listener to the image
img.addEventListener(MouseEvent.CLICK, thisIsTest);