Search code examples
actionscript-3rotationflash-cs5manual

Control rotating with bounding box Flash AC3


I'm a beginner in flash actionscript3, I was looking for hours for an action script could let me rotate movieclip on it's center from corners like any image classic rotation, with bounding box appearing, and without values, only manual rotating, can I get any help for this please??


Solution

  • I wrote some of the code for you, if i understood what you wanted correctly. Put this code on frame 1. For any picture you want to make rotatable, give it an instance name and call makeRotatable(instanceNameOfYourPictureHere).

    It's not perfect, but it's something to get you started.

    import flash.display.MovieClip;
    import flash.events.MouseEvent;
    import flash.display.DisplayObject;
    import flash.geom.Rectangle;
    
    //change "myPicture" to any object you want to make rotatable
    makeRotatable(myPicture);
    
    var currentObject:Object; //holds a reference to whichever objectis currently being rotated
    var oldx:Number;//used to determine mouse movement on every new frame
    var oldy:Number;
    var boundsRect:Rectangle;//used for creating bounding box
    
    function makeRotatable(target:DisplayObject){//this function sets up the rotation center and adds mouse handling
    
        //creating a container for your picture
        var pictureContainer:MovieClip = new MovieClip; 
    
        //adding it to the stage
        stage.addChild(pictureContainer); 
    
        //setting its top left corner, around wich it will rotate, in the center of your picture
        pictureContainer.x = target.x + target.width * 0.5;
        pictureContainer.y = target.y + target.height * 0.5;
    
        //adding your picture into the container, and moving its center onto the rotational point of the container
        pictureContainer.addChild(target);
        target.x = 0 - target.width * 0.5
        target.y = 0 - target.height * 0.5
    
        //adding mouse listeners to the container and stage
        pictureContainer.addEventListener(MouseEvent.MOUSE_DOWN, startRotate)
        stage.addEventListener(MouseEvent.MOUSE_UP, stopRotate)
    
    
    }
    function startRotate(e:MouseEvent):void{//sets up for EnterFrame listener and bounding box
        stage.addEventListener(Event.ENTER_FRAME, changeRotation)
        oldx = stage.mouseX
        oldy = stage.mouseY
        currentObject = e.currentTarget
        boundsRect = currentObject.getBounds(currentObject)
        currentObject.graphics.lineStyle(4,0xFF0000);       
        currentObject.graphics.drawRect(boundsRect.x, boundsRect.y, boundsRect.width, boundsRect.height);
    }
    function stopRotate(e:MouseEvent):void{//removes EnterFrame listener and bounding box
        stage.removeEventListener(Event.ENTER_FRAME, changeRotation)
        currentObject.graphics.clear();
    }
    function changeRotation(e:Event){//rotates the object based on mouse position and movement respective to objects center point
        if(stage.mouseX>currentObject.x){
            currentObject.rotation += stage.mouseY - oldy
        }else{
            currentObject.rotation -= stage.mouseY - oldy
        }
        if(stage.mouseY<currentObject.y){
            currentObject.rotation += stage.mouseX - oldx
        }else{
            currentObject.rotation -= stage.mouseX - oldx
        }
        oldx = stage.mouseX
        oldy = stage.mouseY
    }