A Flash Developer Resource Site

Results 1 to 15 of 15

Thread: How to rotate an arrow with mouse drag????

  1. #1
    Member
    Join Date
    Sep 2014
    Posts
    75

    How to rotate an arrow with mouse drag????

    this attached to 1st frame on stage:

    arrow_mc.onMouseMove = function() {
    var adjacent:Number = _xmouse-arrow_mc._x;
    var opposite:Number = _ymouse-arrow_mc._y;
    var radians:Number = Math.atan2(opposite, adjacent);
    arrow_mc._rotation = radians*180/Math.PI;
    };

    with an "Arrow object" mc on stage called: arrow_mc

    How this code can be enhanced so that the arrow can be rotated by dragging the arrow from its pointer, or tail.


    thanks!

  2. #2
    Client Software Programmer AS3.0's Avatar
    Join Date
    Apr 2011
    Posts
    1,404
    I just made this, try it let me know.


    PHP Code:
    arrow_mc.onPress = function(){
        
    saved_x _xmouse;
        
    saved_y _ymouse;
        
    onEnterFrame = function(){
            if(
    _xmouse saved_x){
            
    arrow_mc._rotation -= 5;
            }
            if(
    _xmouse saved_x){
            
    arrow_mc._rotation += 5;
            }
            }
    }
    arrow_mc.onRelease arrow_mc.onReleaseOutside = function(){
        
    delete onEnterFrame;


  3. #3
    Member
    Join Date
    Sep 2014
    Posts
    75
    Alloy Bacon, thank you!

    this is really a good code to start with. thanks again!

  4. #4
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    PHP Code:
    arrow_mc.onMouseDown = function()
    {
        
    this.onMouseMove = function()
        {
            if (
    this.hitTest(_root._xmouse_root._ymousetrue))
            {
                
    myRadians Math.atan2(_root._ymouse this._y_root._xmouse this._x);
                
    myDegrees Math.round((myRadians 180 Math.PI));
                
    this._rotation myDegrees 90;
                
    arrow_mc._rotation this._rotation;
            }
        };
    };

    arrow_mc.onMouseUp = function()
    {
        if (
    this.onMouseMove)
        {
            
    delete this.onMouseMove;
        }
    }; 

  5. #5
    Member
    Join Date
    Sep 2014
    Posts
    75
    Thank you fruitbeard, what a good improvement to the original code!

    Just one more question, how this code could be utilized, if several arrows are attached from the library to stage, off course each with different instance name.

  6. #6
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    without going too into depth, you can just alter the var names like so.

    PHP Code:
    arrow_mc1.onMouseDown = function()
    {
        
    this.onMouseMove = function()
        {
            if (
    this.hitTest(_root._xmouse_root._ymousetrue))
            {
                
    myRadians1 Math.atan2(_root._ymouse this._y_root._xmouse this._x);
                
    myDegrees1 Math.round((myRadians1 180 Math.PI));
                
    this._rotation myDegrees1 90;
                
    arrow_mc1._rotation this._rotation;
            }
        };
    };

    arrow_mc1.onMouseUp = function()
    {
        if (
    this.onMouseMove)
        {
            
    delete this.onMouseMove;
        }
    };

    arrow_mc2.onMouseDown = function()
    {
        
    this.onMouseMove = function()
        {
            if (
    this.hitTest(_root._xmouse_root._ymousetrue))
            {
                
    myRadians2 Math.atan2(_root._ymouse this._y_root._xmouse this._x);
                
    myDegrees2 Math.round((myRadians2 180 Math.PI));
                
    this._rotation myDegrees2 90;
                
    arrow_mc2._rotation this._rotation;
            }
        };
    };

    arrow_mc2.onMouseUp = function()
    {
        if (
    this.onMouseMove)
        {
            
    delete this.onMouseMove;
        }
    }; 
    as I say, this is just quickly done, but you could use arrays and make them both use the same functions, I will leave that to you.

  7. #7
    Member
    Join Date
    Sep 2014
    Posts
    75
    Ok, thank you, will give try and post it here.

    All the best!

  8. #8
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    PHP Code:
    var clips:Array = new Array(arrow_mc1arrow_mc2arrow_mc3);

    for (var 
    i:Number 0clips.lengthi++)
    {
        var 
    toeNail:MovieClip clips[i];
        
    toeNail.onMouseDown = function()
        {
            
    this.onMouseMove = function()
            {
                if (
    this.hitTest(_root._xmouse_root._ymousetrue))
                {
                    
    this.radians Math.atan2(_root._ymouse this._y_root._xmouse this._x);
                    
    this.degrees Math.round((this.radians 180 Math.PI));
                    
    this._rotation this.degrees 90;
                }
            };
        };
        
    toeNail.onMouseUp = function()
        {
            if (
    this.onMouseMove)
            {
                
    delete this.onMouseMove;
            }
        };


  9. #9
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    I got timed out, so new post.
    PHP Code:
    var i:Number;

    var 
    barrs:Array = new Array(bar1bar2bar3);

    var 
    texts:Array = new Array(outPut1outPut2outPut3);

    var 
    clips:Array = new Array(arrow_mc1arrow_mc2arrow_mc3);

    for (
    0clips.lengthi++)
    {
        var 
    thisText:TextField TextField(texts[i]);
        
    thisText.text "0";

        var 
    thisBar:MovieClip MovieClip(barrs[i]);
        
    thisBar._width 0;

        var 
    toeNail:MovieClip MovieClip(clips[i]);

        
    toeNail.identity i;

        
    toeNail.onRollOver = function()
        {
            
    this.useHandCursor true;
        };

        
    toeNail.onMouseDown = function()
        {
            
    this.onMouseMove = function()
            {
                if (
    this.hitTest(_root._xmouse_root._ymousetrue))
                {
                    
    this.radians Math.atan2(_root._ymouse this._y_root._xmouse this._x);
                    
    this.degrees Math.round((this.radians 180 Math.PI));
                    
    this._rotation this.degrees 90;
                    
    this._rotation this._rotation;
                    
    this.showAngle this._rotation;
                    if (
    this.showAngle 0)
                    {
                        
    this.showAngle this.showAngle 360;
                    }
                    
    texts[this.identity].text String(this.showAngle);
                    
    barrs[this.identity]._width 100 360 this.showAngle;
                }
            };
        };

        
    toeNail.onMouseUp = function()
        {
            if (
    this.onMouseMove)
            {
                
    delete this.onMouseMove;
            }
        };

    Last edited by fruitbeard; 05-31-2015 at 06:45 AM.

  10. #10
    Member
    Join Date
    Sep 2014
    Posts
    75
    fruitbeard, thank you for finding the time to help!

    going through all the codes in this thread, came with the following:
    this allow you to drag, and rotate it.

    first, create mc "Arrow_mc" and inside it a child mc called "drage".

    //Create a button on stage, and put the following code inside it.
    on (press) {
    var i = _root.getNextHighestDepth();
    _root.attachMovie("Arrow_mc", "Arrow_mc"+i, i, {_x:100, _y:100});
    var mcArrow:MovieClip = MovieClip(eval("Arrow_mc"+i));
    }


    //Put the following code in frame 1 on stage:
    _root.onEnterFrame = function() {
    //
    mcArrow.onPress = function() {
    if (this.drag.hitTest(_root._xmouse, _root._ymouse, true)) {
    this.startDrag();
    } else {
    this.onMouseMove = function() {
    this.radians = Math.atan2(_root._ymouse-this._y, _root._xmouse-this._x);
    this.degrees = Math.round((this.radians*180/Math.PI));
    this._rotation = this.degrees;
    };
    }
    };
    mcArrow.onMouseUp = function() {
    this.stopDrag();
    if (this.onMouseMove) {
    delete this.onMouseMove;
    }
    };

    };

  11. #11
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    Well done, you don't need the onEnterFrame at all, infact it's just a waste of resources.

  12. #12
    Member
    Join Date
    Sep 2014
    Posts
    75
    hi,

    You still need to use "onEnterFrame", just test the code.

    regards!

  13. #13
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    HI,

    No you don't

  14. #14
    Member
    Join Date
    Sep 2014
    Posts
    75
    Could you, if possible, please re-upload the file for an old version of flash (Flash pro 8.0) or post the code here. Thanks!
    Last edited by Dr_flash; 06-02-2015 at 02:00 PM.

  15. #15
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    HI,

    Sorry, unable to, CS6 here, perhaps you csn download the trial version.
    or attach yoru *.fla and we can paste the code here

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center