Hellbound Heart
Hellbound Heart

Reputation: 17

as3 creating a custom property in Emanuele Feronato's "Flash Game Development by Example"

I recently picked up Emanuele Feronato's Flash Game Development by Example to try and help expand my knowledge of game design and actionscript 3, and I'm stuck on the first chapter where we're building basically a memory match two game where you have ten sets of tiles and you try and match them up by clicking on them.

In the code I'm stuck on, Mr. Feronato is adding the tiles to the stage using a for loop and addChild, here's the code in particular:

// tile placing loop
for (i:uint=0; i<NUMBER_OF_TILES; i++) {
tile = new tile_movieclip();
addChild(tile);
tile.cardType=tiles[i];
tile.x=5+(tile.width+5)*(i%TILES_PER_ROW);
tile.y=5+(tile.height+5)*(Math.floor(i/TILES_PER_ROW));
tile.gotoAndStop(NUMBER_OF_TILES/2+1);
tile.buttonMode = true;
tile.addEventListener(MouseEvent.CLICK,onTileClicked);
}
// end of tile placing loop

In the 5th line, you can see that he creates a custom property of the tile variable called "cardType," but when I try and run the code I get the error "Access of possibly undefined property cardType through a reference with static type Tile." I have the class Tile extending MovieClip, and the main class extends Sprite, but as far as I can tell I've written the code exactly as in the book and can't get past this. I thought about just using a normal int variable cardType to hold tiles[i] but later on you use the cardType property on a mouse event so I'm a little stuck.

Has something changed in Flash that no longer allows you to create custom properties in this way? Or did I just do something stupid that I'm not catching.

As always, thank you so much for the help.

Upvotes: 1

Views: 306

Answers (1)

Jason Sturges
Jason Sturges

Reputation: 15955

ActionScript supports dynamic classes, in which properties may be added during runtime. Without the dynamic keyword, a class is sealed, meaning its properties may not be altered.

MovieClip is an example of a dynamic class.

Instantiating a MovieClip from code (or MovieClip instance created in Flash Professional), adding this property would be supported:

var tile:MovieClip = new MovieClip();
tile.cardType = "custom value";

However from code, even if extending MovieClip you must add the dynamic keyword:

package {

    import flash.display.MovieClip;

    public dynamic class Tile extends MovieClip {
        /* ... */
    }
}

Now, you may add properties to your Tile instance:

var tile:Tile = new Tile();
tile.cardType = "custom value";

Upvotes: 1

Related Questions