使用另一个班级已经在舞台上的实例

发布于 2024-10-18 11:16:33 字数 1117 浏览 6 评论 0原文

我对面向对象编程的东西很陌生,我很难理解所有这些。

我正在尝试使用类在 AS3.0 中重新创建我在 AS2.0 中使用时间轴编码创建的简单打地鼠游戏。

我已经浏览了很多论坛,但我仍然不明白我到底做错了什么。

这是我的设置:

  1. 我有一个名为 mrBunny 的影片剪辑(我的女朋友告诉我将其更改为兔子,因为鼹鼠太丑了。)。现在舞台上有 6 个 mrBunny 实例,每个实例名为 mrBunny0-5
  2. mrBunny 符号链接到 com.mrBunny 类。
  3. 该类有一个名为 randomPlay(); 的方法,我用它来随机化 mrBunny 的动画时间。
  4. 我在舞台上还有一个带有 stageBtn 类的按钮。

软件包com{

导入 flash.display.SimpleButton;
导入 flash.display.MovieClip;
导入 flash.display.Stage;
导入 flash.events.MouseEvent;

公共类 startBtn 扩展 SimpleButton {

    //构造函数
    公共函数startBtn(){
        this.addEventListener(MouseEvent.CLICK, startClick);
    }

    公共函数startClick(事件:MouseEvent):void {

      mrBunny0.randomPlay();
      mrBunny1.randomPlay();
      mrBunny2.randomPlay();
      mrBunny3.randomPlay();
      mrBunny4.randomPlay();
      mrBunny5.randomPlay();

    }
}

}

我希望能够使用 startBtn 来启动 mrBunny# 实例的动画。

据我所知,我还没有完全掌握类和OOP的情况。

I'm new to this OOP stuff, and I'm having a hard time understanding all of this.

I'm trying to recreate in AS3.0 with classes a simple whack-a-mole game I created in AS2.0 using timeline coding.

I've read through a lot of forums, but I still don't understand what exactly I'm doing wrong.

Heres my setup:

  1. I have a movie clip named mrBunny (my girlfriend told me to change it to bunnies as moles were too ugly.). Now there are 6 instances of mrBunny on the stage, each named mrBunny0-5.
  2. The mrBunny symbol is linked to the com.mrBunny class.
  3. The class has a method called randomPlay(); which I use to randomize the animation times of mrBunny.
  4. I also have a button on the stage with the class stageBtn.

package com{

import flash.display.SimpleButton;
import flash.display.MovieClip;
import flash.display.Stage;
import flash.events.MouseEvent;

public class startBtn extends SimpleButton {

    //Constructor
    public function startBtn() {
        this.addEventListener(MouseEvent.CLICK, startClick);
    }

    public function startClick(event:MouseEvent):void {

      mrBunny0.randomPlay();
      mrBunny1.randomPlay();
      mrBunny2.randomPlay();
      mrBunny3.randomPlay();
      mrBunny4.randomPlay();
      mrBunny5.randomPlay();

    }
}

}

I want to be able to use the startBtn to start the animation of the mrBunny# instances.

As far as I am aware, I'm not fully grasping the situation of classes and OOP.

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

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

发布评论

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

评论(2

我家小可爱 2024-10-25 11:16:33

(兔子太可爱了,无法传递这个问题,所以这里...)

在您的 OOP 代码中需要考虑几个问题领域。我会尝试解释一下。

包名称:

给出包名称是为了唯一地标识一个类。考虑一下当您使用具有 StringUtils 类的第 3 方库时的情况。您自己也碰巧有一个 StringUtils 类。您如何将它们存储在您的工作文件夹中?您如何在代码中解决每个问题?

顾名思义,包提供了一种对类进行分组的机制。因此,当您引用您的类时,您可以将其命名为 com.joemidi.utils.StringUtils ,将另一个命名为 com.someoneelse.utils.StringUtils 。包名称可以是您喜欢的任何名称,只要它反映文件夹结构即可。但是,作为行业标准,人们在包名称中使用 URL,因为它们可以保证是唯一的。这就是您在许多软件包中看到 com 的原因。在您的情况下,最好根据此重组您的包(和文件夹结构)。

来自 IDE 的阶段实例

当您在 Flash IDE 中创建实例时,您必须记住它们放置的位置以及您的代码是否知道它们。正如 @weltraumpirat 所说,兔子不在您的 startBtn 内。执行您尝试执行的操作的正确方法是:

Main:
    + contains the bunnies.
    + listens to startButton for MouseEvent.CLICK
    + when startButton is clicked, manipulates the bunnies.

也就是说,我们意识到还有另一个问题:

不要依赖实例名称:

您应该实例化(即创建新的)兔子在您的代码中,而不是在 Flash IDE 中;并从中心变量访问它们。例如:

public class Main extends MovieClip {

    var bunnies:Array = new Array();

    public function Main() {
        createBunnies(7);
        startButton.addEventListener(MouseEvent.CLICK, onStartClicked)
    }

    protected function createBunnies(bunnyCount:int):void {
        for (var i:int = 0; i < bunnyCount; i++) {
            var bunny:Bunny = new Bunny();
            addChild(bunny);
            // configure bunny.x, bunny.y, etc. here.
            bunnies.push(bunny);
        };
    }

    protected function onStartClicked(e:MouseEvent) {
        for (var i:int = 0; i < bunnies.length; i++) {
            var bunny:Bunny = bunnies[i];
            bunnies.randomPlay();
        };
    }

在这里,您不再受您为兔子指定的实例名称的限制。 (当然,我只是在这里假设你的阶段结构。)这样,兔子就更加“独立”于它们上面的代码。另外,您可以使用持有者精灵并从那里跟踪兔子,但现在可能有点先进。

希望这些对您有用。如果您认真对待这个主题,您可能想阅读有关 OOP 的更多内容,特别是为什么需要它,以及有关它的关键术语:解耦、继承、封装等。

(The bunnies were just too cute to pass on this question, so here goes...)

There are several problem areas to consider in your code about OOP. I will try to explain them a bit.

Package Names:

Package names are given in order to uniquely identify a class. Consider the situation when you are using a 3rd party library, which has a StringUtils class. You also happen to have a StringUtils class yourself. How do you store them in your work folder? How do you address each of them in your code?

Packages, as the name implies, provide a mechanism to group classes. So when you refer to your class you might address it as com.joemidi.utils.StringUtils and the other as com.someoneelse.utils.StringUtils. Package names can be anything you like, as long as it reflects the folder structure. But, as an industry standart people use URLs in their package names, as they are guaranteed to be unique. This is the reason you see com in many packages. In your situation it is better if you restructure your packages (and folder structures) according to this.

Stage Instances from the IDE:

When you create instances in the Flash IDE, you must remember where you put them and whether your code knows about them or not. The bunnies, as @weltraumpirat said, are not inside your startBtn. The proper way to do what you are trying to do is this:

Main:
    + contains the bunnies.
    + listens to startButton for MouseEvent.CLICK
    + when startButton is clicked, manipulates the bunnies.

That said, we realize there is another problem:

Don't Rely on Instance Names:

You should instantiate (that is, create new) bunnies in your code, not in the Flash IDE; and access them from a central variable. For example:

public class Main extends MovieClip {

    var bunnies:Array = new Array();

    public function Main() {
        createBunnies(7);
        startButton.addEventListener(MouseEvent.CLICK, onStartClicked)
    }

    protected function createBunnies(bunnyCount:int):void {
        for (var i:int = 0; i < bunnyCount; i++) {
            var bunny:Bunny = new Bunny();
            addChild(bunny);
            // configure bunny.x, bunny.y, etc. here.
            bunnies.push(bunny);
        };
    }

    protected function onStartClicked(e:MouseEvent) {
        for (var i:int = 0; i < bunnies.length; i++) {
            var bunny:Bunny = bunnies[i];
            bunnies.randomPlay();
        };
    }

Here, you are no longer bound to what instance names you gave to the bunnies. (Of course, I'm just assuming your stage structure here.) And this way, the bunnies are more "independent" of the code above them. Also, you could use a holder sprite and track the bunnies from there, but it might be a bit advanced right now.

Hope these may prove useful to you. If you are serious about this subject, you might like to read more on OOP, specifically on why it is needed, and key terms about it: decoupling, inheritance, encapsulation, etc.

疾风者 2024-10-25 11:16:33

startBtn 没有成员 mrBunny0-5,但主时间线有。请尝试使用 root.mrBunny0 代替。

startBtn doesn't have members mrBunny0-5, the main timeline does. Try root.mrBunny0 instead.

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