如何拖放动画片段的实例

时间:2014-04-06 11:40:42

标签: actionscript-3

我的库中有一个movieclip(star),我将它的类链接名称设置为“star”。然后我用我的代码在舞台上调用它。我想在同一个位置创建多个星形实例并且都可以拖动但不幸的是我设法创建了两个动画片段的实例,只有一个是可拖动的。我需要一些帮助我码。谢谢。

var stars:Array =  [];
var star:Star = new Star();
this.addChild(star);
stars.push(star);
star.x=550;
star.y=490;

for(var i=0; i<stars.length; ++i)
 {
   trace(stars);
   star.addEventListener(MouseEvent.MOUSE_DOWN, clickToDrag);
   star.addEventListener(MouseEvent.MOUSE_UP, releaseToDrop);

}

function clickToDrag(e:MouseEvent):void
{
    e.target.startDrag();
    var star:Star = new Star();
    this.addChild(star);
    stars.push(star);
    star.x=550;
    star.y=490;
}

function releaseToDrop(e:MouseEvent):void
{
    e.target.stopDrag();
    if (star.hitTestObject(target))

 {
    trace("Collision detected!");
   e.target.removeEventListener(MouseEvent.MOUSE_DOWN, clickToDrag);
    }
 else
  {
    trace("No collision.");
  }
 }

1 个答案:

答案 0 :(得分:0)

您只为一颗星添加听众。将其添加到所有明星,

var totalStars:int = 20;

for(var i=0; i<totalStars; i++)
{       
   var star:Star = new Star();
   this.addChild(star);
   star.x=550;
   star.y=490;

   star.addEventListener(MouseEvent.MOUSE_DOWN, clickToDrag);
   star.addEventListener(MouseEvent.MOUSE_UP, releaseToDrop);

   stars.push(star); 
}