Gamua / Starling-Framework

The Cross Platform Game Engine
http://www.starling-framework.org
Other
2.85k stars 819 forks source link

scale, scaleX, scaleY property of Image, reset width and height #892

Closed kolobok55 closed 8 years ago

kolobok55 commented 8 years ago
package
{
    import flash.display.BitmapData;
    import flash.geom.Rectangle;

    import starling.display.Image;
    import starling.display.Sprite;
    import starling.textures.Texture;

    public class StartMin extends Sprite
    {
        public function StartMin()
        {
            super();

            var btmp:BitmapData = new BitmapData( 32, 32, false, 0xff0000 );
            btmp.fillRect( new Rectangle( 8, 8, 16, 16 ), 0x0000ff )
            var txtr:Texture = Texture.fromBitmapData(btmp);

            var img:Image = new Image(txtr);
            img.scale9Grid = new Rectangle( 8, 8, 16, 16 );
            img.width = 200;
            img.height = 200;
            addChild( img );

            var img1:Image = new Image(txtr);
            img1.scale9Grid = new Rectangle( 8, 8, 16, 16 );
            img1.x = 250;
            img1.width = 200;
            img1.height = 200;
            img1.scale = 1;
            addChild( img1 );
        }
    }
}

second Image hasn't 200х200 size

kolobok55 commented 8 years ago

positive value of width reseting negative value of scaleX.

PrimaryFeather commented 8 years ago

In Starling, width and height are actually just wrappers for scaleX and scaleY. So when you double the width of an object, its scaleX will be 2.0.

The same is happening here. Add a trace output to your code, like this:

var img1:Image = new Image(txtr);
img1.scale9Grid = new Rectangle( 8, 8, 16, 16 );
img1.x = 250;
img1.width = 200;
trace(img1.scaleX); // -> 6.25
img1.scale = 1;

As you can see, after setting the width, the scaleX is raised to 6.25 in your sample. Then, in the next line, you're changing it back to 1.0 — which means that the size change is undone.

Does that explain the behavior for you?

kolobok55 commented 8 years ago

Older version hasn't this problem. I've solved this by wraping Image into Sprite. Sprite working correct. IMHO all subclasses of DisplayObjects must has same behavior, scale as factor must be independed of width and height property like a Sprite.

PrimaryFeather commented 8 years ago

There must be a misunderstanding here, because this always worked like this in Starling. And even on a sprite, when you change its width, scale will change, as well.

var sprite:Sprite = new Sprite();
var quad:Quad = new Quad(100, 100);
sprite.addChild(quad);

trace(sprite.width); // 100
trace(sprite.scaleX); // 1.0

sprite.width = 200;

trace(sprite.width); // 200
trace(sprite.scaleX); // 2.0

sprite.scaleX = 1.0;

trace(sprite.width); // 100
trace(sprite.scaleX); // 1.0

Or do you mean something else? (Feather's Scale9Image and Scale3Image might have worked different, if you mean those.)

kolobok55 commented 8 years ago

Sorry, you're right, i've used featherui Scale9Image, migration makes this mistake.

PrimaryFeather commented 8 years ago

Okay, that explains it. Yes, the reason this works different than Feather's Scale9Image is that — as you wrote yourself — objects should always behave the same way. And in this case: an image with a scale9Grid must behave just the same way as an image without one.

Let me know if you run into anything else! :smile: