actionscript-3starling-framework

How can i convert a Quad to a Rectangle using Starling on Flash Develop (AS3)?


I wish to use the "intersects" (from Starling) to create a collision, but i don't know how to convert my Quad "player" and my Quad "block" to Rectangles. Can someone help?


Solution

  • Quad contains a getBounds public method that returns a new rectangle (or an updated one if passed in on the second param), so:

    var playerRect:Rectangle = quadPlayer.getBounds(this);
    var blockRect:Rectangle = blockPlayer.getBounds(this);
    if (playerRect.intersects(blockRect)) {
       trace("Collision!");
    }
    

    Now, I am using this as I do not know your display list hierarchy and this do not know which coordinate system your quads are in, so adjust accordingly.

    http://doc.starling-framework.org/current/starling/display/DisplayObject.html

    Transforming coordinates

    Within the display tree, each object has its own local coordinate system. If you rotate a container, you rotate that coordinate system - and thus all the children of the container.

    Sometimes you need to know where a certain point lies relative to another coordinate system. That's the purpose of the method getTransformationMatrix. It will create a matrix that represents the transformation of a point in one coordinate system to another.

    http://doc.starling-framework.org/current/starling/display/Quad.html

    getBounds(targetSpace:DisplayObject, out:Rectangle = null):Rectangle [override] Returns a rectangle that completely encloses the object as it appears in another coordinate system.