Huh?! .. event.localX preserves "proportions" on a scaled Sprite?

I planned to organize a simple progress bar for my media player by drawing a colored rectangle and then changing its width property as needed. Visually, it looks fine - it scales beautifully (I could not draw it with zero width, so I assigned a minimum value of 0.1).

The real problem is different, although it seems very strange or, at least, absolutely non-obvious. Since I need to track user clicks on this progress bar in order to support the search in the media stream, I thought: I would detect a click event, take the x coordinate as the offset from the parent DisplayObject, count in time and do a search. Now it turns out that event.localX is not really "scalable" with Sprite. As I said, the most correct x coordinate for the progress bar is first 0.1, and it stays that way (!) No matter how wide the actual strip becomes - 5, 100, 1000. If you click on the rightmost pixel of the panel , it will be 0.1!

AND?..

I obviously have something wrong. Maybe there is a method, for example - refreshCoordinates () or something else? What is it?..

+3
source share
3 answers

, , , Flash. , , , , , , .. , , , 0,1 . , , , - . , , localX - , , .

, 100 , , . 100px, , localX . , .

, , localToGlobal globalToLocal . :

var localToThis:Point = new Point( someX, someY );
var globalToStage:Point = localToGlobal( localToThis );
var localToParent:Point = parent.globalToLocal( globalToStage );

( , , 100 , , .)

: 0,1 . , , x, rotation .., Flash , . 100px - , .

+2

, - . LocalToGlobal? - , .

+1

, , . graphics, .

, . , . - :

var sp:Sprite = new Sprite();

sp.graphics.beginFill(0xff0000);
sp.graphics.drawRect(0,0,200,30);
sp.graphics.endFill();
sp.scaleX = 0;

sp.addEventListener(Event.ENTER_FRAME,handleEnterFrame);
sp.addEventListener(MouseEvent.CLICK,handleClick);
addChild(sp);

function handleClick(e:MouseEvent):void {
    trace(e.localX);
}

function handleEnterFrame(e:Event):void {
    sp.scaleX += 0.01;
}

, , scaleX width , .

0
source

Source: https://habr.com/ru/post/1780507/


All Articles