舞台上的影片剪辑消失

发布于 2024-12-31 21:10:47 字数 2665 浏览 5 评论 0原文

我正在创建一个矿工游戏(其中有金块,你必须用钩子抓住它们)。 我修改了 Main 类,以获取名为“Gold”的 MovieClip(根)的所有子级,并将它们放入一个数组(boulders[])中,稍后检查碰撞(我为每个等级)。当玩家收集所有黄金时(当巨石 array.length <= 0 时),然后 goToAndStop(nextLevel),再次从舞台上获取黄金 MovieClip,如果它的名称是 ==“Gold”,则将其放入巨石中[]。 发生了什么: 在第一级(第 2 帧,因为第 1 帧是预加载器)一切都很好 - 我在舞台上有 2 个金色 MovieClip(在设计模式下),所有 2 个金色都放入数组中并显示。 在第二层,我有 4 个金币,但只有 2 个被放入阵列中并显示。 在第三层,我在舞台上设计了7枚金牌,但只有3枚展示并放入巨石阵列中。 这几乎是从下一帧黄金影片剪辑的计数中减去先前的黄金计数(上一帧)。 有人有什么想法吗?

我放置 Main.as 代码:

public function createLevel(){
                nextLevel = this.currentFrame + 1;
                for (var i:uint = 0; i < MovieClip(root).numChildren; i++){
                if(MovieClip(root).getChildAt(i).name=="Gold")
                {
                    this.addChild(MovieClip(root).getChildAt(i));
                    boulders.push(MovieClip(root).getChildAt(i));
                }
                }
                placePod();
                stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPress);
                stage.addEventListener(KeyboardEvent.KEY_UP, onKeyRelease);
                addEventListener(Event.ENTER_FRAME, updateStatus);
            }

在这里,我将所有名为“Gold”的孩子添加到巨石数组中,并将它们放在舞台上。 然后添加 Pod 和杆。

 private function placePod():void {
                pod = new Pod();
                addChild(pod);
                pod.createRod();
            }

这里是监听按键并删除巨石的函数,如果 boulder.length <= 0 则删除所有监听器和 goToAndStop(nextLevel) 并再次调用 createLevel() 函数以再次加载下一帧的黄金 MovieClips。我上面描述的问题就来了。

private function updateStatus(e:Event):void {
                if (!left && !right) {
                    pod.setSpeed(0);
                } else if (left && right) {
                    pod.setSpeed(0);
                } else if (left) {
                    pod.setSpeed(-4);
                } else {
                    pod.setSpeed(4);
                }

                for (var i in boulders) {
                    boulders[i].updateStatus();
                    if (boulders[i].remove) {
                        removeBoulder(boulders[i]);
                        boulders.splice(i, 1);
                    }
                }
            if(boulders.length <= 0) 
            {
                pod.removeRod();
                removeChild(pod);


    stage.removeEventListener(KeyboardEvent.KEY_DOWN,onKeyPress);
    stage.removeEventListener(KeyboardEvent.KEY_UP,onKeyRelease);
                    removeEventListener(Event.ENTER_FRAME, updateStatus);
                    MovieClip(root).gotoAndStop(nextLevel);
                    createLevel();
                }
                }

I am creating a miner game (where there are pieces of gold and you have to catch them with hook).
I modified the Main class to take all children of MovieClip(root) that has name "Gold" and them put them in an Array(boulders[]) where later check collision (I design and put on stage several "Gold" MovieClips for every level). When the player collects all gold (when the boulders array.length <= 0) then goToAndStop(nextLevel), where again it takes the gold MovieClips from the stage and if it's name is == "Gold" then put's it in the boulder[].
What happened:
On the first level (frame 2 cause frame 1 is the preloader) everything is good - I have 2 gold MovieClips on the stage(in design mode) and all 2 gold are put in the array and displayed.
On the second level I have 4 gold and only 2 of them are put in the array and displayed.
On the third level I have 7 golds designed on stage, but only 3 are displayed and put in the boulder array.
It's almost the previous count of the gold(on the previous frame) are subtracted from the count of the next frame gold movieclips.
Someone any idea?

I am placing the Main.as code:

public function createLevel(){
                nextLevel = this.currentFrame + 1;
                for (var i:uint = 0; i < MovieClip(root).numChildren; i++){
                if(MovieClip(root).getChildAt(i).name=="Gold")
                {
                    this.addChild(MovieClip(root).getChildAt(i));
                    boulders.push(MovieClip(root).getChildAt(i));
                }
                }
                placePod();
                stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPress);
                stage.addEventListener(KeyboardEvent.KEY_UP, onKeyRelease);
                addEventListener(Event.ENTER_FRAME, updateStatus);
            }

Here I add all children with name "Gold" in boulder array and put them on the stage.
Then add Pod and rod.

 private function placePod():void {
                pod = new Pod();
                addChild(pod);
                pod.createRod();
            }

And here is the function that listens for key press and remove boulders, then if boulder.length <= 0 removes all listeners and goToAndStop(nextLevel) and calls the createLevel() function again to load again the gold MovieClips of the next frame. Here comes the problem I described upper.

private function updateStatus(e:Event):void {
                if (!left && !right) {
                    pod.setSpeed(0);
                } else if (left && right) {
                    pod.setSpeed(0);
                } else if (left) {
                    pod.setSpeed(-4);
                } else {
                    pod.setSpeed(4);
                }

                for (var i in boulders) {
                    boulders[i].updateStatus();
                    if (boulders[i].remove) {
                        removeBoulder(boulders[i]);
                        boulders.splice(i, 1);
                    }
                }
            if(boulders.length <= 0) 
            {
                pod.removeRod();
                removeChild(pod);


    stage.removeEventListener(KeyboardEvent.KEY_DOWN,onKeyPress);
    stage.removeEventListener(KeyboardEvent.KEY_UP,onKeyRelease);
                    removeEventListener(Event.ENTER_FRAME, updateStatus);
                    MovieClip(root).gotoAndStop(nextLevel);
                    createLevel();
                }
                }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心意如水 2025-01-07 21:10:47

解决有关全局属性的任何问题的最简单的解决方案,

当您在 Flash 中创建某些内容时,首先添加一个空的 MovieClip,为其提供一个实例,例如游戏或测验,而不是双击您的影片剪辑,然后使您的项目返回到主时间轴以键入所有代码现在你所有的变量以及其他变量都可以很容易地成为全局变量

the easiest solution to any problems regarding global properties

when you create something in flash first add an empty MovieClip give it an instance such as game or Quiz than double click into your Movie clip then make your project goback to the main timeline to type all your code and now all of your variables and what not can easly be global

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文