[SOLVED] Using string to load a new world?


(Bora Kasap) #1

I’m trying to implement getDefinitionByName function for to change world but i’m stuck…

var targetworld:Class = getDefinitionByName("SECTOR_41") as Class;
FP.world = new targetworld() as World;

in this, throws an error on SECOND LINE, variable SECTOR_41 not defined… but i’ve that class and it works when using new SECTOR_41();

if i don’t use FP.world = new targetworld() as World; function, not throwing an error…

i’ve tried some other ways but always throwing this error…


(Alex Larioza) #2

How are you setting the current world?


(Bora Kasap) #3

current world is MAP world, so i’m setting it by clicking New Game or Continue Game then GLOBALS.gotoMap();

GLOBALS is static class…

public static function gotoMap():void
{
FP.world = new Map();
}

so i want to use this in globals…

public static function gotoLevel(levelname:String):void
{
var targetworld:Class = getDefinitionByName(levelname) as Class;
FP.world = new targetworld() as World;
}

but not working so i’m using this one…

public static function gotoLevel(levelname:String):void
	{
		switch (levelname)
		{
			case "SECTOR_41":
				FP.world = new SECTOR_41();
			break;
			case "CELL_271":
				FP.world = new CELL_271();
			break;
			case "AURA_44":
				FP.world = new AURA_44();
			break;
			case "SECTOR_33":
				FP.world = new SECTOR_33();
			break;
			case "STAR_13":
				FP.world = new STAR_13();
			break;
			case "STAR_28":
				FP.world = new STAR_28();
			break;
		}
	}

(Alex Larioza) #4

Try importing your world classes in the fie where you have the gotoLevel function.


(Bora Kasap) #5

already imported… and get definition doesn’t throw any errors… the error is throwing when i use new targetworld();


(Alex Larioza) #6

Yes but getDefinition has some strange caveats to it. I’ve always gotten around the undefined issue by making sure the classes were imported.

You can also try passing straight classes too, and changing your gotoLevel to look like this:

public static function gotoLevel(level:Class):void
{
    FP.world = new level();
}

Do you need to pass levels as strings?


(Bora Kasap) #7

actually, i don’t have to do that, but i have to learn that… because it is a great way to control code flow…


(azrafe7) #8

Faced the same issue when testing punk.fx and found a solution in an article on StackOverflow (of which I lost track).

It’s a quirk of AS3, but basically you have to declare the needed classes (importing them will not suffice - actually not even needed with this approach) so that the compiler is aware about them.

Ex.:

public class LevelManager
{
	Level_23;
	Cell_666;

	...

	...
		var levelName:String = "Cell_666";
		var level:Class = Class(getDefinitionByName(levelName));
		FP.world = new level();
	...
}

(Bora Kasap) #9

not enough to feel satisfied…


(azrafe7) #10

?

Why not?

You can write a little script to gather all the Level names and copypaste them in your Globals.

EDIT: If you’re on windows copypaste this into “getLevels.bat” and put it in the folder containing your levels:

@echo off
echo. 2> levels.txt
for %%i in (*.as) do (
  echo %%~ni;
  echo %%~ni; >> levels.txt
)
echo.
echo written in "levels.txt"

When you run it a file named “levels.txt” will be generated with the names of all the .as files in that folder.


(Bora Kasap) #11

because it is not great anymore…

i’m using a folder containing level classes and importing them to GLOBALS.as as package by the way it works when i use new level1();

so, if i already have a package containing level clases, then why am i have to make a list of levels names’ again… thats weird, so that doesn’t look like the best way to me, i feel there is a way to do that easier…

EDIT about “thats weird”… thats not weird, that’s the only way to compile a project, i got it now…


(azrafe7) #12

http://www.rozengain.com/blog/2009/08/21/getdefinitionbyname-referenceerror-and-the-frame-metadata-tag/


(Zachary Lewis) #13

I put together a video tutorial on this kind of thing. Here’s the end effect. You can start from the beginning to learn about how I did things.


(Bora Kasap) #14

hellnooo… :frowning: then your suggestion is the best solution about that, thanky you very much for help, i’m using windows…

also thanks for your great tutorial :] i haven’t used ogmo editor before, or xml by the way… but i’ll learn in the future…

i’ve made my own level editor inside my project, it looks like ogmo… but because of my game doesn’t have lots of tiles, no need for ogmo right now…

    if (this.repeater == 0)
{
	GLOBALS.corecount = 1;
	this.tankcount = 0;
	
	this.group =
	  "11111111110001"
	+ "10000000000001"
	+ "10000000000001"
	+ "10000000000001"
	+ "10001111111111"
	+ "10000000000001"
	+ "10000000000201"
	+ "10000000000001"
	+ "11111111111111";
	
	this.row = 1;
	this.groupwidth = 14;
	this.xspace = 25;
	this.yspace = -25;
	this.bornx = FP.halfWidth - ((this.groupwidth - 1) * this.xspace / 2);
	this.borny = this.yspace - 100;
	
	for (this.col = 1; this.col < this.group.length + 1; this.col++)
	{
		switch (this.group.charAt(this.col - 1))
		{
			case "1":
				Enemy(create(Enemy)).init("tank", this.bornx, this.borny, 50, [0], [0], [0]);
				this.tankcount++;
			break;
			
			case "2":
				Enemy(create(Enemy)).init("core", this.bornx, this.borny, 50, [0], [0], [0]);
			break;
		}
		
		if (this.col % this.groupwidth == 0)
		{
			this.borny += this.yspace;
			this.bornx = FP.halfWidth - ((this.groupwidth - 1) * this.xspace / 2);
		}
		else
		{
			this.bornx += this.xspace;
		}
	}
}

(Jacob Albano) #15

For what it’s worth, none of my games use tiles at all and I use Ogmo for everything. It has a lot of flexibility and works quite well when you’re working with entities.

I used to do it the way you do (laying out levels in code) and it’s really pretty terrible for iteration because of the amount of time you waste between making changes and seeing them in action. If you use code to design levels, you have to recompile the game every time you make a change (which can get super slow!), whereas with data files and dynamic loading you can set it up to reload changes to your levels without even closing the SWF, in addition to cutting compile times way down when you do have to make a change in code.


(Bora Kasap) #16

you’re absolutely right….

but game is so complicated even for me, firstly, the numbers in editor you see as 1 and 2, they are not tiles, i’m working on something different, think like live tiles

sometimes enemies(tiles) spawning in different positions then coming together, or they are spawning together as a group then they are splitting around with given orders or a mix of both etc

(i’ve set video playback start time 3:33, take a looK)

so i mean, i’m affraid of using ogmo editor because of i can’t see the future, what if things’ll be “more complicated” than “not using ogmo” in the future (for this game)


(Jacob Albano) #17

Well, just because you set something up as tiles in Ogmo doesn’t mean they have to use Tilemaps in the game. :wink: Ogmo can output tilemaps as strings just like you have above, and you can then take those strings and loop through them just like you’re doing now.


(Bora Kasap) #18

it looks like i’m lightening about ogmo right now…


(Bora Kasap) #19

And now, i’ve realized that i can use ogmo very well for my game with Flashpunk’s QuadPath, when i mix ogmo and quadpath, it looks like everything’ll work great…also maybe i won’t need to use any paths, thank you very much!!!

i’m curious about something… when i finish my project, how i’ll import this files into the compiled swf file?


(Bora Kasap) #20

oh, i think i don’t need to use ogmo for this game… but xml’ll be great to check without recompiling lots of times… :slight_smile: