AS3 对象数组到类

发布于 2024-12-01 21:47:32 字数 1730 浏览 3 评论 0原文

我是新手,在使用 as3 中的类时遇到问题。 我在我的主时间轴中创建了一系列对象

    function badPlayer() 
    {
        var bads:Array = new Array();
        for (var i=0; i<5; i++) 
        {
            var mc = new bman();
            mc.name=["mc"+i];
            bads.push(mc); 
            _backGround.addChild(mc);
            mc.x = 100;
            mc.y = 100;
            trace (bads);
            Baddies(_backGround.mc); //here I am trying to export mc to my class
        }
    }

,这是我的班级的一个片段。我的跟踪语句甚至不会输出。

    public class Baddies extends MovieClip
{
    private var pistolSound:pistolShot = new pistolShot();
    //private var mc = new mc();
    private var _rotateSpeedMax:Number = 2;
    private var _gravity:Number = .68;
    private var _bulletSpeed:Number = 2;        
    private var _maxDistance:Number = 200;
    private var _reloadSpeed:Number = 500; //milliseconds
    private var _barrelLength:Number = 20;
    private var _bulletSpread:Number = 5;
    private var _isLoaded:Boolean = true;       
    private var _isFiring:Boolean = true;
    private var _endX:Number;
    private var _endY:Number;
    private var _startX:Number;
    private var _startY:Number;
    private var _reloadTimer:Timer;
    private var _bullets:Array = [];
    private var _gun:MovieClip;
    private var _enemy:MovieClip;
    private var _yx:Number;
    private var _yy:Number;
    private var _pcos:Number;
    private var _psin:Number;
    private var _trueRotation:Number;
    public function Baddies() 
    {
        trace("working");
        stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
    }

基本上,我试图创建几个坏人(bman),并将相同的代码应用于每个坏人。我还尝试将 bman 的链接名称更改为 Baddies,但没有成功。

I am new and having an issue with the use of classes in as3.
I have created an array of objects in my main timeline

    function badPlayer() 
    {
        var bads:Array = new Array();
        for (var i=0; i<5; i++) 
        {
            var mc = new bman();
            mc.name=["mc"+i];
            bads.push(mc); 
            _backGround.addChild(mc);
            mc.x = 100;
            mc.y = 100;
            trace (bads);
            Baddies(_backGround.mc); //here I am trying to export mc to my class
        }
    }

Here is a snip-it from my class. My trace statement wont even output.

    public class Baddies extends MovieClip
{
    private var pistolSound:pistolShot = new pistolShot();
    //private var mc = new mc();
    private var _rotateSpeedMax:Number = 2;
    private var _gravity:Number = .68;
    private var _bulletSpeed:Number = 2;        
    private var _maxDistance:Number = 200;
    private var _reloadSpeed:Number = 500; //milliseconds
    private var _barrelLength:Number = 20;
    private var _bulletSpread:Number = 5;
    private var _isLoaded:Boolean = true;       
    private var _isFiring:Boolean = true;
    private var _endX:Number;
    private var _endY:Number;
    private var _startX:Number;
    private var _startY:Number;
    private var _reloadTimer:Timer;
    private var _bullets:Array = [];
    private var _gun:MovieClip;
    private var _enemy:MovieClip;
    private var _yx:Number;
    private var _yy:Number;
    private var _pcos:Number;
    private var _psin:Number;
    private var _trueRotation:Number;
    public function Baddies() 
    {
        trace("working");
        stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
    }

Basically I am trying to create several bad guys (bman) and have the same code apply to each of them. I have also tried to change the linkage name of bman to Baddies with no success.

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

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

发布评论

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

评论(1

妄想挽回 2024-12-08 21:47:32

这段代码有一些非常错误的地方。

Baddies(_backGround.mc); //here I am trying to export mc to my class

正如评论中已经指出的那样,这是一种类型转换。顺便说一句,Baddies 不是一个好名字,因为它是复数。您可能想创建一个新的坏人,这可以通过以下行完成:

var baddie = new Baddies();

现在您的构造函数使用stage变量。这不起作用,因为该对象不在舞台上,因此 stagenull (如果您将实例拖放到编辑器中的舞台上,则可能会起作用)。因此,在使用 stage 之前,您实际上需要将对象添加到 stage:

public function Baddies() {
    trace("new baddie created");
}

public function init(mc:MovieClip) {
    mc.addChild(this); // display this baddie
    trace("working");
    stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
}

并在 badPlayer 函数中:

var baddie = new Baddies();
baddie.init(_backGround);

There are a few thing that are very wrong with this code.

Baddies(_backGround.mc); //here I am trying to export mc to my class

This is a typecast, as already stated in the comments. By the way Baddies isn't a good name, because it plural. You probably want to create a new bad guy, which would be done with this line:

var baddie = new Baddies();

Now your constructor uses the stage variable. This won't work because the object isn't on the stage, therefore stage is null (it may works if you drag and drop an instance to the stage in the editor). So before using the stage you actually need to add the object to the stage:

public function Baddies() {
    trace("new baddie created");
}

public function init(mc:MovieClip) {
    mc.addChild(this); // display this baddie
    trace("working");
    stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
}

And in the badPlayer function:

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