尝试运行我正在使用粒子效果库的一小段代码时出现以下错误。
错误#1009:无法访问空对象引用的属性或方法
我最初从Main运行我的整个代码并且工作正常,当我决定将它放入“ParticleManager”类时,我开始收到此错误。
错误本身就在这里 -
stage.addEventListener(MouseEvent.CLICK, _onStageMouseDown);
function _onStageMouseDown(e:MouseEvent):void
{
//Draws an explosion on to the screen at the position of the mouse click.
var emitter:Emitter3D = new ParticleExplosion(new Vector3D(mouseX- width/2, mouseY- height/2, 0));
renderer.addEmitter( emitter );
emitter.start();
trace(e);
trace(renderer.mouseX)
trace(renderer.mouseY)
}
}
它出现在那里的第一行。而且我试图研究它并发现这是因为舞台没有设置任何东西,但我该怎么解决呢?为什么它在Main之前运行良好?
谢谢!
编辑 - 要求提供其他信息。
主要课程
package
{
import flash.display.Sprite;
[SWF(width='800', height='600', frameRate='60', backgroundColor='#000000')]
/**
* ...
* @author
*/
public class Main extends Sprite
{
private var pManager:ParticleManager;
public function Main()
{
pManager = new ParticleManager;
}
}
}
ParticleManager类,之前只是命名为“Main”,并且可以从中运行。
package
{
//Flint imports
import flash.events.Event;
import flash.events.MouseEvent;
import org.flintparticles.common.emitters.Emitter;
import org.flintparticles.common.actions.ScaleImage;
import org.flintparticles.common.events.EmitterEvent;
import org.flintparticles.common.events.ParticleEvent;
import org.flintparticles.threeD.emitters.Emitter3D;
import org.flintparticles.threeD.particles.Particle3D;
import org.flintparticles.threeD.renderers.BitmapRenderer;
import org.flintparticles.threeD.renderers.controllers.FirstPersonCamera;
import org.flintparticles.threeD.zones.LineZone;
//Flash imports
import flash.display.Sprite;
import flash.filters.BlurFilter;
import flash.filters.ColorMatrixFilter;
import flash.geom.Rectangle;
import flash.geom.Vector3D;
/**
* ...
* @author
*/
public class ParticleManager extends Sprite
{
private var orbitter:FirstPersonCamera;
private var renderer:BitmapRenderer;
public function ParticleManager()
{
//Sets up the "BitMapRenderer" a rectangle where the particle effects will be drawn on to
renderer = new BitmapRenderer( new Rectangle( 0, 0, 800, 600 ), false );
renderer.x = 0;
renderer.y = 0;
renderer.addFilter( new BlurFilter( 2, 2, 1 ) );
renderer.addFilter( new ColorMatrixFilter( [ 1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0.95,0 ] ) );
addChild( renderer );
renderer.camera.position = new Vector3D( 0, 0, -400 );
renderer.camera.target = new Vector3D( 0, 0, 0 );
renderer.camera.projectionDistance = 400;
//Mouse click
function addedToStageHandler(event:Event):void
{
stage.addEventListener(MouseEvent.CLICK, _onStageMouseDown);
}
function _onStageMouseDown(e:MouseEvent):void
{
//Draws an explosion on to the screen at the position of the mouse click.
var emitter:Emitter3D = new ParticleExplosion(new Vector3D(mouseX- width/2, mouseY- height/2, 0));
renderer.addEmitter( emitter );
emitter.start();
trace(e);
trace(renderer.mouseX)
trace(renderer.mouseY)
}
}
public function removeEmitter( ev:EmitterEvent ):void
{
Emitter3D( ev.target ).removeEventListener( EmitterEvent.EMITTER_EMPTY, removeEmitter );
renderer.removeEmitter( Emitter3D( ev.target ) );
}
public function destroy():void
{
for each( var e:Emitter in renderer.emitters )
{
e.stop();
}
}
}
}
答案 0 :(得分:0)
很可能它没有加入舞台;因此,代码执行时stage为null。
侦听添加到舞台,然后执行该代码:
addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
function addedToStageHandler(event:Event):void
{
stage.addEventListener(MouseEvent.CLICK, _onStageMouseDown);
}
以前来自您的Main,它可能已经添加到舞台上。
从Main,我看不到粒子管理器是如何添加到显示列表的。我相信你只需要将粒子管理器添加到Main:
package
{
import flash.display.Sprite;
[SWF(width = '800', height = '600', frameRate = '60', backgroundColor = '#000000')]
public class Main extends Sprite
{
private var pManager:ParticleManager;
public function Main()
{
pManager = new ParticleManager;
addChild(pManager);
}
}
}