DaVikingCode / Citrus-Engine

Modern AS3 Game Engine
http://citrusengine.com/
Other
549 stars 231 forks source link

2 Starling Sprite in the view property = coordinates issue #93

Open alamboley opened 11 years ago

alamboley commented 11 years ago

http://forum.starling-framework.org/topic/2-starling-sprite-in-the-view-property-coordinates-issue

gsynuh commented 10 years ago

I was just going over the list of bugs and thought I'd look at this one.

the moveRegistrationPoint code works only if the content is already registered topLeft itself. which means it fails to actually center something around the origin if that thing is not starting at a top left position already - that's because we're not taking into account the amount of displacement required to move that _content to reposition it relative to the container (starling art), we need to get its bounds in the StarlingArt space.

anyway, setting to "center" will not always center if that rule is not followed, that's where the misconception/issue arises as one would think that using "center" would actually center the content when all it does is move the thing, as a fake pivot point in fact... and if the _content already has seen its pivot point changed (like in the example on the forum post) - then the result is not really predictable, although we would've effectively been moving something around.

here's a moveRegistrationPoint function that would, in all possible cases, reposition the content correctly - in fact this function should actually be called "align" and is anyway for StarlingArt doing exactly what alignPivot does, this function is easily portable to SpriteArt as well.

private static var rectBounds:Rectangle = new Rectangle(); // rectangle helper.
public function moveRegistrationPoint(registrationPoint:String):void {
            if(_content.parent == this)
                _content.getBounds(this, rectBounds);
            else
                return;

            if (registrationPoint == "topLeft") { // effectively re-aligns the content for its topleft to match StarlingArt's 0,0 point
                _content.x = -rectBounds.x;
                _content.y = -rectBounds.y;
            } else if (registrationPoint == "center") { // centers the content around starling art's 0,0
                _content.x = -rectBounds.x - rectBounds.width*.5;
                _content.y = -rectBounds.y - rectBounds.height*.5;
            } else if (registrationPoint == "bottomRight") { //and yes why not.
                _content.x = -rectBounds.x - rectBounds.width;
                _content.y = -rectBounds.y - rectBounds.height;
            }
}

even though it actually does what its supposed to do now... some code or examples break, as most heros we have are sometimes set to topLeft where they should actually be centered to be like the physical body instead.

Up to now I think it was just a mysterious and unclear function (so would be the registration property of citrusObject) as some results seemed to make no sense.

a "align" property is more interesting in that case instead of a talking about registration points cause in the end we just want our stuff to be aligned in some way.

(and this is exactly like using alignPivot() with starling, all I'm doing here is reproducing the same behavior... only maybe slower)

aligning could be, instead of a string, a value as well for finer control, not sure what is best anyway.

alamboley commented 10 years ago

Hey, nice function!

Could you tell me the examples broken? Normally I've never changed the registrationPoint for physics object except for DragonBones (I think).

Also we've to remove the fact that it is called twice :/