The basic method of making new instances is to write out
var t1:MyClass = new MyClass( 0 );
var t2:MyClass = new MyClass( 0 );
etc
I know there is a much quicker way to do this than writing them out, say if I want to go up to t20, but I can’t seem to quite remember how.
Thanks for help, this should be a quick fix.
var tArray:Array = new Array();
var MyTemp:MyClass;
for(var i:int = 0;i<20;i++){
MyTemp = new MyClass(0);
tArray(i) = MyTemp;
tArray(i).name = "t" & i+1;
}
(probably lots of typos, don’t have flash at work to check)
Basically with that you can reference all your objects that you create directly with the array. The name is not even necessary.
Thanks Carlo, but I keep getting an error when I try your method. Here’s the whole code, maybe someone can see what’s wrong.
BTW, the error is: [Fault] exception, information=TypeError: Error #1009: Cannot access a property or method of a null object reference.
Fault, Main.as:39
package {
import flash.display.Sprite;
public class Main extends Sprite {
public var tiles:Array;
public var t_name:String;
private var map:Array = [
[ 0, 0, 0, 0, 0 ],
[ 0, 1, 1, 1, 0 ],
[ 0, 1, 1, 1, 0 ],
[ 0, 1, 1, 1, 0 ],
[ 0, 0, 0, 0, 0 ]
];
public function Main():void {
initTiles();
var t1:Tile = new Tile( 5 );
}
public function initTiles():void {
var tempTile:Tile;
for ( var i:int = 0; i < map.length; i++ ) {
for ( var j:int = 0; j < map[0].length; j++ ) {
tempTile = new Tile( map*[j] )
trace( map*[j] )
tiles*[j] = tempTile;
}
}
for ( i = 0; i < map.length; i++ ) {
for ( j = 0; j < map[0].length; j++ ) {
trace( tiles*[j] )
}
}
}
}
}
class Tile {
public var walkable:Boolean;
public var pic:int;
public function Tile( setup:int ) {
pic = setup;
walkable = ( setup > 50 );
}
public function getWalkable():Boolean {
return walkable;
}
}
It looks like the problem is more confusing than I thought. Will someone please tell me why this does not work, because I am at my wit’s end.
package {
import flash.display.Sprite;
public class Main extends Sprite {
public var tiles:Array;
public function Main():void {
var t1:Tile = new Tile( 5 );
tiles.push( t1 );
}
}
}
class Tile {
public var walkable:Boolean;
public var pic:int;
public function Tile( setup:int ) {
pic = setup;
walkable = ( setup > 50 );
}
public function toString():String {
return "#=" + pic + " || walkable=" + walkable
}
}
I get error 1009 even for this seemingly simple piece of code.