Author Topic: Funky code in FlxSprite::loadGraphic()  (Read 3006 times)

IQAndreas

  • Member
  • **
  • Posts: 35
  • Karma: +0/-0
    • View Profile
    • IQAndreas.com
Funky code in FlxSprite::loadGraphic()
« on: Wed, Jan 18, 2012 »
My brain is a tad slow this morning. Could someone help me decode what is going on in "FlxSprite::loadGraphic"?

It's doing something funny in the highlighted areas where it's using "width" and "height" interchangeably:
Code: [Select]
if(Reverse)
_flipped = _pixels.width>>1;
else
_flipped = 0;
if(Width == 0)
{
if(Animated)
Width = _pixels.height;    // <------------------
else if(_flipped > 0)
Width = _pixels.width*0.5;
else
Width = _pixels.width;
}
width = frameWidth = Width;
if(Height == 0)
{
if(Animated)
Height = width;    // <------------------
else
Height = _pixels.height;
}
height = frameHeight = Height;
Plus, what happens if both "Reverse" and "Animated" are set to true?

wg/funstorm

  • Global Moderator
  • Key Contributor
  • *****
  • Posts: 596
  • Karma: +0/-0
    • View Profile
    • Funstorm
Re: Funky code in FlxSprite::loadGraphic()
« Reply #1 on: Wed, Jan 18, 2012 »
If the programmer did not specify a width or height, but wants the sprite to be animated, Flixel assumes that each frame of the animation is a square that is as wide & high as the sprite is height.

Basically it's making a best guess at the dimensions of each frame in the animation. It doesn't have much information to base this guess on, so it assumes that the sprite sheet is only 1 row and that's why it uses the height to determine the frame width & height (because it assumes the sprite sheet consists of only 1 row but several columns). This will work fine if you sprite sheet is only 1 row and the frames are square, but won't work if it's multiple rows or your frames are not square. In that case you have to provide flixel with the width/height cause there's no way it can guess it.

Hope that makes sense :P
« Last Edit: Wed, Jan 18, 2012 by wg/funstorm »

IQAndreas

  • Member
  • **
  • Posts: 35
  • Karma: +0/-0
    • View Profile
    • IQAndreas.com
Re: Funky code in FlxSprite::loadGraphic()
« Reply #2 on: Wed, Jan 18, 2012 »
Ah, got it! ;D

And just to double check, if the graphic is animated and flipped, is the new sprite sheet generated in such a fashion? (Imagine this is a sprite sheet)
[1][2][3][4]

Generates to (where "1r" is the flipped version of frame 1 etc)
[1][2][3][4][1r][2r][3r][4r]

(I don't currently have a sprite sheet or project to test this on)

Wing Eraser

  • Guest
Re: Funky code in FlxSprite::loadGraphic()
« Reply #3 on: Wed, Jan 18, 2012 »
The generation is before that. Take a look at FlxG.addBitmap.

Code: [Select]
var newPixels:BitmapData = new BitmapData(pixels.width<<1,pixels.height,true,0x00000000);
newPixels.draw(pixels);
var mtx:Matrix = new Matrix();
mtx.scale(-1,1);
mtx.translate(newPixels.width,0);
newPixels.draw(pixels,mtx);
pixels = newPixels;

And then the post of wf/funstorm makes your question complete.

IQAndreas

  • Member
  • **
  • Posts: 35
  • Karma: +0/-0
    • View Profile
    • IQAndreas.com
Re: Funky code in FlxSprite::loadGraphic()
« Reply #4 on: Thu, Jan 19, 2012 »
Ah, like this?
[1][2][3][4]

Generates to (where "1r" is the flipped version of frame 1 etc)
[1][2][3][4][4r][3r][2r][1r]

Thanks guys, I really shouldn't be staring at code on days when I can't think. ;)

test84

  • Key Contributor
  • *****
  • Posts: 1328
  • Karma: +0/-0
  • ت
    • View Profile
    • My personal site.
Re: Funky code in FlxSprite::loadGraphic()
« Reply #5 on: Thu, Jan 19, 2012 »
umm, I think you should a little more on the subject but you are on the right track ;)
blog, twitter, Check out my award winning game, Rot Gut:

IQAndreas

  • Member
  • **
  • Posts: 35
  • Karma: +0/-0
    • View Profile
    • IQAndreas.com
Re: Funky code in FlxSprite::loadGraphic()
« Reply #6 on: Fri, Jan 20, 2012 »
umm, I think you should a little more on the subject but you are on the right track ;)
I must be really slow this week... but are you sure?

First it draws the "initial frames", then it draws the same frames reflected across the x axis and moved so it's on the right.


Also, if a moderator could move this to the "help" section. This was all an embarrassing error in thinking on my part, not a problem with the released code. ;)