0
votes

I am making a Top Down Shooter in Flash CS4 using AS3.

When I shoot the bullets. I have set its starting position to be player.x and player.y. But that is where the player movieclip's "head" is at. I want it to be created from the place where the Gun is.

I made another movieclip inside of Player movieclip called "NewShootingPoint" and positioned it where the gun is.

I can't find any proper localToGlobal references to advice me. I know that localToGlobal converts a movieclip's local position to global position. But how do I do that?

I tried this: (New Code)

package {

import flash.display.MovieClip;
import flash.geom.Point;
import flash.display.Stage;
import flash.events.MouseEvent;
import flash.events.Event;

public class Main extends MovieClip {

    //var myPoint : Point = new Point(340.1, 232.2);
    var player : Player = new Player;
    var origin : Gun = new Gun();
    var bullet : Bullet = new Bullet();     

    public function Main() {

        stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDownHandler);
        player.x = stage.stageWidth/2;
        player.y = stage.stageHeight/2;
        stage.addChild(player);

    }

    function onMouseDownHandler(e:Event) {

        // I want this to happen.
        bullet.x = origin.x;
        bullet.y = origin.y;
        // But it's still stuck at (0,0)
        stage.addChild(bullet);
        bullet.addEventListener(Event.ENTER_FRAME, thingy);

    }

    function thingy(e:Event) {

        bullet.x += 10;
        bullet.y += 10;

    }

}

}

Yes. I know I should use classes. I'll do that later. Please advice for this!

1
localToGlobal is used to get an object coordinates according to the stage but since you add everything to the stage to start with localToGlobal is of no use to you. - BotMaster

1 Answers

0
votes

The documentation on localToGlobal is here.

The key is to understand and use the method against the correct local coordinate space and local coordinates, which the documentation states clearly but can still be confusing.

In my experience, while it's sort of instinctive to use your target object's x,y as the local point, it's often easiest to simply use the target object as the local coordinate space with a point of 0,0. For example, in your case this would be:

var bulletOrigin:Point = player.shootingPoint.localToGlobal(new Point(0, 0));