forum > Problem rotation the tower

  • After a long time, I understand haXe and as3.0.I'm moving the adobe code in haXe.But I'm having trouble with rotating the turret.It rotates around the mouse when the mouse moves.I want it to rotate around its center.
    I have created resource.swf by swfmill3.02.My xml:

    <?xml version="1.0" ?>
    <movie version="9">
        <frame>
            <library>
                <clip id="Canon" import="Canon.png"/>
        </library>
        </frame>
    </movie>

    import flash.display.MovieClip;
    import flash.events.MouseEvent;
    import flash.text.TextField;
    class Canon extends MovieClip{}
    class Main extends MovieClip{
        static var cn:Cannon=new Cannon(); 
        static var mousex;
        static var mousey;
        static var angle;
    
        static function main()
        {
            cn.x=100;
            cn.y=100;
            flash.Lib.current.addChild(cn);        
            flash.Lib.current.stage.addEventListener(MouseEvent.MOUSE_MOVE, move);
        }
    
        static function move(event:MouseEvent){
                           //flash.Lib.current.stage.mouseX I'm right?
                    mousex = flash.Lib.current.stage.mouseX-cn.x;
                         mousey = (flash.Lib.current.stage.mouseY-cn.y) * -1;
                         angle = Math.atan(mousey/mousex)/(Math.PI/180);
                         if (mousex<0) {
                                angle += 180;
                         }
                         if (mousex>=0 && mousey<0) {
                                angle += 360;
                         }
           
                         cn.rotation = (angle * -1)+90;
            }
    }
    

  • Without making a test I suggest you use Math.atan2 , also you probably need to work out cannon centre relative to top corner of the cannon graphic.
    to apply a rotation you can -position then rotate and +position, obviously if you do this visually it adds render overhead, but it will be more trick to do it virtually and apply the result so best to start simple. Have you the cannon png so if someone wants to do a test using your asset they can? Good luck sorry late here but if you get nowhere I can post a solution sometime, I am sure I have done this many times.

  • I can use the registration point?How to set registration point in the center?

  • I tried:

    <?xml version="1.0" ?>
    <movie version="9">
    <clip id="Canon" import="Canon.png"/>
    <library>
        <clip id="Canon">
            <frame>
                     <place id="Canon"  x="-50" y="-50"/>
            </frame>
        </clip>
    </library>
    </movie>

    x,y=-50 because my Canon is 100x100.But not change.

  • You can place your Cannon at -50, -50 in a Sprite is your haxe code, or like I said emulate it by adding the 50 rotating and then taking the 50 off. I am not sure how well doing it in swfmill if it works will be.

  • thanks you so much.
    New xml:

    <?xml version="1.0" ?>
    <movie version="9">
        <frame>
        <library>
                <clip id="Canon.png" import="Canon.png"  />
                <clip id="Canon" class="Canon">
                    <place id="Canon.png" x="-50" y="-50" />
                </clip>
        <place id="Canon" x="100" y="100" />
        </library>
        </frame>
    </movie>

    x=-png.width/2
    y=-png.height/2
    Main.hx:
    import flash.display.MovieClip;
    import flash.display.MovieClip;
    import flash.events.MouseEvent;
    import flash.events.Event;
    import flash.text.TextField;
    class Canon extends MovieClip{}
    class Main extends MovieClip{
        static var cn:Canon=new Canon(); 
        static var mousex;
        static var mousey;
        static var angle;
    
        static function main()
        {
            cn.x=120;
            cn.y=120;
            flash.Lib.current.addChild(cn);
            flash.Lib.current.stage.addEventListener(MouseEvent.MOUSE_MOVE, move);
        }
         static function move(event:MouseEvent)
         {
            // find out mouse coordinates to find out the angle
            //mouses or targets coordinates
            var cy = flash.Lib.current.stage.mouseY - cn.y; 
            var cx = flash.Lib.current.stage.mouseX - cn.x;
            // find out the angle
            var Radians = Math.atan2(cy,cx);
            // convert to degrees to rotate
            var Degrees = Radians * 180 / Math.PI;
            // rotate
            cn.rotation = Degrees+180;
         }
    
    }

    I am looking for parts of swfmill.I'm trying to use it for MovieClip has 2 frames

< Prev | Page 1 / 1 | Next >