Author Topic: Converting Sprite to FlxSprite  (Read 1506 times)

carlflor

  • Member
  • **
  • Posts: 13
  • Karma: +0/-0
    • View Profile
Converting Sprite to FlxSprite
« on: Thu, Apr 28, 2011 »
HI! is there a way I can convert Sprite to a FlxSprite?

Basically used a URLRequest for my swf image and made it into a Sprite (for my background image...)
now, when I use addChild or addChildAt, it prints on top of the whole screen.

I think I should use add() function in my PlayState for it to work. But then, add() only asks for a FlxSprite. Is there a way I can solve this?

Thanks!!

Adam Atomic

  • Founder
  • Key Contributor
  • *****
  • Posts: 852
  • Karma: +0/-0
  • new dad
    • View Profile
    • Adam Atomic
Re: Converting Sprite to FlxSprite
« Reply #1 on: Thu, Apr 28, 2011 »
this is a bit of a crazy process, but very doable.  what you have to do (i think) is create a new FlxSprite the same size as the Sprite, then get a reference to the FlxSprite's associated bitmapdata with something like:

Code: [Select]
var mySprite:Sprite = URLWhatever();
var myFlxSprite = new FlxSprite().makeGraphic(mySprite.width,mySprite.height);
var myBitmapData:BitmapData = mySprite.pixels;

Then, you need to use DisplayObject & flash graphics draw calls to stamp or draw the Sprite onto that BitmapData object.  I don't remember actually what calls those are but it would be something like "myBitmapData.draw(Sprite);" or something like that.  Then, as long as you set:

Code: [Select]
var mySprite.dirty = true;
Then that FlxSprite should draw/render fine in the flixel game loop, and you can dump the Sprite from memory

UPDATED - hope that helps more!
« Last Edit: Thu, Apr 28, 2011 by Adam Atomic »

carlflor

  • Member
  • **
  • Posts: 13
  • Karma: +0/-0
    • View Profile
Re: Converting Sprite to FlxSprite
« Reply #2 on: Fri, Apr 29, 2011 »
it showed a white screen :| when I removed the add(myFlxSprite) it goes away

carlflor

  • Member
  • **
  • Posts: 13
  • Karma: +0/-0
    • View Profile
Re: Converting Sprite to FlxSprite
« Reply #3 on: Fri, Apr 29, 2011 »
I'm currently using an older flixel version (and I think I'm not allowed to update it D:) in which there's no field called dirty... can there still be a way? I tried using myFlxSprite.render() and myFlxSprite().update, but the same white rectangle appears :(

I made it like this
Code: [Select]
wallPic = Sprite(imgLoader.content);

wallHolder = new FlxSprite().createGraphic(wallPic.width, wallPic.height);

var temp:BitmapData = wallHolder.pixels;

temp.draw(wallPic);

add(wallHolder);
« Last Edit: Fri, Apr 29, 2011 by carlflor »

Adam Atomic

  • Founder
  • Key Contributor
  • *****
  • Posts: 852
  • Karma: +0/-0
  • new dad
    • View Profile
    • Adam Atomic
Re: Converting Sprite to FlxSprite
« Reply #4 on: Fri, Apr 29, 2011 »
that's definitely the right idea there - in version sof flixel without .dirty or .drawFrame() you should be able to just reassign the bitmap I think.  so if you added one last line of code there:

Code: [Select]
wallHolder.pixels = temp;
I htink that might work!

zadvornykh

  • Active Member
  • ***
  • Posts: 205
  • Karma: +1/-0
    • View Profile
Re: Converting Sprite to FlxSprite
« Reply #5 on: Fri, Apr 29, 2011 »
Is your sprite a single frame or an animation? If it is an animation, is the first frame white?

If it is an animation, the process discussed above will not work, as you would need to update the animation every frame, which is quite cumbersome.

If it is just a single frame, you can just turn it into an image, embed it, and use it with the flixel flow.