dcsimg
A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: Object to follow mouse click

  1. #1
    Member
    Join Date
    Dec 2006
    Posts
    31

    Object to follow mouse click

    I have a map (a picture of a forest) and a little bunny. What needs to be done is anywhere you click the mouse on the map, the little bunny walks in the direction of the mouse click.

    Can anyone please help me with this?
    Beauty is only a light switch away.

  2. #2
    Member
    Join Date
    Nov 2007
    Location
    Brazil
    Posts
    47
    When movie starts:

    var bunny = element ("My little bunny")

    function getMousePos() {
    bunny.x = mouseX
    bunny.y = mouseY
    }


    Add a mouse event (onClick) in the forest with the script:
    getMousePos()

    I hope that I help you '']
    []'s
    leocavalcante.com

  3. #3
    Member
    Join Date
    Dec 2006
    Posts
    31
    Hi Leo. Thank you for your reply. I couldn't get the script to work, but I do have something similar and it works except that the bunny doesn't scroll to the mouse, it just appears at the mouse. I need it to scroll or slide to the mouse. I can do it in mx flash just because I copied a tutorial, but I don't know how to eidt the code for 3dfa. Does anyone know how the code should be for 3dfa? Here are the codes:

    Code:
    onClipEvent (load) {
    friction = "0.18";
    targetx = Random(300)+20;
    targety = Random(300)+20
    }
    
    onClipEvent (enterFrame) {
    mouse_x = int(targetx-this._x);
    mouse_y = int(targety-this._y); 
    if (mouse_x>0 && mouse_y>0) {
    quad = Number(4);
    }
    
    if (mouse_x<0 && mouse_y>0) {
    quad = Number(1);
    }
    
    if (mouse_x<0 && mouse_y<0) {
    quad = Number(2);
    }
    
    if (mouse_x>0 && mouse_y<0) {
    quad = Number(3);
    }
    
    abs_x = Math.abs(mouse_x);
    abs_y = Math.abs(mouse_y);
    tg = abs_y/abs_x;
    _root.maths = Math.atan(tg)*Number(180)/Math.PI;
    if (quad == 1) { angle = number(90) - number(_root.maths) }
    if (quad == 2) { angle = number(90) + number(_root.maths) }
    if (quad == 3) { angle = number(270) - number(_root.maths) }
    if (quad == 4) { angle = number(270) + number(_root.maths) }
    if (not _root.done) { setProperty (_this, _rotation, angle); _root.done = true}
    speedx = difx*friction;
    speedy = dify*friction;
    setProperty (this, _y, _root.object._y+speedy);
    setProperty (this, _x, _root.object._x+speedx);
    difx = int(targetx)-this._x;
    dify = int(targety)-this._y;
    }
    Code:
    gotoAndPlay(1);
    Code:
    onClipEvent (load) {
    startDrag (this, true);
    _root.drag = this;
    }
    
    onClipEvent (mouseDown) { 
    _root.difx = this._x- _root.object._x;
    _root.object.targetx = this._x;
    _root.dify = this._y- _root.object._y;
    _root.object.targety = this._y;
    _root.done = False
    }
    Any help is greatly appreciated.
    Beauty is only a light switch away.

  4. #4
    Member
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    76
    Hi,

    I've built a script some months ago for moving an element smoothly to another.

    Past these scripts into the Movie script of the main movie.
    Replace *** NAME *** with the name of your bunny element.


    When movie starts script
    Code:
    // the element to move, replace *** NAME *** with element name
    var e = element (" *** NAME *** ");
    
    // the target position
    var t = new Point(0,0);
    
    // smooth factor
    // you can select the speed of movement by changing the smooth factor
    // try values between 0 and 1
    var factor = 0.1;
    on every frame script
    Code:
    if (t.x != 0 && t.y != 0)
    {
    	if (t.x > e.x)
    		e.x += (t.x-e.x)*factor;
    	if (t.x < e.x)
    		e.x -= (e.x-t.x)*factor;
    	if (t.y > e.y)
    		e.y += (t.y-e.y)*factor;
    	if (t.y < e.y)
    		e.y -= (e.y-t.y)*factor;
    }

    Now you have to create a Mouse element
    [Add new element] -> [Action elements] -> [Mouse]

    A properties window opens. Add an "onMouseDown" event by clicking [Add mouse event] and selecting "onMouseDown" in the drop down menu.

    Click the [Add new action] button and select "Script". Then copy the following script in the script window:

    Script window
    Code:
    root.t.x = root.mouseX;
    root.t.y = root.mouseY;
    By changing the smooth factor in the main script, you can control how fast the element moves to the mouse.


    Hope that helps you,
    Leifi

  5. #5
    Member
    Join Date
    Dec 2006
    Posts
    31
    Thank you for that leifi. I wasn't even aware of the mouse selection in the active elements menu. Anyway, I did the three steps and when I play it, I get the following error message:
    Unknown type 'Point' cannot be created on line 5 var t = new Point(0,0)

    I exported the movie just to see, but it doesn't work either. Do you know what is going on here? I am also going to play around with the script and see if I can figure it out. Again, thank you so much for replying.
    Beauty is only a light switch away.

  6. #6
    Member
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    76
    Hmm that's very strange. It works for me... Do you use the newest version of 3DFA ( 4.9.8.4 )? If not, you can get it here.

    Otherwise, could you send me your .movie file? That would help me a lot to see where the problem is.

    Cheers,
    Leifi

  7. #7
    Member
    Join Date
    Dec 2006
    Posts
    31
    Oh, I used version 4.9.6.2, I am downloading the newest version now. I will let you know how that goes. Sorry about that. I didn't know about the upgrade.

    Okay, I got it and oh my gosh it does work!!! Thank you so much. That is so great. I changed my map to ice so that the bunny (kiko) looks like he is skating. I can't tell you how much I appreciate your help. People on this forum are so great with helping and sharing their codes. You and another member, Finogi, have helped me out so much. I can only offer my thanks. Wish I could offer more.
    Last edited by WinxClub; 04-04-2008 at 12:54 PM.
    Beauty is only a light switch away.

  8. #8
    Senior Member Zoranan's Avatar
    Join Date
    May 2008
    Posts
    126
    is this for a game your developing?

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