私は検索しましたが、いくつかの同様のエラーが見つかりましたが、残念ながら問題は解決されていません。私はiPadゲームを作成しています。最適化のために、gotoAndLearnからのチュートリアルの例に従って、Rというアセット参照クラスを作成しました。
マイエラー火災Iが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]
ライン6におけるRのコードは次のとおりです
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のプロパティにアクセスしようとしたとき、私はまだ同様のエラーを取得します(したがって、後者の機能は何もしない作る)R._l1Backgroundへのアクセスをコメントしよう。私のコードには(おそらく)他のエラーがあるかもしれませんが、今は残りのゲームをデバッグしないようにしています。
私は先生にこれについて尋ね、彼らが思いついた鉛や解決策、そして私自身の研究の結果を送ります。助けを前にありがとう。
編集:要求した後、ここに私の完全なクラスがある
私の完全なリファレンスクラス(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);
}
}
背景クラス、プロパティでは、次のようになります:
そして、より良いよりも安全です、ここで背景をインスタンス化するゲームクラスです。使用されるメソッドは、ゲームオブジェクト(背景、ハド、zwig、ラケット、ツインキー、スコア)を初期化して作成するメソッドと、クラスの終わりに表示を更新するレンダリングメソッドです。 (他の人が存在する場合、別のいずれかの可能性又は)_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();
}
}
}
新しいL1Background()。のBitmapDataと他の50個のBitmapDataの割り当てが "INIT" 方式で行われる必要があると言えるでしょう。クラス宣言エリアで関数呼び出しを行うべきではありません。 –
私は、これをやる時間がほとんどなく、やるべきことがたくさんあるので、私はこれをただちに行うようにしました。私はクラス内で必要なアセットをインポートするだけですが、参照クラスで動作させるようにしていきます。 – Yokai