Hello and welcome to our community! Is this your first visit?
Register
Page 1 of 2 12 LastLast
Results 1 to 10 of 16
  1. #1
    Enthusiast Blitz's Avatar

    Join Date
    Mar 2009
    Location
    Zombocom
    Posts
    2,045

    Anyone know... (ActionScript help)

    ...what the code would be for making an arrow rotate in the direction that user's cursor is, and when, on click, the arrow goes to the user's cursor (with momentum)?

    Here's an example, when the mouse turns red, that is symbolizing a click.



    If anyone could help me with this, I would greatly appreciate it.
    [center:s1g75gto]"I rather tell thee what is to be feared than what I fear; for always I am Caesar"

    Steam Username: Blitzkrieg ( Add CrashClosed54 )[/center:s1g75gto]

  2. #2
    Senior Member .Glenn's Avatar
    Join Date
    Jun 2009
    Location
    Somewhere
    Posts
    260

    Re: Anyone know... (AS2 help)

    For the direction thing, this tutorial might help.
    NOTE: This tutorial is for AS3, but you can use the same math functions.
    Or just google it up.

    And for the arrow going for the cursor, you could make the arrow move half the distance to the mouse when the mouse is clicked. But it doesn't give momentum though.
    Use your physics skills.
    [center:1x4wv24k]


    Steam ID: jamieswhiteshirt
    BAD TRANSLATOR
    [/center:1x4wv24k]

  3. #3
    Veteran Enthusiast LeadingManNigel's Avatar


    Join Date
    May 2005
    Location
    New York City
    Posts
    6,213

    Re: Anyone know... (AS2 help)

    Also note you have to adjust your code depending on what direction the triangle would be facing(before code is applied). If it is facing left I believe you add -90 to the formula, I am not sure because I believe flash does angles weird.

  4. #4
    Enthusiast Blitz's Avatar

    Join Date
    Mar 2009
    Location
    Zombocom
    Posts
    2,045

    Re: Anyone know... (AS2 help)

    Ok, I have a code here, though I'm not sure whats wrong.

    Here is the code
    Code:
    //this is the code for making the ball goto the click
    moveBall = function () {
    this.dx = (this.targx-this._x)*.1;
    this.dy = (this.targy-this._y)*.1;
    this._x += this.dx;
    this._y += this.dy;
    };
    
    setTarget = function () {
    this.targx = _root._xmouse;
    this.targy = _root._ymouse;
    
    };
    ball.targx = 10;
    ball.targy = 10;
    ball.onEnterFrame = moveBall;
    ball.onMouseDown = setTarget;
    
    //this is for my cursor
    Mouse.hide();
    
    cursor_mc.onMouseMove = function(){
       this._x = _root._xmouse;
       this._y = _root._ymouse;
       updateAfterEvent();
    };
    And here is an example of what is going on
    http://swfbox.org/view/1juiw8
    [center:s1g75gto]"I rather tell thee what is to be feared than what I fear; for always I am Caesar"

    Steam Username: Blitzkrieg ( Add CrashClosed54 )[/center:s1g75gto]

  5. #5
    Fanatic Enthusiast Niall's Avatar

    Join Date
    Aug 2007
    Posts
    3,298

    Re: Anyone know... (AS2 help)

    Alrighty then. The "rotate to mouse" thing has already been explained in many places, so I'll just give you this link to learn from - http://www.actionscript.org/resources/a ... Page1.html

    Now, to your mouse problem.Your move-to-mouse code seems fine (not the way I'd do it, same result though) but you're never updating the movement. Your code is only run once, when it's first loaded. You'll need to place some stuff in an onEnterFrame loop to make sure it's constantly updated.
    To move towards the mouse we need to get the distance from it to the object. Let's say the mouse's x value is mx, which is 120 pixels along from the origin point. The object's x is 20 pixels along:

    mx = 120
    dx = x-mx


    Now, x(20)-mx(120) = -100, right? So, we need to keep moving the object's x along until dx reaches zero. x += dx would work fine, as we're constantly moving the object's x forward by dx, but that means the movement is instant.
    If dx = x-mx = 100, and x += dx, that means we move x forward 100 pixels straight away. So we need to add a little easing to it. Luckily, this is pretty simple.

    dx(-100)/10 = -10. If instead of moving the x axis along by dx, we move it along by dx divided by a value, the movement is significantly smaller and easier to follow. Therefore, the higher the value dx is divided by, the slower the movement.


    I still haven't explained how to detect all the mouse down stuff, and what I just said might make no sense at all. So, for your convenience I present you with this cute little .fla, with comments, for your inspection: http://filebox.me/files/xk3hfdca2_tomouse.fla

  6. #6
    Insanity Skype's Avatar



    Join Date
    Apr 2007
    Location
    England
    Posts
    27,397

    Re: Anyone know... (AS2 help)

    ^ if x = 20, and mx = 120, x - mx would be -100.

  7. #7
    Fanatic Enthusiast Niall's Avatar

    Join Date
    Aug 2007
    Posts
    3,298

    Re: Anyone know... (AS2 help)

    Quote Originally Posted by Skype
    ^ if x = 20, and mx = 120, x - mx would be -100.
    Oh shit lol, my mistake. Well it'll still work, as it's just moving by -100. Thanks for pointing that out.

  8. #8
    Enthusiast Blitz's Avatar

    Join Date
    Mar 2009
    Location
    Zombocom
    Posts
    2,045

    Re: Anyone know... (AS2 help)

    Alright, I completely restarted and looked up some tutorials. I got everything to work, but I am yet to find the right code for a mouse watcher.

    http://swfbox.org/view/dlv1yq (Progress)

    If anyone is curious, here is the code I now have;
    Code:
    //This is for making the arrow gotoClick
    import fl.transitions.Tween;
    import fl.transitions.TweenEvent;
    import fl.transitions.easing.*;
    
    var xMovement:Tween;
    var yMovement:Tween;
    
    
    function Start():void {
     stage.addEventListener(MouseEvent.CLICK, moveToClick);
    }
    
    function moveToClick(event:MouseEvent):void {
    
     xMovement = new Tween(triangle_mc, "x", Back.easeIn, triangle_mc.x, mouseX, .7, true);
     yMovement = new Tween(triangle_mc, "y", Back.easeIn, triangle_mc.y, mouseY, .7, true);
    }
    
    Start();
    
    //Code for the cursor
    Mouse.hide();
    
    Mouse.hide();
    cursor.startDrag(true);
    
    //this is where the arrow looks at the cursor
    Oh, and Niall, did you use ActionScript 2? Because I'm getting a ton of errors, though the code isn't in the movie clip itself (It won't let me).
    [center:s1g75gto]"I rather tell thee what is to be feared than what I fear; for always I am Caesar"

    Steam Username: Blitzkrieg ( Add CrashClosed54 )[/center:s1g75gto]

  9. #9
    Fanatic Enthusiast Niall's Avatar

    Join Date
    Aug 2007
    Posts
    3,298

    Re: Anyone know... (AS2 help)

    Updated Start() function:

    function Start():void {
    stage.addEventListener(MouseEvent.CLICK, moveToClick);
    stage.addEventListener(Event.ENTER_FRAME, step);
    }


    Anywhere:

    function step(e:Event) {
    triangle_mc.rotation = Math.atan2(mouseY-triangle_mc.y, mouseX-triangle_mc.x)*180/Math.PI;
    }



    yay as3

  10. #10
    Enthusiast Blitz's Avatar

    Join Date
    Mar 2009
    Location
    Zombocom
    Posts
    2,045

    Re: Anyone know... (AS2 help)

    Quote Originally Posted by Niall
    Updated Start() function:

    function Start():void {
    stage.addEventListener(MouseEvent.CLICK, moveToClick);
    stage.addEventListener(Event.ENTER_FRAME, step);
    }


    Anywhere:

    function step(e:Event) {
    triangle_mc.rotation = Math.atan2(mouseY-triangle_mc.y, mouseX-triangle_mc.x)*180/Math.PI;
    }



    yay as3
    I tried inserting that into the end of the code, but the effect was this

    I then tried making a new animation with your code by itself, with the result of
    "1120: Access of undefined property moveToClick."
    Referring to the section of your code here (shown below)
    Code:
    function Start():void {
    stage.addEventListener(MouseEvent.CLICK, moveToClick); <---- Right there
    stage.addEventListener(Event.ENTER_FRAME, step);
    }
    function step(e:Event) {
    triangle_mc.rotation = Math.atan2(mouseY-triangle_mc.y, mouseX-triangle_mc.x)*180/Math.PI;
    }
    I tried taking that line of code out, with the result of no mouse replace, no rotation, no nothing.

    Halp?

    (By the way, this is the first time I've used AS in a while, bear with me
    [center:s1g75gto]"I rather tell thee what is to be feared than what I fear; for always I am Caesar"

    Steam Username: Blitzkrieg ( Add CrashClosed54 )[/center:s1g75gto]


 
Page 1 of 2 12 LastLast

Posting Permissions

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