2
\$\begingroup\$

I would like to make a mirror reflection of an entity in Flashpunk. So I would like to copy a spritemap from one Entity to another (reflectionEntity). If i use something like this

reflectionEntity.sprite = otherEntity.sprite;

reflectionEntity.sprite.scaleY=-1; // to reflect

then the graphic of the original entity is also scaled.

Is there a simple way out of this? I have been searching for the solution for more than an hour and have not found a suitable one.

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

I'm assuming that reflectionEntity.sprite is an instance of Image or TiledSpriteMap. In the constructor for Image there's a souce:* parameter where you specify where the image comes from. This is the data you want to share.

Copying reflectionEntity.sprite (again, assuming that this is something like Image, or any subclass of Graphic really) just copies the references to that, which is why your changes propagate.

Flashpunk keeps an internal reference to the source, as seen here, but has it marked protected for whatever weird reason.

Keep the reference to whatever object you use to load your image, and pass that into a new instance like so:

[however the embed statement is written goes here, I forget]
var yourSpriteSource:Class; // Class, assuming you're doing resource embedding
/*
...
*/
otherEntity.sprite = new Image(yourSpriteSource);
reflectionEntity.sprite = new Image(yourSpriteSource);

Another alternative is to subclass each Graphic class you're interested in and expose the source() function. Another alternative to that (which is likely what I would do on a project), is fork Flashpunk and make the source() function public. But that's a bit extreme.

Think of Graphic subclasses as bits of data that know how to render the real graphics data, which is the source parameter you pass into their constructor.

\$\endgroup\$
2
  • \$\begingroup\$ Thank you very much for the suggestion, probably this is how I am going to do this, I was looking for a simpler (and more elegant) solution, but I guess I will copy the source image and manually set the frames. \$\endgroup\$
    – James
    Mar 9, 2013 at 12:03
  • \$\begingroup\$ @James If you want to make it more elegant, extend Image/TiledSpriteMap/whatever you're using and write a clone() method for it that also sets the frames for the new clone. \$\endgroup\$ Mar 9, 2013 at 18:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .