1

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.

1 Answers1

1

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";
Jason Sturges
  • 15,855
  • 14
  • 59
  • 80
  • Very interesting, thank you Jason that works perfectly and makes a lot of sense, thank you for explaining. Just out of curiosity is that just an error in his code, or is just something that was changed in Flash since the book was written? – Hellbound Heart Jul 03 '14 at 15:23
  • In AS3 classes are sealed by default; therefore, require the dynamic modifier. From Emanuele Feronato's Flash Game Development by Example book, `tile_movieclip` is a MovieClip symbol created in Flash Professional. Perhaps you are expanding upon his examples by creating classes for linkage; or, perhaps its just errata in the example. – Jason Sturges Jul 03 '14 at 15:50
  • You created a specific class and linked it with your movieclip while feronato only added a as3 export name to his movieclip making it be default dynamic. Note that feronato tutorials teach functionality not proper convention. Using dynamic property is mostly good for getting the job done in a quick way, it is not by far the way app should be developed. – BotMaster Jul 04 '14 at 15:29