beans
beans

Reputation: 1765

Scale a sprite in as3?

I have a sprite in as3 code, I want to enter its height to resize it, how can I scale the width accordingly?

sprite.height = 200;
sprite.width = ??

Thanks

Upvotes: 0

Views: 12062

Answers (2)

blahdiblah
blahdiblah

Reputation: 34031

The scale properties are updated when you set the width/height, so you can use them to scale the other dimension:

sprite.height = 200;
sprite.scaleX = sprite.scaleY;

Easier and less error prone than tracking/updating the aspect ratio on your own.

Upvotes: 6

Jason Sturges
Jason Sturges

Reputation: 15955

If you want to scale a sprite, why not use use scale methods?

sprite.scaleX = 2;
sprite.scaleY = 2;

Otherwise, you will need to apply a ratio, such as width / height.

Say your sprite was width: 150, height: 100. That means width is 1.5 times height.

// calculate ratio of scale factor
var ratio:Number = sprite.width / sprite.height; // 1.5

// apply ratio your sprite's original dimensions:
sprite.height = 200;
sprite.width = sprite.height * ratio; // 300

This could also be accomplished using a Matrix transform.

Upvotes: 9

Related Questions