2012-03-26 145 views
1

當我嘗試運行我爲使用粒子效果庫所做的一小段代碼時,出現以下錯誤。AS3 - 錯誤#1009:無法訪問空對象引用的屬性或方法

錯誤#1009:

我本來我從主運行整個代碼和它工作得很好,當我決定把它變成一個「無法訪問空對象引用的屬性或方法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類,這是以前相同的只是命名爲「主」,並將從運行。

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

最有可能它沒有添加到舞臺;因此,代碼執行時stage爲空。

傾聽添加到舞臺上,然後執行該代碼:

addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler); 

function addedToStageHandler(event:Event):void 
{ 
    stage.addEventListener(MouseEvent.CLICK, _onStageMouseDown); 
} 
從主

此之前,人們可能已經加入到舞臺。

從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); 
     } 
    } 
} 
+0

隨着那另外我不再出現錯誤,則負載的FlashPlayer,然後只需用whitescreen,在那裏像以前一樣,將工作的罰款坐。 – user1293902 2012-03-26 20:53:09

+0

是否有錯誤?不僅需要您的代碼摘錄才能完全理解您的實現。 – 2012-03-26 21:20:03

+0

沒有錯誤。我已更新我的原始帖子以顯示完整的Main和ParticalManager類。在此之前,ParticalManager類僅僅是Main類。 – user1293902 2012-03-26 21:29:10

相關問題