Question concerning storing variables in movieClips/Sprites

I have a question concerning something I used to do in Adobe Flash/Animate that I haven’t been able to do in Haxe Openfl, and I’m wondering if it’s possible, but I’m just going about it all wrong.

In Animate/Flash I’d either draw a shape, or load a bitmap on to the stage and position it where I want it. I know that this element will be dragged around by a user and if that user does not put it in a specified location, I will want to return it to it’s original placement. So I would create a variable attached to my movieClip.

For example, if my movieClip was called “drag1”, I might do this when I set it up…
drag1.origX = drag1.x;
drag1.origY = drag1.y;

Then later when I need it to return to it’s starting point, I can just tell drag1.x to go to drag1.origX.

This was very handy when I had multiple objects on the stage with their own individual information to keep track of. Storing information in the movieClip was useful on many levels. Does something like this exist in Haxe and Openfl?

Thanks!

This requires adding new dynamic fields to MovieClip instances.

This is not allowed in “Flash strict” compilation with ActionScript 3. There are ways to work around this using the Haxe Reflect API, but it can hurt performance.

You might use consider using a map:

private var originalPosition:Map<MovieClip, Point>;

...

originalPosition[drag1] = new Point (drag1.x, drag1.y);

...

drag1.x = originalPosition[drag1].x;
drag2.y = originalPosition[drag2].y;

There’s obviously ways to extend MovieClip in a custom class as well, but Map (or if you prefer, Reflect) might be less intrusive.

Reflect.setField (drag1, "origX", drag1.x);
Reflect.setField (drag1, "origY", drag1.y);

if (Reflect.hasField (drag1, "origX") && Reflect.hasField (drag1, "origY")) {
    
    drag1.x = Reflect.field (drag1, "origX");
    drag1.y = Reflect.field (drag1, "origY");
    
}
1 Like

Why not a custom “MyMovieClip” class with all the information you want?
Or put the information in a string and save it in the name of the MovieClip :slight_smile:

1 Like

Thanks again Singmagesty, those do look like good ways around what I was trying to accomplish. Ofcourse I still want to look into creating a custom class as there could be instances in the future where I’ll want to keep track of other information, and a custom class sounds like a good way to do it.

Vega, I didn’t think to use a custom class mainly because I haven’t had much experience with those and so it didn’t occur to me I could use one like that. At my company senior programmers tend to build all the larger pieces, and I slip in with some design/animation and simple programming elements. They just wanted me to look into Haxe and see what I could do, so I’m on my own this go around.

I think a custom class could be very useful, so I’m going to look in to that.

Thanks!

Actually, I am still having some difficulty with this. Those solutions work great as alternatives for how I used Movieclips in Flash. However I’m now currently working with Sprites and I’m finding that those techniques don’t work as great for my needs.

I need to be able to drag my sprite around, and still keep tabs on it’s original location. I’ll continue to dig into custom classes though as it seems a promising direction.

You can also try this: