1

XML File:

<MapDetails>
<MapTiles TileWidth="16" TileHeight="16">TestMap1Tiles</MapTiles>

<MapTileFile>TestMap1</MapTileFile>

<Enemy1Class>SkellMonsta</Enemy1Class>

Super simple. I want to use an xml file to define everything in the map. I have the xml file loaded properly in my GameState class. Everything traces out fine. (Using Flixel, btw).

add(map.loadMap(new TestMap1, TestMap1Tiles, mapXML.MapTiles.@TileWidth, mapXML.MapTiles.@TileHeight));

Could not use mapXML.MapTileFile to reference TestMap1. I get this if I try:

Error #1007: Instantiation attempted on a non-constructor.

Same thing happens if I try to reference the SkellMonsta class like this:

enemies.add(mapXML.Enemy1Class = new mapXML.Enemy1Class(100, 40, hero));

I've tried using - as Enemy - but that doesn't work. What am I missing here?

1 Answer 1

3

Your issue is that you are trying to use a string value from your xml like it's a class.

You first need gain a reference to the actual class. You can do this with flash.utils.getDefinitionByName

This method will take a string reference and return a Class reference. Keep in mind though, that it wants a fully qualified name, so it should include any packages references. So in other words, flash.display.Sprite instead of Sprite.

So for your example, assuming your Enemy classes are top level, you could do:

var cls:Class = getDefinitionByName(getmapXML.Enemy1Class) as Class;

enemies.add(new cls(100, 40, hero));
Sign up to request clarification or add additional context in comments.

9 Comments

Tried this and I keep getting Error #1065: Variable SkellaMonsta is not defined. I've tried to reference it every way that I know how. com.Actors.Enemies.SkellMonsta ... ../src/com/Actors/Enemies/SkellMonsta ... I even placed a test duplicate in the com folder(where this state class is located) and same thing.
Been playing with it here. If I do var cls:SkellMonsta = getDefinitionByName("com.Actors.Enemies." + mapXML.Enemy1Class) as SkellMonsta; by itself - no errors. If I add enemies.add(new cls(100, 40, hero)); I get: Call to a possible undefined method cls.
So I used the Enemy class path, which SkellMonsta extends. It's a level higher than SkellMonsta and has default graphics and all. It works! My generic enemy class shows, but skellmonsta is still an issue. seems like it just refuses to resolve the filepath to skellmonsta. What is my problem here? I do have them both imported. com.Actors.Enemies.SkellMonsta -- doesn't work. com.Actors.Enemy -- works
So, your SkellMonsta class is located in the com folder? what does the first line say? package com {? If so, you'd do com.SkellMonsta as the parameter to getDefinitionByName
Or better yet, try this and see what the output is: trace(getQualifiedClassName(SkellMonsta)); If it doesn't error, then the result is what you'll want to pass to getDefinitionByName. If it does error, then it means that class isn't actually compiled/available to your program
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.