简单的方法让很多按钮共享一个功能? AS3

时间:2016-01-18 16:41:54

标签: actionscript-3 flash

我正在尝试在闪存中做一个简单的点亮pegboard。我已经完成了1 peg的一般逻辑,但总共有2,300个钉子,我不想为每个动画片段添加一个事件监听器。

这是我的代码:

import flash.events.Event;
var my_color:ColorTransform = new ColorTransform();
movieClip_1.addEventListener(MouseEvent.MOUSE_UP, fl_MouseClickHandler);
function fl_MouseClickHandler(event:MouseEvent):void
{

    if (my_color.color == 0)
    {
        my_color.color = 0x0000FF;
        event.target.transform.colorTransform = my_color;
    }
    else if (my_color.color == 255)
    {
        my_color.color = 0x00FF00;
        event.target.transform.colorTransform = my_color;
    }
    else if (my_color.color == 65280)
    {
        my_color.color = 0xFF0000;
        event.target.transform.colorTransform = my_color;
    }
    else if (my_color.color == 16711680)
    {
        my_color.color = 0xFFFFFF;
        event.target.transform.colorTransform = my_color;
    }
    else if (my_color.color == 16777215)
    {
        my_color.color = 0x000000;
        event.target.transform.colorTransform = my_color;
    }
    else
    {
        trace(my_color.color);
    }   
}

[Screenshot of Final Product]

2 个答案:

答案 0 :(得分:2)

以下是实现此目的的三种方法:

  1. 将代码放在peg自己的时间轴上。 (或创建一个类文件,并将其附加到您的peg对象)。这将自动为每个peg实例重复使用相同的代码。只需使用相同的代码,但使用this关键字而不是对影片剪辑的硬引用:

    var my_color:ColorTransform = new ColorTransform();
    this.addEventListener(MouseEvent.MOUSE_UP, fl_MouseClickHandler);
    function fl_MouseClickHandler(event:MouseEvent):void
    {
        //.....
    
  2. 制作一个容器Sprite / MovieClip并让所有钉子成为它的唯一子项。然后遍历该容器的所有子节点并附加侦听器:

    //loop through all children of the container and add an event listener
    var i:int = container.numChildren;
    
    while(i--){
        container.getChildAt(i).addEventListener(....);
    }
    

    这很好,因为您不必为它们提供实例名称,这将非常繁琐。

  3. 将点击监听器附加到所有挂钩的公共父级,并使用事件的target属性来查看点击是否在挂钩上。 假设您右键单击了peg库对象,转到属性并选中“export for actionscript”并将其命名为“MyPeg”,您可以这样做:

    commonParent.addEventListener(MouseEvent.CLICK, parentClick);
    
    function parentClick(e:Event):void {
        if(e.target is MyPeg){
            //it's a PEG, do something
        }
    }
    

    现在,根据你的peg对象的结构,target也可以引用你的peg的一个孩子(而不是peg本身)。如果适用,可以避免这种情况,您可以在挂钩的子节点上禁用鼠标输入。所以在你的peg对象的第一帧,你可以这样:this.mouseChildren = false;

  4. 现在,更好(不那么乏味)也是通过代码实例化你的钉子。所以如前所述,在你的属性中导出你的peg for actionscript,并给它一个类名(我的例子中有“ MyPeg ”)。然后是这些方面的东西:

    var curRow:int = 0;
    var curCol:int = 0;
    
    var totalRows:int = 25;
    var totalCols:int = 92;
    
    var startingY:int = 10;
    var startingX:int = 10;
    
    var padding:int = 2; //gap between pegs
    
    var curPeg:MyPeg;
    
    while(true){
        //create the peg, and add it to the display.
        curPeg = new MyPeg();
        addChild(curPeg);
    
        //add the click listener to this peg
        curPeg.addEventListener(MouseEvent.CLICK, fl_mouseClickHandler);
    
        //assign the position of this peg
        curPeg.x = startingX + (curCol * (curPeg.width + padding));
        curPeg.y = startingY + (curRow * (curPeg.height + padding));
    
        //increment the column
        curCol++;
    
        //check if we've reached the last column in the row
        if(curCol >= totalCols - 1){
            //yes, so reset the column to 0 and increment the row
            curCol = 0;
            curRow++;
    
            //break out of the loop if the curRow exceeds or is equal to the total rows var
            if(curRow >= totalRows) break;
        }
    }
    

    这样您只需修改分配给totalColstotalRows的数字即可更改网格大小 - 无需在FlashPro中繁琐地移动2300个对象。

答案 1 :(得分:0)

一种方法是循环遍历2300钉的父母的所有孩子。

for (var i:int=0; i<numChildren; i++) {
  var clip = getChildAt(i);
  if (clip.name.indexOf('movieClip_')==0) {
     clip.addEventListener((MouseEvent.MOUSE_UP, fl_MouseClickHandler);
  }
}

另一种方法是在整个父剪辑中添加一个处理程序,然后在处理程序中检查并查看所点击的内容是否是其中一个钉子。但是你必须在子剪辑上禁用<?php $key = 'a16byteslongkey!'; $data = base64_decode('LsCH4nvvGPKN67v94Ig9BweQgOk9rtDdK7ZugeJkTS8='); $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB); $iv = substr($data, 0, $iv_size); function aes256_cbc_decrypt($key, $data, $iv) { if(32 !== strlen($key)) $key = hash('SHA256', $key, true); if(16 !== strlen($iv)) $iv = hash('MD5', $iv, true); $data = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $data, MCRYPT_MODE_ECB, $iv); $padding = ord($data[strlen($data) - 1]); return substr($data, 0, -$padding); } $result = aes256_cbc_decrypt($key,$data,$iv); var_dump($result); ?> 才能使其工作。

请注意,您可能希望用switch / case替换那个大的if / then语句,在这种情况下更清晰,更紧凑。