2012-03-26 93 views
0

所以我搜索了一下,發現了一些類似的錯誤,但不幸的是沒有解決這個問題。我正在編寫一款iPad遊戲,爲了優化,我創建了一個名爲R的資產引用類,遵循gotoAndLearn的教程示例。

我的錯誤火災時,我嘗試從R中的第一個靜態的BitmapData,奇怪的是指向類報關行:

TypeError: Error #1009: Cannot access a property or method of a null object reference. 

at Game::R$cinit() 

at global$init()[...\Game\R.as:6] 

at Game::Background/init()[...\Game\Background.as:33] 

的R第6行的代碼是這樣的:

public final class R 

爲了更好的可視性,這裏是R的完整的聲明和第一線,我嘗試訪問的:

package Game 
{ 
import flash.display.BitmapData; 
import flash.geom.Rectangle; 

public final class R 
{ 
    /** Embed source asset in a class. **/ 
    [Embed (source= "Assets/Backgrounds/lvl1.png")] 
    public static var L1Background:Class; 
    /** Make a BitmapData out of the latter class. **/ 
    public static var _l1Background:BitmapData= new L1Background().bitmapData; 

該類的以下內容大部分是相同的事物,反覆重複以加載所有資產。另外,這裏是調用R._l1Background的代碼。這是Background類的初始化。

private function init(e:Event):void 
    { 
     this.removeEventListener(Event.ENTER_FRAME,init); 

     switch (level) 
     { 
      case 0: 
       // image is a class property to keep track of the BitmapData to use later. 
       image= R._l1Background; 
      break; 

      default: 
       image= R._l1Background; 
      break; 
     } 
    } 

如果我試圖評論的訪問R._l1Background(從而使後者的功能什麼也不做)我仍然得到類似的錯誤,當其他功能嘗試訪問的r屬性。我可能(很有可能)在我的代碼中有其他錯誤,但目前這使我無法調試其餘的遊戲。

我會問我的老師這個問題,並提出他們想出的任何領導或解決方案,以及我自己的研究成果。預先感謝您的幫助。

編輯:請求後,這裏是我完整的類

我的完整的參考級(R級)如下:

package Game 
{ 
import flash.display.BitmapData; 
import flash.geom.Rectangle; 

public final class R 
{ 
    /** Class of the level1 background asset. In order to use it, create a 
    * new instance and convert it to bitmapData. **/ 
    [Embed (source= "Assets/Backgrounds/lvl1.png")] 
    public static const L1Background:Class; 
    /** BitmapData of the image. **/ 
    public static var _l1Background:BitmapData= new L1Background().bitmapData; 

    /** Class of the players' HUD asset. In order to use it, create a new 
    * instance and convert it to bitmapData. **/ 
    [Embed (source= "Assets/HUD/playerHud1.png")] 
     public static const Hud1:Class; 
    /** BitmapData of the image. **/ 
    public static var _hud1:BitmapData= new Hud1().bitmapData; 

    /** Class of the points counters asset. In order to use it, create a new 
    * instance and convert it to bitmapData. **/ 
    [Embed (source= "Assets/HUD/counter.png")] 
     public static const Counter1:Class; 
    /** BitmapData of the image. **/ 
    public static var _counter1:BitmapData= new Counter1().bitmapData; 

    /** Class of the racquet 1 asset. In order to use it, create a new 
    * instance and convert it to bitmapData. 
    * This is a yellow up-pointed racquet. **/ 
    [Embed (source= "Assets/Players/racq1.png")] 
     public static const Racquet1:Class; 
    /** BitmapData of the image. **/ 
    public static var _racquet1:BitmapData= new Racquet1().bitmapData; 

    /** Class of the racquet 2 asset. In order to use it, create a new 
    * instance and convert it to bitmapData. 
    * This is a red down-pointed racquet. **/ 
    [Embed (source= "Assets/Players/racq2.png")] 
     public static const Racquet2:Class; 
    /** BitmapData of the image. **/ 
    public static var _racquet2:BitmapData= new Racquet2().bitmapData; 

    /** Class of the twinky 1 asset. In order to use it, create a new 
    * instance and convert it to bitmapData. 
    * This is a yellow up-pointed twinky. **/ 
    [Embed (source= "Assets/Players/twinky1.png")] 
     public static const Twinky1:Class; 
    /** BitmapData of the image. **/ 
    public static var _twinky1:BitmapData= new Twinky1().bitmapData; 

    /** Class of the twinky 2 asset. In order to use it, create a new 
    * instance and convert it to bitmapData. 
    * This is a red down-pointed twinky. **/ 
    [Embed (source= "Assets/Players/twinky2.png")] 
     public static const Twinky2:Class; 
    /** BitmapData of the image. **/ 
    public static var _twinky2:BitmapData= new Twinky2().bitmapData; 

    /** Class of the "back" button asset. In order to use it, create a new 
    * instance and convert it to bitmapData. **/ 
    [Embed (source= "Assets/HUD/bt_back.png")] 
     public static const BtBack:Class; 
    /** BitmapData of the image. **/ 
    public static var _btBack:BitmapData= new BtBack().bitmapData; 

    /** Class of the "replay" button asset. In order to use it, create a new 
    * instance and convert it to bitmapData. **/ 
    [Embed (source= "Assets/HUD/bt_replay.png")] 
     public static const BtReplay:Class; 
    /** BitmapData of the image. **/ 
    public static var _btReplay:BitmapData= new BtReplay().bitmapData; 

    /** Class of the yellow "Winner" asset. In order to use it, create a new 
    * instance and convert it to bitmapData. **/ 
    [Embed(source="Assets/HUD/gagne_jaune.png")] 
     public static const GagneJaune:Class; 
    /** BitmapData of the image. **/ 
    public static var _gagneJaune:BitmapData= new GagneJaune().bitmapData; 

    /** Class of the red "Winner" asset. In order to use it, create a new 
    * instance and convert it to bitmapData. **/ 
    [Embed(source="Assets/HUD/gagne_rouge.png")] 
     public static const GagneRouge:Class; 
    /** BitmapData of the image. **/ 
    public static var _gagneRouge:BitmapData= new GagneRouge().bitmapData; 

    /** Class of the yellow "Looser" asset. In order to use it, create a new 
    * instance and convert it to bitmapData. **/ 
    [Embed(source="Assets/HUD/perdu_jaune.png")] 
     public static const PerduJaune:Class; 
    /** BitmapData of the image. **/ 
    public static var _perduJaune:BitmapData= new PerduJaune().bitmapData; 

    /** Class of the red "Looser" asset. In order to use it, create a new 
    * instance and convert it to bitmapData. **/ 
    [Embed(source="Assets/HUD/perdu_rouge.png")] 
     public static const PerduRouge:Class; 
    /** BitmapData of the image. **/ 
    public static var _perduRouge:BitmapData= new PerduRouge().bitmapData; 

    /** Class of the idle zwig 1 asset. In order to use it, create a new 
    * instance and convert it to bitmapData. 
    * This is a yellow idle down-pointed zwig. **/ 
    [Embed (source="Assets/Players/zwig1_idle.png")] 
     public static const Zwig1idle:Class; 
    /** BitmapData of the image. **/ 
    public static var _zwig1idle:BitmapData= new Zwig1idle().bitmapData; 
    /** Blitting array for the idle zwig 1. **/ 
    public static var _zwig1idleArray:Array= new Array[z1i000,z1i001,z1i002,z1i003,z1i004,z1i005,z1i006,z1i007,z1i008,z1i009, 
                z1i010,z1i011,z1i012,z1i013,z1i014,z1i015,z1i016,z1i017,z1i018,z1i019, 
                z1i020,z1i021,z1i022,z1i023,z1i024,z1i025,z1i026,z1i027]; 

    /** Class of the idle zwig 2 asset. In order to use it, create a new 
    * instance and convert it to bitmapData. 
    * This is a red idle up-pointed zwig. **/ 
    [Embed (source="Assets/Players/zwig2_idle.png")] 
     public static const Zwig2idle:Class; 
    /** BitmapData of the image. **/ 
    public static var _zwig2idle:BitmapData= new Zwig2idle().bitmapData; 
    /** Blitting array for the idle zwig 2. **/ 
    public static var _zwig2idleArray:Array= new Array[z2i000]; 

    public static var z1i006:Rectangle = new Rectangle(2,133,129,129); 
    public static var z1i021:Rectangle = new Rectangle(133,395,129,129); 
    public static var z1i014:Rectangle = new Rectangle(133,264,129,129); 
    public static var z1i025:Rectangle = new Rectangle(2,526,129,129); 
    public static var z1i016:Rectangle = new Rectangle(264,264,129,129); 
    public static var z1i005:Rectangle = new Rectangle(264,2,129,129); 
    public static var z1i023:Rectangle = new Rectangle(264,395,129,129); 
    public static var z1i024:Rectangle = new Rectangle(2,526,129,129); 
    public static var z1i020:Rectangle = new Rectangle(133,395,129,129); 
    public static var z1i011:Rectangle = new Rectangle(264,133,129,129); 
    public static var z1i015:Rectangle = new Rectangle(133,264,129,129); 
    public static var z1i013:Rectangle = new Rectangle(2,264,129,129); 
    public static var z1i001:Rectangle = new Rectangle(2,2,129,129); 
    public static var z1i010:Rectangle = new Rectangle(264,133,129,129); 
    public static var z1i027:Rectangle = new Rectangle(133,526,129,129); 
    public static var z1i007:Rectangle = new Rectangle(2,133,129,129); 
    public static var z1i018:Rectangle = new Rectangle(2,395,129,129); 
    public static var z1i019:Rectangle = new Rectangle(2,395,129,129); 
    public static var z1i004:Rectangle = new Rectangle(264,2,129,129); 
    public static var z1i017:Rectangle = new Rectangle(264,264,129,129); 
    public static var z1i008:Rectangle = new Rectangle(133,133,129,129); 
    public static var z1i022:Rectangle = new Rectangle(264,395,129,129); 
    public static var z1i000:Rectangle = new Rectangle(2,2,129,129); 
    public static var z1i026:Rectangle = new Rectangle(133,526,129,129); 
    public static var z1i012:Rectangle = new Rectangle(2,264,129,129); 
    public static var z1i003:Rectangle = new Rectangle(133,2,129,129); 
    public static var z1i009:Rectangle = new Rectangle(133,133,129,129); 
    public static var z1i002:Rectangle = new Rectangle(133,2,129,129); 

    public static var z2i000:Rectangle = new Rectangle(2,2,141,149); 
} 
} 

背景類,它試圖股票資產使用在一個屬性,是這樣的:

package Game 
{ 
import flash.display.BitmapData; 
import flash.display.Sprite; 
import flash.geom.Rectangle; 
import flash.events.Event; 
import flash.geom.Point; 

import Game.R; 

public class Background extends Sprite 
{ 
    private var canvas:BitmapData; 
    private var point:Point= new Point(); 
    private var level:uint; 
    private var image:BitmapData; 

    /** Initialise the background by telling it on which canvas to render 
    * and the level needs to be shown. **/ 
    public function Background(canvas:BitmapData,level:uint=0) 
    { 
     this.canvas= canvas; 
     this.level= level; 
     this.addEventListener(Event.ENTER_FRAME,init); 
    } 

    private function init(e:Event):void 
    { 
     this.removeEventListener(Event.ENTER_FRAME,init); 

     switch (this.level) 
     { 
      case 0: 
       this.image= new R.L1Background().bitmapData; 
      break; 

      default: 
       this.image= new R.L1Background().bitmapData; 
      break; 
     } 
    } 

    /** Render the background in the canvas. **/ 
    public function render():void 
    { 
     this.canvas.copyPixels(this.image, this.canvas.rect, this.point); 
    } 
} 
} 

而且,比遺憾更好的安全,這裏是實例化背景的遊戲類。使用的方法是初始化和創建遊戲對象(背景,hud,zwigs,racquets,twinkys和score)以及在課程結束時刷新顯示的渲染方法。而(用於不同的一個或可能,如果有其他人)運行靜態初始化爲_l1Background

package Game 
{ 
import Game.*; 
import Game.Worlds.Level1.Level1; 

import com.greensock.TweenMax; 
import com.greensock.easing.*; 

import flash.display.Bitmap; 
import flash.display.BitmapData; 
import flash.display.Sprite; 
import flash.display3D.IndexBuffer3D; 
import flash.events.Event; 
import flash.events.MouseEvent; 
import flash.text.TextField; 
import flash.ui.Multitouch; 
import flash.ui.MultitouchInputMode; 

public class Game extends Sprite 
{ 
    /** Hold each racquet so as to retrieve them when collision detection is needed. **/ 
    public var racquetList:Vector.<Racquet>= new Vector.<Racquet>(2,true); 
    /** Hold each Zwig so as to retrieve them when collision detection is needed. **/ 
    public var zwigList:Vector.<Zwig>= new Vector.<Zwig>(2,true); 

    /** Object that contains the coordinates for the Twinkys in the counter. **/ 
    public var twinkyScore0:Object= {firstX:727,firstY:950,secondX:710,secondY:911, 
     thirdX:690,thirdY:872,fourthX:674,fourthY:840, 
     fifthX:657,fifthY:808}; 
    public var twinkyScore1:Object= {firstX:41,firstY:74,secondX:58,secondY:113, 
     thirdX:78,thirdY:152,fourthX:94,fourthY:184, 
     fifthX:111,fifthY:216}; 

    /** Speed decay coefficient. The closer to 1 the less speed decays. **/ 
    private var friction:Number= .96; 

    /** Important positions for the placement of game elements. 
    * LianaHeight is the height at which the liana on the players' HUDs is ending their zone and on which the racquet travels. 
    * TwinkyHeight is the height at which the players stop controlling their Twinkys. 
    * YMargin is the vertical margin for the Twinkys. Used to place them at the end of the tube when added. 
    * XMargin is the horizontal margin for the Twinkys. Used to place them at the end of the tube when added. **/ 
    private var positions:Object= {LianaHeight:165,TwinkyHeight:265,YMargin:8.0,XMargin:200.0}; 

    private var _mRef:ZwigsIpad; 
    private var i:uint; 
    Multitouch.inputMode= MultitouchInputMode.TOUCH_POINT; 

    /** Textfield used for debugging. **/ 
    public var aText:TextField; 

    private var _Canvas:BitmapData= new BitmapData(ZwigsIpad.BORDERS.right,ZwigsIpad.BORDERS.bottom); 
    private var _Background:Background; 
    private var _HUD1:HUD; 
    private var _HUD2:HUD; 
    private var _Zwig1:Zwig; 
    private var _Zwig2:Zwig; 
    private var _Racquet1:Racquet; 
    private var _Racquet2:Racquet; 
    private var _Twinky1:Twinky; 
    private var _Twinky2:Twinky; 
    private var _Score:Score; 

    /** Create the first level. It will create the stage and add the background, HUDs, Zwigs, Racquets and Twinkys, and manages the game until the end. **/ 
    public function Game(m:ZwigsIpad) 
    { 
     this._mRef= m; 
     this.addEventListener(Event.ADDED_TO_STAGE,init,false,0,true); 
    } 

    private function init(e:Event):void 
    { 
     this.removeEventListener(Event.ADDED_TO_STAGE,init); 

     // Text output for debugging the game 
     aText= new TextField(); 
     this.addChild(aText); 
     this.aText.textColor= 0xFF0000; 
     this.aText.width= 384; 
     this.aText.height= 1024; 

     this.aText.appendText("\n added textfield"); 

     // Get informations from Level1 
     // LATER make it dependant from what level was chosen (switch case) 
     this.positions.LianaHeight= Level1.LIANAHEIGHT; 
     this.positions.TwinkyHeight= Level1.TWINKYHEIGHT; 
     this.positions.YMargin= Level1.YMARGIN; 
     this.positions.XMargin= Level1.XMARGIN; 
     this.friction= Level1.FRICTION; 

     this.aText.appendText("\n got level1 infos"); 

     // Add background 
     this._Background= new Background(this._Canvas,0); 

     this.aText.appendText("\n added background"); 

     // Add HUD 
     this._HUD1= new HUD(this._Canvas); 
     this._HUD2= new HUD(this._Canvas,true,1); 

     this.aText.appendText("\n added hud"); 

     // Add zwigs 
     this._Zwig1= new Zwig(this.positions,this._Canvas); 
     this._Zwig2= new Zwig(this.positions,this._Canvas,true,1); 

     this.aText.appendText("\n added zwigs"); 

     // Add racquets 
     this._Racquet1= new Racquet(this,this.positions,this._Canvas); 
     this._Racquet2= new Racquet(this,this.positions,this._Canvas,false,1); 

     this.aText..appendText("\n added racquets"); 

     // Add twinkys 
     this._Twinky1= new Twinky(this._Score,this,this.positions,this.friction,this._Canvas,0); 
     this._Twinky2= new Twinky(this._Score,this,this.positions,this.friction,this._Canvas,1,false,1); 

     this.aText.appendText("\n added twinkys"); 

     // Add scoring 
     this._Score= new Score(this,this._mRef); 
     this.addChild(this._Score); 

     this.aText.appendText("\n added score"); 

     this.addEventListener(Event.ENTER_FRAME,renderLevel); 
    } 

    private function renderLevel(e:Event):void 
    { 
     this._Canvas.lock(); 

     this._Background.render(); 
     this._HUD1.render(); 
     this._HUD2.render(); 
     this._Score.render(); 
     this._Zwig1.render(); 
     this._Zwig2.render(); 
     this._Racquet1.render(); 
     this._Racquet2.render(); 
     this._Twinky1.render(); 
     this._Twinky2.render(); 

     this._Canvas.unlock(); 
    } 
} 
} 
+0

新L1Background()的位圖數據和其他50位圖數據分配需要在「INIT」的方法來實現。您不應該在類聲明區域中進行函數調用。 – 2012-03-26 15:52:58

+0

因爲我有很多工作要做,很少有時間去做,所以我已經讓這一個去做了。我只是在類中導入所需的資源,但會繼續嘗試使其與參考類一起工作。 – Yokai 2012-03-29 09:40:02

回答

1

的錯誤是。

可能是這兩個靜態變量初始化的順序是不確定的。通常,[Embed]用於const而不是var,因此您可以嘗試。

您也可以嘗試將new L1Background().bitmapData移動到實際需要使用背景的位置,而不是靜態。如果你有很多背景,這也將節省一些記憶。

+0

我試着將'var'轉換成'const'並在初始化時移動了'new L1Background()',但沒有成功(儘管內存保存部分總是加號)。 R.as.第6行仍然出現#1009錯誤 – Yokai 2012-03-26 09:23:04

+0

我認爲你需要發佈完整的課程,而不僅僅是該片段。 – 2012-03-26 12:31:06

0

編輯:我太快讀了你的課,這可能不是正確的答案。

所以錯誤#1009會發生這一行:

this.image= new R.L1Background().bitmapData; 

但在另一個類的變量已經是BitmapData,因此它不具有財產.bitmapData - 因此你「不能訪問此屬性」。

public static var _l1Background:BitmapData= new L1Background().bitmapData; 

你可以只說this.image= new R.L1Background();

+0

仍然不是,儘管我會認爲它會伎倆。 我也注意到,而不是'this.image = new R.L1Background();' 它應該是'this.image = R._l1Background;''R._l1Background'就像你之前說的那樣。 – Yokai 2012-03-27 11:25:33