Question

I am currently struggling with starling. I just created a simple class called "Star" that inherits from starling.display.Sprite. In this class in the "onAddedToStage" function I have the following code:

img = new Image(Assets.getTextureAtlas(Assets.ATLAS_MENU).getTexture("star_full.png"));
img.height = 171;
img.width = 179;
img.x = -img.width / 2;
img.y = -img.height / 2;
addChild(img);

Unfortunately, the following code in another class doesn't really work:

 var star:ObjectStar = new ObjectStar(starsCount == totalStars);
 star.x = 286;
 star.y = 161;
 star.width = 73;
 star.height = 70;
 addChild(star);

The object is drawn at the right position, but the width and height are still 171 and 179, not 73 and 70 like I wanted them to be. Is this a starling related issue or am I just really stupid?

Était-ce utile?

La solution

I am not familiar with Starling, however I believe what you are seeing is normal behavior in Flash. If you have a Sprite, which has a red box that is 100x100, and you put that into another Sprite, and set the parent sprite's width to 200x200, the red box will still be drawn at 100x100 pixels. I have a feeling previous versions of flash, when using AS2, behaved differently (i.e. the way you expected it to) but when using AS3, it doesn't.

Anyway... one way to fix your problem is to override the width and height setters in the ObjectStar class, like so;

override public function set width(value:Number):void 
{
    img.width = value;
    super.width = value;
}


override public function set height(value:Number):void 
{
    img.height = value;
    super.height = value;
}

So, this way, when you set the width and height of the ObjectStar, you are explicitly setting the width and height of the img as well.

(Obviously, you would need to keep a reference to the 'img' you create.)

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top