PDA

View Full Version : Creating sprites from a loop in a class (posted .as files)



mattgyver
January 29th, 2009, 03:36 AM
I’m having a heck of a time drawing sprites based on a loop inside of a class.

My goal is to create anywhere from 1 to 1000 clickable tiles. They will be line/fill and not an existing library movieClip. When I make a package with “public class HexagonTile extends Sprite”, draw and addChild to the tiles, then from a separate .as class loop call “var testHex:HexagonTile = new HexagonTile();”, the tiles don’t get drawn, but there are no errors and the traces come back with correct info.

So the jist of it is: In one class file I run a loop and want to create x number of tiles:

for (var j:Number = 0; j < gameMap[0].length; j++) {
var testHex:HexagonTile = new HexagonTile();
}

So the HexagonTile class needs to draw some graphics and place it all on the stage:


public class HexagonTile extends Sprite {
public var checker:Sprite;
public function HexagonTile():void {
checker = new Sprite();
checker.y = Math.random() * 400 >> 0;
checker.x = Math.random() * 400 >> 0;
checker.graphics.lineStyle(2, 0xFF0000);
checker.graphics.beginFill(0x0000FF);
checker.graphics.drawCircle(10, 10, 100);
checker.graphics.endFill();
addChild(checker);
trace(checker.name);
}
}


The above code has no errors and traces the checker names fine, so everything is connected right I think, just no drawings.

Here is a link to my 2 class .as files, and the main fla (which is only used to trigger the class). AS3 is a pain. http://mattgyver.com/HexagonBoard.zip I've also attached it.

Thanks!

Krilnon
January 29th, 2009, 04:00 AM
The problem is that you are not actually adding any of the tiles to the stage. Although you have this code:
// be sure to add it to the display list
addChild(checker);

…the 'fatal' flaw is that addChild will only add the supplied DisplayObject as a child of the method's implicit argument (this, in this case). So, although you have added the checker as a child of the tile, you haven't made the tile a child of anything. In other words, you haven't actually put it on Flash Player's display list.

The way to make sure that your objects are on the display list is to either add them directly to the stage (probably not the most organized way to do it, and you won't have access to the stage until you give your class access), add them from the document class (which is the one that is, in your case, auto-generated by Flash based on the timeline code that you have), or add them from a child of the document class.

There are loads of resources out there that are more than sufficient to teach you the details of all of this, so I'll leave it at that.

However, here's a quick way to get things to start showing up in your project:

HexagonBoard.fla, line 24:
addChild(hexBoard);

HexagonTile.as, line 9:
public class HexagonTile extends Sprite {

HexagonBoard.as, line 37:
testHex.x = Math.random() * 200;
testHex.y = Math.random() * 200;
addChild(testHex);

mattgyver
January 29th, 2009, 11:11 AM
I'm just having trouble changing my thought process from AS2 to AS3. Thanks for the help! Here's all the code in the class (this works).



package {

import flash.events.*;
import flash.display.*;

public class HexagonBoard extends Sprite {

public var mapWidth:int;
public var mapHeight:int;
public var gameMap:Array = new Array();
public var hexTile:Sprite;

// -------------------------------------------------------------------------------------------
// build the hexagon board : receives an array of data and fills the map terrain
public function HexagonBoard(terrainArray:Array):void {
gameMap = terrainArray;
mapWidth = terrainArray[0].length;
mapHeight = terrainArray.length;
// create HexagonTiles
HexagonTiles();
} // public function HexagonBoard

// -------------------------------------------------------------------------------------------
// build the hexagon board : receives an array of data and fills the map terrain
public function HexagonTiles():void {

for (var i:int = 0; i < mapWidth; i++) {
for (var j:int = 0; j < mapHeight; j++) {
//trace(i,j);

// create a new Sprite for the checker
hexTile = new Sprite();
hexTile.x = Math.random() * 500 >> 0;
hexTile.y = Math.random() * 500 >> 0;

// draw checker in checker sprite's graphics
hexTile.graphics.lineStyle(1, 0xFFFFFF, 1, true);
hexTile.graphics.beginFill(0x0000FF);
hexTile.graphics.drawCircle(10, 10, 10);
hexTile.graphics.endFill();

// be sure to add it to the display list
addChild(hexTile);
//trace(checker.name);

}

}

} // public function HexagonTiles

} // public class HexagonBoard

} // package