【问题标题】:Generate falling random object to choose from in the array生成下落随机对象以在数组中进行选择
【发布时间】:2014-01-12 22:22:14
【问题描述】:

我正在尝试创建一个类似于 Tap Tap 的游戏,但掉落的物体是随机的。我现在遇到的问题是如果随机对象再次被选中(在它完成移动之前),它的位置将被重置。(这也意味着两个相同的对象不会出现在我不想要的屏幕上)我想出了一个解决这个问题的方法,我应该能够创建一个选择的电影剪辑的副本,但我迷路了。帮助?

另外,我是 Flash 新手。如果您有任何建议/建议,请告诉我! 谢谢你,祝你有美好的一天。

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
            }
        }
    }
}

【问题讨论】:

    标签: actionscript-3 flash


    【解决方案1】:

    如果您知道它的类名(Flash CSx 中的符号名称),您可以创建一个“选中的影片剪辑的副本”。假设你画了一个很好的笔记并在图书馆中将其命名为NGood1。然后你需要复制那个符号,你做var note:MovieClip = new NGood1();你可以在你的数组中做一组笔记作为classes,也就是说,所有命名的笔记在notes 数组不是时间轴上的对象,而是类名称或符号名称(这些名称与 Actionscript 3 相同),要复制选定的 notes[spawn] 符号,请执行 var note:MovieClip = new notes[spawn](); 注意括号,这些使 Flash 调用构造函数来创建一个全新的对象。

    您还没有做的另一件事是适当的清理。看,您正在为每个音符分配一个 MoveNote 函数作为事件侦听器,要删除该侦听器,您在 same 事件上分配 另一个 事件侦听器 (@987654328 @) - 你不应该这样做,而是当你需要笔记停止移动时调用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
        }
    }
    

    【讨论】:

    • 谢谢伙计。我设法稍微清理了我的代码。但是我收到了 var note:MovieClip = new notes[spawn](); 的错误。我是否需要删除时间线中的影片剪辑?这是我现在的代码:pastebin.com/3a9acu1X
    • 是的,你需要,因为NGood1 现在不是对象名而是类名。假设您绘制了一个新的影片剪辑并将其保存到库中,然后有一个复选框“Export for Actionscript”,您选中它并输入您可以在 notes 数组中使用的类名。此外,您再次尝试依赖 btimerhandler 函数中的局部变量,因为您的 bsecondsN 位于 函数 MoveNote 中,至少不在主时间线代码中。请注意那些花括号,它们表示对象的可用性及其生命周期。
    • 您应该在MoveNote 代码之外找到您的btimerhandler 侦听器,就像我对与SpawnNote 相关的MoveNote 所做的那样。事实上,你似乎想让你的笔记在“地面”上放置 3 秒然后消失,但你能告诉我如果同时有几个笔记在地面上,要删除什么笔记吗?
    • 好吧,我想做的是,如果音符到达地面,它会在消失之前停留 3 秒。嗯,现在我意识到每次产生新音符时都会覆盖音符变量,因此无法将其与其他音符区分开来。顺便说一句,我删除了时间轴中的对象,但出现错误 #1034。我已经检查了“Export for Actionscript”。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-29
    • 2021-03-17
    • 1970-01-01
    • 2019-11-17
    • 2014-05-13
    • 1970-01-01
    • 2015-04-15
    相关资源
    最近更新 更多