将变量传递给事件侦听器

时间:2014-08-17 05:15:32

标签: actionscript-3

我希望能够将变量传递给事件。搜索之后,我发现这是不可能的,很多人只是使用了匿名函数。问题是,我需要传递的变量是唯一的,所以我不能使用匿名函数。不知道我该如何解决这个问题?以下是我的代码顺便说一下的方式:

  • 创建“note”的功能
  • 这张便条好或坏,(独特)
  • 添加移动事件
  • 如果超出舞台,请删除该笔记
  • 根据音符类型减去或添加点数。 (尚未编码)
function SpawnNote(rpos:int):void
{   
    var spawn:int;  
    spawn = int(Math.random() * notes.length);          

rtemp = rpos;

if(rsn%2==1)
{
    if(rtemp==1)
    {
        rpos = int(Math.random() * 2)+2;            
    }

    if(rtemp==2)
    {
        rpos = int(Math.random() * 2);
        if(rpos==0)
        {
            rpos = 3;
        }
    }

    if(rtemp==3)
    {
        rpos = int(Math.random() * 2)+1;                
    }

}


var note:MovieClip = new notes[spawn]();

addChild(note);

if (rpos ==1)
{
    note.x = pos1;

}
else if (rpos==2)
{
    note.x = pos2;

}
else if (rpos==3)
{
    note.x = pos3;

}


note.y = 150;
note.addEventListener(Event.ENTER_FRAME, MoveNote); 
note.addEventListener(MouseEvent.CLICK, CheckNote(spawn));      

}

function MoveNote(event:Event):void
{
    var note:DisplayObject = event.target as DisplayObject;
    note.y +=  15;
    if (note.y >= stage.stageHeight + 50)
    {

        note.visible = true;
        note.removeEventListener( Event.ENTER_FRAME, MoveNote);
        note.removeEventListener(MouseEvent.CLICK, CheckNote);
        this.removeChild(note);     
    }   
}

1 个答案:

答案 0 :(得分:0)

您应该在注释中添加一个字段,表示它是好是坏,并​​将note视为其类或MovieClip,而不只是DisplayObject

note.bad = ... // some expression dependent on "spawn"
function CheckNote(e:Event):void {
    var note:MovieClip=e.target as MovieClip;
    if (note==null) return;
    if (note.bad) ... // process, now you can query if the note is bad.
}
相关问题