2012-03-26 96 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