Would anyone be willing to help me with some Actionscript?

Started by Nine Toes, Sat 01/05/2010 14:57:07

Previous topic - Next topic

Nine Toes

I'm trying to translate this script (ActionScript 2) to ActionScript 3 - which, it seems, is quite a mental workout, especially if you don't know much ActionScript to begin with.

Here is what I have so far (slightly modified):
Code: ags

var tileMap001:Array = new Array();
tileMap001 = [
	[  0,   0,   0,   0,   0,   0,   0,   0,   0,  0],
	[  0, 101, 101, 101, 101, 101, 101, 101, 101,  0],
   	[  0, 101, 101, 101, 101, 101, 101, 101, 101,  0],
	[  0, 101, 101,   0, 101, 101, 101, 101, 101,  0],
	[  0, 101, 101, 101, 101, 101, 101, 101, 101,  0],
	[  0, 101, 101, 101, 101, 101, 101, 101, 101,  0],
	[  0, 101, 101, 101, 101, 101,   0, 101, 101,  0],
	[  0, 101, 101, 101, 101, 101, 101, 101, 101,  0],
	[  0, 101, 101, 101, 101, 101, 101, 101, 101,  0],
	[  0,   0,   0,   0,   0,   0,   0,   0,   0,  0]
	];

var game = {tileW:32, tileH:32};
game.Tile1 = function(){};
game.Tile1.prototype.walkable = false;
game.Tile1.prototype.frame = 1;
game.Tile101 = function(){};
game.Tile101.prototype.walkable = true;
game.Tile101.prototype.frame = 101;

var tileSet001:mcTileSet001 = new mcTileSet001();

function buildMap(map:Array, tileSet:MovieClip){
	var emptyField:mcEmptyField = new mcEmptyField();
	this.addChild(emptyField);
	game.clip = this.emptyField;
	var mapWidth:Number = map[0].length;
	var mapHeight:Number = map.length;
	for (var i = 0; i < mapHeight; ++i){
		for (var j = 0; j < mapWidth; ++j){
			var tileID:String = new String();
			tileID = "tile_" + i.toString() + "_" + j.toString();
			trace("TileID '" + tileID + "' created."); //for debugging purposes
			
			// ...I've narrowed the problem down to the next 5 lines of code... 
                        // ...everything else works just fine until you remove the comment block from the next line only...
			/*game[tileID] = new game["Tile"+map[i][j]];
			game.clip.addChild(tileSet);
			game.clip[tileID].x = (j * game.tileW);
			game.clip[tileID].y = (i * game.tileH);
			game.clip[tileID].gotoAndStop(game[tileID].frame);*/
		}
	}
}

buildMap(tileMap001, tileSet001);
stop();


If I uncomment just the first line of that block of code, I get an error in the output:
TileID 'tile_0_0' created.
TypeError: Error #1007: Instantiation attempted on a non-constructor.
   at flashisogame_fla::MainTimeline/buildMap()
   at flashisogame_fla::MainTimeline/frame2()
   at flash.display::MovieClip/gotoAndStop()
   at flashisogame_fla::MainTimeline/startGame()


Most of what I've translated, I learned all on my own through research and trial and error.  It's just that one little block; I wouldn't even know what to search for.  I know it works perfectly in AS2, but I'm learning, and I'm trying to keep it current.  The ActionScript part of my Flash class only covers the basics.

I'm pretty sure I shouldn't be using "game" or "game.clip" like an array... but I just don't know why, or how it can be fixed.

So, I was wondering if someone could kind of explain to me what is going on there, and toss me a few hints as to how it should be scripted?
Watch, I just killed this topic...

fred

I made it work in AS3 like this, slightly optimized in places:

Code: ags

stop();

var tileW:uint = 32;
var tileH:uint = 32;
var tileSet001:String = "mcTileSet001";
var tileMap001:Array = [
	[  0,   0,   0,   0,   0,   0,   0,   0,   0,  0],
	[  0, 101, 101, 101, 101, 101, 101, 101, 101,  0],
   	[  0, 101, 101, 101, 101, 101, 101, 101, 101,  0],
	[  0, 101, 101,   0, 101, 101, 101, 101, 101,  0],
	[  0, 101, 101, 101, 101, 101, 101, 101, 101,  0],
	[  0, 101, 101, 101, 101, 101, 101, 101, 101,  0],
	[  0, 101, 101, 101, 101, 101,   0, 101, 101,  0],
	[  0, 101, 101, 101, 101, 101, 101, 101, 101,  0],
	[  0, 101, 101, 101, 101, 101, 101, 101, 101,  0],
	[  0,   0,   0,   0,   0,   0,   0,   0,   0,  0]
	];

function buildMap(map:Array, tileSet:String){
	var emptyField:mcEmptyField = new mcEmptyField();
	this.addChild(emptyField);
	var mapWidth:uint = map[0].length;
	var mapHeight:uint = map.length;
	for (var i:uint = 0; i < mapHeight; ++i){
		for (var j:uint = 0; j < mapWidth; ++j){			
			var ClassReference:Class = getDefinitionByName(tileSet) as Class;
      		        var t = new ClassReference(); 
			if (tileMap001[i][j] == 0){
				t.walkable = false;
				t.defaultFrame = 1;
			} else {
				t.walkable = true;
				t.defaultFrame = 10;
			}
			t.x = j * tileW; 
			t.y = i * tileH;
			t.uniqueID = (i*tileH)+j;
                        t.uniqueIDString = "tile_" + i + "_" + j;
			trace (t.uniqueIDString);//debug trace
			t.gotoAndStop(t.defaultFrame);
			emptyField.addChild(t);
		}
	}
};

buildMap(tileMap001, tileSet001);




SMF spam blocked by CleanTalk