Texture atlas using singleton issue. #2759
-
Hi, I want to load my atlases using singleton: as mentioned in this article https://www.ohsat.com/tutorial/flixel/using-texture-atlas/ import flixel.graphics.frames.FlxAtlasFrames;
class TpSingleton {
public static final instance:TpSingleton = new TpSingleton();
private var tp = new Map<String, FlxAtlasFrames>();
private function new () {
tp["tploc1"] = FlxAtlasFrames.fromTexturePackerJson("assets/images/atlas/tploc1.png", "assets/data/atlas/tploc1.json");
trace(tp);
}
public function getTP(name:String):FlxAtlasFrames {
var ret = tp[name];
if (ret == null)
throw "TP doesn't exist!";
return ret;
}
} When I call, FlxAtlasFrames.fromTexturePackerJson("assets/images/atlas/tploc1.png", "assets/data/atlas/tploc1.json"); directly without the singleton it works flawless. But with the singleton im getting: source/TpSingleton.hx:13: {tploc1 => null} Is there any way i could fix this? Thanks in advance. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
My guess is that it has something to do with when fromTexturePackerJson is being called. with the singleton above it is called whenever TPSingleton is first referenced, which may be before flixel is even created. if you had more control over it's construction, by having a static it's also worth mentioning that graphics and bitmaps are destroyed on state switches to clear up memory, that likely isn't the cause here but its something you'll need to think about as it will render cached atlases useless with the way you're doing it. you could set `the atlas's graphic to avoid being destroyed. combining both these recommendations would look something like this: import flixel.graphics.frames.FlxAtlasFrames;
class TpSingleton {
public static final instance:TpSingleton = null;
private var tp = new Map<String, FlxAtlasFrames>();
private function new () {
add("tploc1", "assets/images/atlas/tploc1.png", "assets/data/atlas/tploc1.json");
trace(tp);
}
public static function init() {
instance = new TpSingleton();
}
public function add(id:String, imagePath:String, dataPath:String) {
final atlas = FlxAtlasFrames.fromTexturePackerJson(imagePath, dataPath);
atlas.parent.persists = true;
atlas.parent.destroyOnNoUse = true;
tp[id] = atlas;
}
public function getTP(name:String):FlxAtlasFrames {
var ret = tp[name];
if (ret == null)
throw "TP doesn't exist!";
return ret;
}
} I'm also just not a fan of these singleton image caches. flixel aready has a caching and graphic lookup and these systems tend to cause more issues than they solve |
Beta Was this translation helpful? Give feedback.
My guess is that it has something to do with when fromTexturePackerJson is being called. with the singleton above it is called whenever TPSingleton is first referenced, which may be before flixel is even created. if you had more control over it's construction, by having a static
init
orcreate
method, you may have better luck.it's also worth mentioning that graphics and bitmaps are destroyed on state switches to clear up memory, that likely isn't the cause here but its something you'll need to think about as it will render cached atlases useless with the way you're doing it. you could set `the atlas's graphic to avoid being destroyed.
combining both these recommendations would look somet…