2014-01-12 23 views
1

我正在尝试创建一个类似Tap Tap的游戏,除了下落的物体被随机化之外。我现在遇到的问题是如果随机对象再次被选中(在它移动之前),它的位置将被重置(这也意味着两个相同的对象不会出现在我不想要的屏幕中)我想出了一个办法解决这个问题,我应该能够创建一个摘录的动画片段的副本,但我迷路了。帮帮我?生成下降的随机对象在阵列中进行选择

此外,我是新来的闪存。如果您有建议/建议,请告诉我! 谢谢你,祝你有美好的一天。

var notes:Array = new Array(NGood1,NGood2,NGood3,NGood4,NGood5,NBad1,NBad2,NBad3,NBad4,NBad5); 

var pos1:int; 
var pos2:int; 
var pos3:int; 

pos1 = (stage.stageWidth/3) -100; 
pos2 = (stage.stageWidth/2) -100; 
pos3 = ((stage.stageWidth/3) *2) -100; 

var timerN:Timer = new Timer(1000,120); 
timerN.addEventListener(TimerEvent.TIMER, timerhandler); 
timerN.start(); 

var secondsN:Number = 1; 

function timerhandler(event:TimerEvent) 
{ 
    //trace("Seconds elapsed: " + seconds); 
    SpawnNote(null); 
    secondsN++; 

} 


function SpawnNote(event:Event):void 
{ 
    var spawn:int; 
    var rpos:int; 
    spawn = int(Math.random() * notes.length); 
    rpos = int(Math.random() * 3) + 1; 
    var note:MovieClip = new MovieClip(); 
    note = 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 = -20; 
    note.addEventListener(Event.ENTER_FRAME, MoveNote); 
    function MoveNote(event:Event):void 
    { 
     note.y += 5; 
     if (note.y >= stage.stageHeight - 50) 
     { 
      note.addEventListener(Event.ENTER_FRAME, StopNote); 
      function StopNote(event:Event):void 
      { 
       note.removeEventListener(Event.ENTER_FRAME, MoveNote); 
       //do more 
      } 
     } 
    } 
} 

回答

2

如果您知道其类名(Flash CSx中的符号名称),则可以创建“拾取的动画片段副本”。假设你已经画出了一张不错的纸条,并在库中命名为NGood1。然后你需要复制那个符号,你做var note:MovieClip = new NGood1();你可以做一组笔记从中挑选出来,也就是说,notes数组中的所有命名笔记都不是时间线,而是类名或符号名称(而这些与Actionscript 3相同),并且复制一个选定的notes[spawn]符号,您可以这样做var note:MovieClip = new notes[spawn]();请注意括号,这些使得Flash可以调用构造函数来完成一个完整的新对象。

你还没有做的另一件事是正确的清理。你看,你要指定一个MoveNote功能每一个音符作为一个事件监听器,并删除您分配事件(Event.ENTER_FRAME)另一事件侦听器监听器 - 你不应该这样做,而不是你打电话removeEventListener(Event.ENTER_FRAME, MoveNote);时你需要注意停止移动。

还有一件事要做:一旦你在听众中,你需要依靠event.target来找出哪些对象现在正在处理事件,并且你总是使用note变量。想象一下,你已经在舞台上添加了两个注释,现在你需要将它们都移动到每帧5个像素。你在那里有两个音符,但只有其中一个被存储在你的note变量中,因此,两个听众(你为每个音符分配一个音符,这使得两个活动音符)将移动一个音符,而不是每个音符移动它自己的音符。幸运的是,你有一种方法来访问正在特定侦听器中侦听的对象,即通过该事件并获取其属性target。然后你移动该目标(如果需要,首先进行类型转换),这会使每个音符以自己的速度向下移动。

var notes:Array = [NGood1,NGood2,NGood3,NGood4,NGood5,NBad1,NBad2,NBad3,NBad4,NBad5]; 
// this syntax is valid too, and here all the note names are symbol names! 
function SpawnNote(event:Event):void { 
    // your code up to creation is intact 
    var note:MovieClip = new notes[spawn](); 
    addChild(note); 
    // again intact code up to listener 
    note.addEventListener(Event.ENTER_FRAME, MoveNote); 
} // watch this! You are to put function outside this function, and it's the better 
// way of making event listeners for nested objects. 
function MoveNote(event:Event):void 
{ 
    var note:DisplayObject=event.target as DisplayObject; 
    // get the note being processed, then process as intended 
    note.y += 5; 
    if (note.y >= stage.stageHeight - 50) 
    { 
     note.removeEventListener(Event.ENTER_FRAME, MoveNote); 
     //do more 
     removeChild(note); // note's out of play 
     // If, however, you need to do something for more than a single frame, 
     // you may add a listener too, and program corresponding behavior 
    } 
} 
+0

谢谢你。我设法清理了一下我的代码。 但我得到错误与var注:MovieClip =新笔记[spawn](); 我是否需要删除时间线中的动画片段? 这里是我的代码:http://pastebin.com/3a9acu1X –

+0

是的,你需要,因为'NGood1'现在不是对象名称,而是类名称。比方说,你画一个新的影片剪辑并保存到图书馆,然后有一个复选框“Export for Actionscript”,你检查它并输入类名,然后你可以在你的'notes'数组中使用它。此外,你再次试图在你的'btimerhandler'函数中依赖局部变量,因为你的'bsecondsN'位于**函数**'MoveNote'内,而不是在主时间轴代码中。请注意那些花括号,这些表示物体的可用性和它们的寿命。 – Vesper

+0

您应该在'MoveNote'代码之外定位您的'btimerhandler'监听器,就像我使用与'SpawnNote'相关的'MoveNote'一样。事实上,你似乎想让你的笔记在“地面”放下3秒钟然后消失,但是如果同时有几个地面在地面上,你能告诉我什么笔记要被删除吗? – Vesper