Tracing width/height of object in OpenFl 3, when targeting all platforms besides Flash

Just upgraded to 3.0 beta and found out that the method I used to retrieve width/height of objects doesn’t work anymore. :stuck_out_tongue:

If I do this:

var Test:Sprite = new Sprite();
Test.graphics.beginFill( 0xff0000, 1 );
Test.graphics.drawRect( 0, 0, 320, 240 );
Test.graphics.endFill();
addChild( Test );

And execute trace( Test.width ) when targeting HTML, Windows or Neko, all I get is 0. I tried to do type casting — trace( cast( Test, Sprite ).width ) — but no success.

If I do it while targeting Flash it outputs the correct width/height of the object. Any other target gives me 0.

If the object is from a class that extends a DisplayObject (hope I’m saying this right…still a total noob :sweat_smile:) sometimes I also get values like “103.92304845413263”, even though the object is 320px wide.

Is there a way to get the correct width of objects in 3.0? Should I use legacy mode?

Sorry if I wrote it badly, not a native english speaker :sweat_drops:

BTW, I’m on a machine running Win 8.1 Pro (if this helps with anything)

Thanks! :bow:

EDIT: Not all DisplayObjects return a different value for its dimensions. Using Shape, instead of Sprite/MovieClip, returns the proper width. :confused:

I just tried this on Neko:

package;


import openfl.display.Sprite;


class Main extends Sprite {
	
	
	public function new () {
		
		super ();
		
		var Test:Sprite = new Sprite();
		Test.graphics.beginFill( 0xff0000, 1 );
		Test.graphics.drawRect( 0, 0, 320, 240 );
		Test.graphics.endFill();
		addChild( Test );
		
		trace (Test.width);
		
	}
	
	
}

I got 320. Is this occurring as part of a larger, more complicated project, or maybe this was resolved in the repository, and hasn’t been released yet. I think we’ll do another beta release soon today

1 Like

Thanks for answering. :slight_smile:

This is what happens with neko here: http://i.imgur.com/1343uXO.png. And it occurs in both small and big projects, in any target which isn’t flash. :pensive:

In legacy mode it traces the object’s dimensions just fine.

BTW, I’m using Haxe 3.1.3. Should I upgrade it to 3.2.0-rc?

Joshua, I tested the code again and I’d like to correct one thing I said.

Not all objects that extend DisplayObject returns 0. Trying the code I posted with Shape returns the proper width, but using Movieclip or Sprite returns 0. :confused:

Will properly write an issue in the Github repo, if it helps. :smile:

Does it make a difference if you use the latest versions that were posted to haxelib today?

Oh! Works perfectly in all targets now! :smile: http://i.imgur.com/tf04xOP.png

Tested with Windows, Flash, HTML5 and Neko targets, no legacy mode, in both command line and FD. Will edit my github issue to mark as solved. :sweat_smile:

Thanks! :bow:

You should avoid using the width and height properties of DisplayObjectContainer(s) such as Sprite and MovieClip, as it will change if any of their children moves over the initial width and height.
This will be even more likely to happen if you center your Sprite, to make things like rotation easier.

Great to hear! Thanks for letting us know :slight_smile:

Hi, Marco. And thanks! I already learnt this by trial and error when I started thinking about the current project :sweat_smile: , but that isn’t a big problem. For what I’m doing here, I have to use width/height of the container to properly resize it when the stage/screen resizes.

Before adding children objects, I set up a small safe zone so I don’t have to worry about its children moving. Since I’m building a puzzle right now, since each tile’s movements are predictable, and since the tiles move relative to the container’s original dimensions, it doesn’t pose a problem, for now. :stuck_out_tongue: But for my second project, I might have a few headaches if I do this. :persevere:

Since I’m still learning Haxe (and how to properly write code), I think it is a good thing to let these things happen. :blush:

Thanks for answering, Joshua! :blush: