woodscreative
woodscreative

Reputation: 1031

AS3 Relative points

So I have a Crosshair on the Stage and I have an Enemy with nested child limbs, when the below function is called I want to create a hit mark and attach it as a child to the Enemies Limb but how do I translate the X/Y position of the Crosshair to the Enemies Limb bearing in mind the Limb may also be rotated? Any help appreciated.

function _hit (e):void
{
    if (theEnemy.limb1.hitTestObject(crosshair)) {
        var theHit:HitMark = new HitMark();
        theHit.x = ?;
        theHit.y = ?;       
        theEnemy.limb1.addChild(theHit);
    }
}

Upvotes: 1

Views: 197

Answers (1)

grapefrukt
grapefrukt

Reputation: 27045

Mouse coordinates inside display objects will be rotated and translated for you, assuming your crosshair follows the mouse you can do this:

var theHit:HitMark = new HitMark();
theHit.x = theEnemy.limb1.mouseX;
theHit.y = theEnemy.limb1.mouseY;
theEnemy.limb1.addChild(theHit);

If not, you'll have to use globalToLocal()

var theHit:HitMark = new HitMark();
var globalHitPoint:Point = new Point(crosshair.x, crosshair.y);
var localHitPoint:Point = theEnemy.limb1.globalToLocal(globalHitPoint);
theHit.x = localHitPoint.x;
theHit.y = localHitPoint.y;
theEnemy.limb1.addChild(theHit);

Upvotes: 2

Related Questions