如何在 FlashDevelop 中正确创建影片剪辑

发布于 2024-12-14 11:39:41 字数 3260 浏览 1 评论 0原文

我试图弄清楚为什么当我在 flashdevelop 中创建影片剪辑时,所述影片剪辑总是最终为空。

这是我尝试使用的代码:

import flash.display.MovieClip;
    import flash.events.Event;
    import flash.display.Sprite;
    import flash.display.Bitmap;

    public class Rain extends MovieClip
    {
        [Embed(source = '../../../../../lib/images/icons/drop.png')]
        private var Drop: Class;

        private var offset:int = 50;
        private var dropsNumber:int;
        private var dropsVector:Vector.<MovieClip> = new Vector.<MovieClip>();

        public var drop:MovieClip;

        public function init(drops:int, fallSpeed:int, windSpeed:int, hArea:int, vArea:int, dir:String):void
        {
            dropsNumber = drops;

            if (dir == "right")
            {
                offset *= -1;
            }

            for (var i:int = 0; i < drops; i++)
            {
                drop = new Drop() as MovieClip;

                drop.fallSpeed = fallSpeed;
                drop.windSpeed = windSpeed;
                drop.dir = dir;
                drop.hArea = hArea;
                drop.vArea = vArea;

                drop.x = Math.random() * (hArea + offset);
                drop.y=Math.random()*vArea;

                //

                drop.scaleX = Math.round(((Math.random() * 1) + 0.3) * 10) / 10;
                drop.scaleY=drop.scaleX;

                //

                dropsVector.push(drop);

                addChild(drop);
            }

            inTheDirection();
        }

        private function inTheDirection():void
        {
            for (var i:int = 0; i < dropsNumber; i++)
            {
                switch (dropsVector[i].dir)
                {
                    case "left" :

                        dropsVector[i].addEventListener(Event.ENTER_FRAME, moveLeft);

                        break;

                    case "right" :

                        dropsVector[i].scaleX*=-1;
                        dropsVector[i].addEventListener(Event.ENTER_FRAME, moveRight);

                        break;

                    default :

                        trace("There is some error dude...");
                }
            }
        }

        private function moveLeft(e:Event):void
        {
            e.target.x-=e.target.windSpeed;
            e.target.y+=Math.random()*e.target.fallSpeed;

            if (e.target.y>e.target.vArea+e.target.height)
            {
                e.target.x = Math.random() * (e.target.hArea + (offset * 2));
                e.target.y=- e.target.height;
            }
        }

        private function moveRight(e:Event):void
        {
            e.target.x+=e.target.windSpeed;
            e.target.y+=Math.random()*e.target.fallSpeed;

            if (e.target.y>e.target.vArea+e.target.height)
            {
                e.target.x = Math.random() * (e.target.hArea - offset * 2) + offset * 2;//Check
                e.target.y=- e.target.height;
            }
        }
    }

它的目的是在屏幕上绘制雨滴以获得雨滴效果,但我收到此错误:

**[Fault] exception, information=TypeError: Error #1009: Cannot access a property or method of a null object reference.
Fault, Rain.as:32**

我环顾四周但无济于事。因此,非常感谢任何帮助!

谢谢!

I'm trying to figure out why, when I create a movieclip in flashdevelop, said movieclip always ends up being null.

Here is the code I'm trying to use:

import flash.display.MovieClip;
    import flash.events.Event;
    import flash.display.Sprite;
    import flash.display.Bitmap;

    public class Rain extends MovieClip
    {
        [Embed(source = '../../../../../lib/images/icons/drop.png')]
        private var Drop: Class;

        private var offset:int = 50;
        private var dropsNumber:int;
        private var dropsVector:Vector.<MovieClip> = new Vector.<MovieClip>();

        public var drop:MovieClip;

        public function init(drops:int, fallSpeed:int, windSpeed:int, hArea:int, vArea:int, dir:String):void
        {
            dropsNumber = drops;

            if (dir == "right")
            {
                offset *= -1;
            }

            for (var i:int = 0; i < drops; i++)
            {
                drop = new Drop() as MovieClip;

                drop.fallSpeed = fallSpeed;
                drop.windSpeed = windSpeed;
                drop.dir = dir;
                drop.hArea = hArea;
                drop.vArea = vArea;

                drop.x = Math.random() * (hArea + offset);
                drop.y=Math.random()*vArea;

                //

                drop.scaleX = Math.round(((Math.random() * 1) + 0.3) * 10) / 10;
                drop.scaleY=drop.scaleX;

                //

                dropsVector.push(drop);

                addChild(drop);
            }

            inTheDirection();
        }

        private function inTheDirection():void
        {
            for (var i:int = 0; i < dropsNumber; i++)
            {
                switch (dropsVector[i].dir)
                {
                    case "left" :

                        dropsVector[i].addEventListener(Event.ENTER_FRAME, moveLeft);

                        break;

                    case "right" :

                        dropsVector[i].scaleX*=-1;
                        dropsVector[i].addEventListener(Event.ENTER_FRAME, moveRight);

                        break;

                    default :

                        trace("There is some error dude...");
                }
            }
        }

        private function moveLeft(e:Event):void
        {
            e.target.x-=e.target.windSpeed;
            e.target.y+=Math.random()*e.target.fallSpeed;

            if (e.target.y>e.target.vArea+e.target.height)
            {
                e.target.x = Math.random() * (e.target.hArea + (offset * 2));
                e.target.y=- e.target.height;
            }
        }

        private function moveRight(e:Event):void
        {
            e.target.x+=e.target.windSpeed;
            e.target.y+=Math.random()*e.target.fallSpeed;

            if (e.target.y>e.target.vArea+e.target.height)
            {
                e.target.x = Math.random() * (e.target.hArea - offset * 2) + offset * 2;//Check
                e.target.y=- e.target.height;
            }
        }
    }

It is meant to draw rain drops on the screen for a rainy effect but I'm getting this error:

**[Fault] exception, information=TypeError: Error #1009: Cannot access a property or method of a null object reference.
Fault, Rain.as:32**

I've done some looking around to no avail. So any help is greatly appreciated!

Thanks!

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

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

发布评论

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

评论(2

鹿港小镇 2024-12-21 11:39:41

问题是您的 Drop 类是位图。它不能动态添加属性。

简单的解决方法是创建空的影片剪辑并将您的拖放添加到其中。更好的解决方法是为您的 Drop 编写一个具有您需要的属性的自定义类。但由于您只要求简单的修复,请尝试以下操作:

drop = new MovieClip();
drop.addChild(new Drop());

应该替换:

drop = new Drop() as MovieClip; // replace this line

更好的修复方法是创建一个使用您想要的属性扩展 Bitmap 的类,如下所示:

package 
{
    import flash.display.Bitmap;
    import flash.display.BitmapData;

    /**
     * ...
     * @author ...
     */

     [Embed(source = '../../../../../lib/images/icons/drop.png')]
      private var DropArt: Class;
      public var fallSpeed;
      public var windSpeed;
      public var dir;
      public var hArea;
      public var vArea;

    public class  Drop extends Bitmap
    {   
        var art:Bitmap  = new DropArt();
        this.bitmapData = art.bitmapData;
    }

}

The problem is that your Drop class is a Bitmap. It cannot have properties dynamically added.

The easy fix is to create empty movie clips and add your drop to it. The better fix is to write a custom class for your Drop that has the properties you need. But since you only asked for the easy fix, try this:

drop = new MovieClip();
drop.addChild(new Drop());

That should replace:

drop = new Drop() as MovieClip; // replace this line

The better fix is to make a class that extends Bitmap with the properties you want like so:

package 
{
    import flash.display.Bitmap;
    import flash.display.BitmapData;

    /**
     * ...
     * @author ...
     */

     [Embed(source = '../../../../../lib/images/icons/drop.png')]
      private var DropArt: Class;
      public var fallSpeed;
      public var windSpeed;
      public var dir;
      public var hArea;
      public var vArea;

    public class  Drop extends Bitmap
    {   
        var art:Bitmap  = new DropArt();
        this.bitmapData = art.bitmapData;
    }

}
迷乱花海 2024-12-21 11:39:41
  1. 在 AS 中,您可以向 MovieClip 添加自定义属性,因此您不必创建自定义类。
  2. 您在 for 循环中覆盖相同的 drop 实例,这可能没有用。

删除此:

private var Drop: Class;

删除此:

public var drop:MovieClip;

在 for 循环中使用此:

var drop = new MovieClip();

如果您需要水滴,请创建一个数组并将水滴推入其中。

我希望这就是您正在寻找的,祝您好运

  1. In AS you can add custom properties to MovieClips, so you don't have to create a custom class.
  2. You are overriding the same drop instance in the for loop, which might not be useful.

Remove this:

private var Drop: Class;

Remove this:

public var drop:MovieClip;

In your for loop use this:

var drop = new MovieClip();

If you need the drops, create an array and push the drops in it.

I hope this is what you are looking for, good luck,
Rob

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