Yokai
Yokai

Reputation: 3

Actionscript 3 error #1009 at global$init()

So I have searched, found some similar errors but unfortunately no resolution to the problem. I am writing an iPad game, and for optimisation I have created an assets reference class called R, following the example of a tutorial from gotoAndLearn.

My error fires when I try to access the first static BitmapData from R, and oddly points to the class declaration line:

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]

The code of R at line 6 is this:

public final class R

For better visibility, here is the complete declaration of R and the first lines, the ones that I try to access:

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;

The following of the class being mostly the same thing repeating over and over to load all the assets. Also, here is the code which calls for R._l1Background. It is the initialisation of the Background class.

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;
        }
    }

If I try commenting the access to R._l1Background (thus making the latter function do nothing) I still get similar errors when other functions try accessing R's properties. I might (most likely) have other errors in my code, but for the moment this is keeping me from debugging the rest of the game.

I will ask my teachers about this and forward any lead or solution they come up with, as well as the results of my own researches. Thanks in advance for any help.

Edit: after a request, here are my complete classes

My complete reference class (R class) is as follows:

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);
}
}

The Background class, which tries to stock the asset to use in a property, is like this:

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);
    }
}
}

And, better safe than sorry, here is the Game class which instantiates the Background. The methods used are the ones to initialise and create the game objects (background, hud, zwigs, racquets, twinkys and score), and the rendering method to refresh the display at the end of the class.

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();
    }
}
}

Upvotes: 0

Views: 1034

Answers (2)

Miha
Miha

Reputation: 301

Edit: I read your class too quickly, this is probably not the correct answer.

So the error #1009 happens this line:

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

But in another class your variable is already of BitmapData, so it doesn't have property .bitmapData -- hence you "cannot access this property".

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

You could just say this.image= new R.L1Background();

Upvotes: 0

Peter Hall
Peter Hall

Reputation: 58825

The error is while running the static initializer for _l1Background (or possibly for a different one if there are others).

It could be that the order that the two static variables are inititialized is indeterminate. usually, the [Embed] is used on a const instead of a var, so you could try that.

You could also try moving the new L1Background().bitmapData to the point where you actually need to use the background, instead of statically. That will also save some memory if you have a lot of backgrounds.

Upvotes: 1

Related Questions