如何在 FlashDevelop 中正确创建影片剪辑
我试图弄清楚为什么当我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是您的 Drop 类是位图。它不能动态添加属性。
简单的解决方法是创建空的影片剪辑并将您的拖放添加到其中。更好的解决方法是为您的 Drop 编写一个具有您需要的属性的自定义类。但由于您只要求简单的修复,请尝试以下操作:
应该替换:
更好的修复方法是创建一个使用您想要的属性扩展 Bitmap 的类,如下所示:
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:
That should replace:
The better fix is to make a class that extends Bitmap with the properties you want like so:
删除此:
删除此:
在 for 循环中使用此:
如果您需要水滴,请创建一个数组并将水滴推入其中。
我希望这就是您正在寻找的,祝您好运
抢
Remove this:
Remove this:
In your for loop use this:
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