Passing Texture Atlas into FlxBackdrop instead of single image for optimization

52 views
Skip to first unread message

Shaun Michael K. Stone

unread,
Jun 27, 2015, 8:46:40 AM6/27/15
to haxef...@googlegroups.com
Hi all,

So I have a foreground layer and a few background layers. I have packed this all into Sparrow data using Texturepacker as I want to contain everything in one spritesheet. The limitation of FlxBackdrop is that you pass the string of the image you want to repeat over and over:

function new(Graphic:Dynamic, ?ScrollX:Float = 1, ?ScrollY:Float = 1, ?RepeatX:Bool = true, ?RepeatY:Bool = true):Void


HillsOne = new FlxBackdrop("assets/single/HillsOne.png", 1, 0, true, false);        
add
(HillsOne);

I want to pass in the spritesheet rather than the single image, as I do with the game objects:

var textureAtlas:SparrowData = new SparrowData(
   
"assets/sheets/Desktop.xml",
   
"assets/sheets/Desktop.png"
);

this.loadGraphicFromTexture(textureAtlas, false, 'HillsOne');

How can I do this?





Sam Bellman

unread,
Jun 30, 2015, 11:21:29 AM6/30/15
to haxef...@googlegroups.com
I thinks this should work:
var frame:TextureAtlasFrame = null;
for (i in 0...textureAtlas.frames.length)
{
if (textureAtlas.frames[i].name == "bg")
{
frame = textureAtlas.frames[i];
break;
}
}
var textureRegion:TextureRegion = new TextureRegion(FlxG.bitmap.add(texture.asset), Math.floor(frame.frame.left), Math.floor(frame.frame.top), Math.floor(frame.frame.width), Math.floor(frame.frame.height), 0, 0, Math.floor(frame.frame.width), Math.floor(frame.frame.height));
var backdrop:FlxBackdrop = new FlxBackdrop(textureRegion, 1, 1, true, true);

There might be a better, cleaner way of doing it but this is how I did it in a project I had. I was using TexturePackerXMLData but I don't think it should make a difference - you're just creating a Texture region with the asset from the texturepacker and feeding it the frame data then using that in the FlxBackdrop.

Shaun Michael K. Stone

unread,
Jul 4, 2015, 9:56:15 AM7/4/15
to haxef...@googlegroups.com
FlxG.bitmap.add(texture.asset)

Where do you get the texture.asset from?

Sam Bellman

unread,
Jul 4, 2015, 12:09:28 PM7/4/15
to haxef...@googlegroups.com

I think I meant textureAtlas.asset, textureAtlas being a TexturePackerXMLData , so it should be the same as your sparrowData. I didn't copy and paste from my stuff, I was writing on the fly but if that doesn't work I can check up exactly what I did when I get back home.

--
HaxeFlixel Development Community
See our github https://github.com/haxeflixel/ and our documentation http://haxeflixel.com/documentation/
---
You received this message because you are subscribed to a topic in the Google Groups "HaxeFlixel" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/haxeflixel/IVgtrn_u-d8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to haxeflixel+...@googlegroups.com.
Visit this group at http://groups.google.com/group/haxeflixel.
To view this discussion on the web visit https://groups.google.com/d/msgid/haxeflixel/0506692d-ebe4-469a-893f-b63e8a6f8040%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Shaun Michael K. Stone

unread,
Jul 4, 2015, 3:39:25 PM7/4/15
to haxef...@googlegroups.com
Okay, thanks for helping, Sam. This is what I got:

package helpers;
import flixel.addons.display.FlxBackdrop;
import flixel.FlxG;
import flixel.util.loaders.SparrowData;
import flixel.util.loaders.TextureAtlasFrame;
import flixel.util.loaders.TexturePackerXMLData;
import flixel.util.loaders.TextureRegion;

/**
 * @author Shaun Stone (SMKS)
 */
class AtlasImageExtractor
{
public function new() {}
/**
* @param name
* @param textureAtlas
* @return
*/
public static function getImageByName(name:String, textureAtlas:SparrowData):TextureRegion 
{
var frame:TextureAtlasFrame = null;
for (i in 0...textureAtlas.frames.length)
{
if (textureAtlas.frames[i].name == name)
{
frame = textureAtlas.frames[i];
break;
}
}
if (frame == null) {
throw "Frame not found in atlas";
}
return buildTextureRegion(frame);
}
/**
* @param frame
* @return
*/
private static function buildTextureRegion(frame:TextureAtlasFrame):TextureRegion 
{
var texture = new TexturePackerXMLData(
PathAssets.getAtlasXml(),
                        PathAssets.getAtlasAsset()
);
var textureRegion:TextureRegion = new TextureRegion(
FlxG.bitmap.add(texture.asset), 
Math.floor(frame.frame.left), 
Math.floor(frame.frame.top), 
Math.floor(frame.frame.width), 
Math.floor(frame.frame.height), 
0, 
0, 
Math.floor(frame.frame.width), 
Math.floor(frame.frame.height)
);
return textureRegion;
}
}

Sam Bellman

unread,
Jul 5, 2015, 10:22:07 AM7/5/15
to haxef...@googlegroups.com
That looks good - you should be able to pass in textureAtlas:SparrowData to buildTextureRegion and use that instead of creating a new TexturePackerXMLData. They both extend TexturePackerData so your sparrow data will have the asset property which you can use.

Shaun Michael K. Stone

unread,
Jul 5, 2015, 10:38:05 AM7/5/15
to haxef...@googlegroups.com
Ahh of course. Here is the final:

package helpers;

import flixel.addons.display.FlxBackdrop;
import flixel.FlxG;
import flixel.util.loaders.SparrowData;
import flixel.util.loaders.TextureAtlasFrame;
import flixel.util.loaders.TexturePackerXMLData;
import flixel.util.loaders.TextureRegion;

/**
 * This is used to do the FlxBackdrop using an atlas instead
 * of a single image
 * 
 * @author Shaun Stone (SMKS)
 */
class AtlasImageExtractor
{
public function new() {}
/**
* @param name
* @param textureAtlas
* @return
*/
public static function getImageByName(name:String, textureAtlas:SparrowData):TextureRegion 
{
var frame:TextureAtlasFrame = null;
for (i in 0...textureAtlas.frames.length)
{
if (textureAtlas.frames[i].name == name)
{
frame = textureAtlas.frames[i];
break;
}
}
if (frame == null) {
throw "Frame not found in atlas";
}
return buildTextureRegion(frame, textureAtlas);
}
/**
* @param frame
* @return
*/
private static function buildTextureRegion(frame:TextureAtlasFrame, textureAtlas:SparrowData):TextureRegion 
{
var textureRegion:TextureRegion = new TextureRegion(
FlxG.bitmap.add(textureAtlas.asset), 
Reply all
Reply to author
Forward
0 new messages