Character movement with mouse click

This will be for an MMO online game/ world. So it needs to be good.
Hi guys. I'm really happy I got this to work as I'm a neebie to AS3 but getting to love its simplicity. My problem is not my AS3 but my maths.
I click the mouse and the movieclip runs to the mouse's x position. However, it only goes in one direction ie: downwards and to the right.
I need to it go in all directions depending on where I click. I suppose it has to do with comparing x and y positions of both mouse and mc - ie: if one is bigger than the other then you can tell where both are positioned and then put the code in. Am I right and does anybody hace a little code like this flying around for me?
import flash.display.Stage;
import flash.events.Event;
stage.addEventListener(MouseEvent.CLICK, myClickReaction);
function myClickReaction (e:MouseEvent):void{
    sunny.gotoAndPlay("runback")
    //sunny.x = root.stage.mouseX
    //sunny.y = root.stage.mouseY
    addEventListener(Event.ENTER_FRAME, onEnterFrame);
function onEnterFrame(event:Event):void {
             var xDistance:Number
             var yDistance:Number
            //mc increments by 5 until it reaches mouse - mc (ie the distance)
            sunny.x += 5;
            sunny.y += 5;
            xDistance = root.stage.mouseX - sunny.x
            trace (xDistance)
            if (xDistance <= 0 ) {
            trace("works")
            sunny.gotoAndPlay("static")
            removeEventListener(Event.ENTER_FRAME, onEnterFrame);

Thanks for contacting but the code more or less works already apart from the comments below.
The position I get comparing mousex position and movieclip position. BUT it goes loopy when I move the mouse around. ie: I need the coordinates when I click and not a variable that changes while I move mouse around.
I will check it out now.
import flash.display.Stage;
import flash.events.Event;
stage.addEventListener(MouseEvent.CLICK, myClickReaction);
function myClickReaction (e:MouseEvent):void{
//sunny.x = root.stage.mouseX
//sunny.y = root.stage.mouseY
addEventListener(Event.ENTER_FRAME, onEnterFrame);
function onEnterFrame(event:Event):void {
             var xDistance:Number
    var yDistance:Number
   //mc increments by 5 until it reaches mouse - mc (ie the distance)
            // if you click to the right of sunny then mouse is > than sunny.x so x = 5
   if (root.stage.mouseX > sunny.x) {
    sunny.gotoAndPlay("walk right")
    sunny.x += 5;
    sunny.y += 0;
    xDistance = root.stage.mouseX - sunny.x
    //if (xDistance <= 0 ) {
    //sunny.gotoAndPlay("static")
    //removeEventListener(Event.ENTER_FRAME, onEnterFrame);

Similar Messages

  • Dropped Shadow Movement on Mouse Click

    I have a rollover graphic that changes states with mouse-over.  The graphic has a dropped shadow given the illusion of depths and as the mouse clicks on the graphics I would like this shadow to move up and to the left.  This would give the illusion of the graphic being pushed down and thereby further the depth perception.  I made the rollover through the rollover dialog box in Dreamweaver.  The entire page is made in Photoshop, sliced and then imported to Dreamweaver. The rollover graphic is one slice in this document and it works fine. However, I cannot figure out how to insert the dropped shadow in the existing slice so that I can attache the required move script in the behaviors tab.  Furthermore, the independent dropped shadow file need to be moved, affected, by the click on the overlaying rollover graphics.
    I am neither a savvy web developer nor a savvy Dreamweaver user so any input and help is appreciated and PLEASE don’t assume that I know anything about any of this = break it down to the most fundamental steps.
    Thank you

    Thanks for the fast reply
    However, this is way above my head as an extreme beginner on Dreamweaver.
    How do I implement the CSS formatting?
    What is a pseudo-class?
    How do I import the images needed?
    I believe three graphics; one basic neutral, one change for mouse over and then maybe a separate graphic for the shadow that is then simply disabled on the click on the previous graphics.  Or however you suggest that this should be executed.
    Any help and input is appreciated and PLEASE assume that I know nothing about any of this so basics and fundamentals are appreciated.

  • Controlling movie with mouse location

    I am a designer trying to be a flash programmer and I have a
    navigation idea that I've been struggling with. I am simply trying
    to do this: put a movie on the stage and as the mouse moves closer
    or farther from the movie, it plays forwards and backwards. The
    movie I made is a circular logo with navigation buttons curling out
    from it like a jack knife. My idea is to have the buttons curl out
    as the user moves the mouse closer to the logo and curl back in as
    the mouse moves away.
    I understand the coding concept - calculate where the cursor
    is on the stage, figure out the distance of the cursor to the logo
    movie, and just go to certain frames in the logo movie based on
    that distance.
    I have the stage, the logo movie, and even the code to figure
    out the the mouse location. But beyond that, I need help. I just
    don't know enough about actionscript syntax and although
    conceptually this seems like a basic exercise, I really don't know
    where to start. If anyone is willing to help, the .fla file can be
    downloaded here:
    http://0099d33.netsolhost.com/westside-home2.fla
    Thanks so much for any help you can offer.
    null

    You would replace the one mDistance line in your original
    with the three I've given you. But if you weren't able to figure
    that bit out, you might not be able to figure out how to modify the
    whichFrame line either? Do you have a conceptual understanding of
    what your code is doing? Being able to read the code and understand
    what it is doing is really central to Actionscript. So here goes:
    mcLogo.onEnterframe: this is attaching some code that will
    repeat at the frame rate of the movie to the mcLogo instance. Even
    if there is a stop() on the timeline, this code will continue to
    repeat at the framerate set by your movie. By attaching it to
    mcLogo your scope will be mcLogo.
    In both version the next line (or three in my case) say find
    the distance between the current mouse position and this._x (for
    example). Since we are inside an onEnterFrame which is attache to
    mcLogo, so "this" refers to that time line. So this._x is saying
    the _x location of the mcLogo clip.
    If you remember basic geometry a right hand triangle's long
    side (the hypotenuse) is given by h^2 = a^2 + b^2. (The square of
    the hypotenuse is equal to the sum of the squares of the other two
    sides.) So we are figuring out how far between mcLogo and the mouse
    in the x and the y directions. This gives us the other two sides.
    So the distance between the mouse and the clip is the square root
    of the sum of the squares of those two sides.
    Finally since you need to adjust the distance to some scale
    of the number of frames you have added to your clip. So your have
    some code that makes the distance proportional to the number for
    frames in your clip.
    As for the second part of your question, "Don't use scenes!"
    So that makes that easy. Scenes are great tools for organizing your
    timelines when you are just doing some timeline-based animations.
    But when using Actionscript there are some bugs and limitation in
    scenes and you are only asking for trouble.
    So if you have this code say on frame ten with a stop().
    There is no looping that needs to happen. The onEnterFrame will
    continue doing the animation until someone clicks.
    Add maybe ten blank frames after 10 and then on frame 20 put
    your new thing you want – perhaps even give it a frame label
    called, "home" or "about" or whatever section it is about. Then you
    can use:
    this.gotoAndPlay("about");
    Also don't forget that you will want to remove the
    onEnterFrame event because otherwise it will keep running. So you
    should probably also include:
    delete mcLogo.onEnterframe;

  • Problems with mouse clicks..

    Hello.
    i�m having problems with the mouse clicks in a component...
    i have events that occur when a click is given and others when 2 clicks are given, but these events occurs only once a while....it�s not always that they occur...
    can anyone help me with it?? it�s very strange...
    thanx

    How about copy and pasting your mouseClicked or mousePressed methods. Are you sure you know the difference between a click and a press?

  • Firefox 9.0.1 with Flash player not catch keyboard event with mouse click.

    In a flash application, I need select mutil items using shite key and mouse click, it is not working. looks like firefox didn't pass the keyboard even alone with the click event. This happen on Window 7, IE and Chrome have no problem. only FF

    i have the same problem everytime i open a website with flashcontent firefox freezes and i have to kill the flash process to unfreeze the browser the only way i can see a youtube video is in html5 mode.
    Hardware
    Acer aspire 4553
    Turion X2 p520
    3gb of ddr3 ram
    Ati Mobility Radeon 4250

  • How to control the speed of nested movie with mouse

    I have a movie nested inside another movie with pictures that
    scroll horizontally across the screen. I would like the user to be
    able to control the speed and direction of the pictures by moving
    their mouse. Can anyone give me some insight on where to look to
    find out how to do this?
    If this can't be done then perhaps add controls on the screen
    to stop / play / speed up / slow down the horizontal scrolling
    pictures

    it can be done. search for a scrolling movieclip tutorial
    using google or at flashkit.com

  • Issues with mouse clicks/jumps after using CleanMyMac App.

    Since using this app. my hitherto slightly slow responding iMac has required multiple mouse clicks and is less/unresponsive.
    Anyone else used this software?
    As yet have not approached CleanMy Mac.
    I have reinstalled Mountain Lion (download) not clean install.
    Any suggestions? Is there a separate app for mouse that I can reload. Im guessing a minor chunk of code was deleted along with rubbish.
    Reg25x

    Uninstall CleanMyMac and do not reinstall it.
    CleanMyMac is one of a broad category of time- and money-wasters capable of causing system corruption that can only be rectified by reinstalling OS X, restoring from a backup, or completely erasing your system and rebuilding it from the ground up. Get rid of it and test your Mac for operation. If it does not perform normally, the possibility that Cleanmymac resulted in system corruption must be considered.
    The vast majority of Mac problems reported on this site are the direct result of having used garbage like that. Never install such junk on a Mac.

  • Desktop moves with mouse!

    A friend reported to me from work that when she mouses, the whole desktop on her computer (10.4.11, but not sure what model, maybe a G4 tower) moves with the mouse movement! Is this a known issue? Any solution?

    Hi there,
    It sounds as if she might have accidently enabled the zoom feature. This can be toggled on and off using the cmdoption8 combination or in the 'Universal Access' pane of System Preferences. The zoom level can be controlled with the cmdoption'=' or cmdoption'-' keyboard combinations.
    Yang

  • Help, How do i get objects to move with the click of a mouse button?

    I am developing a game where by an object can move around the screen by either using the keyboard or the mouse. I have figured out how to control the object using the keyboard. But the mouse control is proving difficult. Can anyone give me some suggestions on how to tackle this?

    This is better !:
    import java.awt.*;
    import java.awt.geom.*;
    import java.awt.event.*;
    public class MoveO extends Frame implements MouseListener
         Label obj = new Label("@");
    public MoveO()
         setBackground(Color.pink);
         addWindowListener(new WindowAdapter()
        {     public void windowClosing(WindowEvent ev)
              {     dispose();
                   System.exit(0);}});
         setLayout(null);
         add(obj);
         obj.setBounds(110,130,20,20);
         obj.setBackground(Color.red);
           setBounds(1,1,600,460);
         setVisible(true);
         addMouseListener(this);
    public void mouseEntered(MouseEvent m){}
    public void mouseExited(MouseEvent m) {}
    public void mouseClicked(MouseEvent m){}
    public void mousePressed(MouseEvent m){}
    public void mouseReleased(MouseEvent m)
         move2(m.getPoint());
    private void move2(Point mto)
         Point ofr = obj.getLocation();
         Line2D.Double line = new Line2D.Double(ofr.x,ofr.y,mto.x,mto.y);
         Rectangle2D.Double r = new Rectangle2D.Double();
         r.setRect(line.getBounds2D());
         if (mto.x >= ofr.x) 
              for (double x=0; x <= r.width; x++)
                   for (double y=0; y <= r.height; y++)
                        if (line.ptSegDist(r.x+x,r.y+y) < 0.25)          
                             obj.setLocation((int)(r.x+x),(int)(r.y+y));
         else
              for (double x=r.width; x >= 0; x--)
                   for (double y=r.height; y >= 0; y--)
                        if (line.ptSegDist(r.x+x,r.y+y) < 0.25)          
                             obj.setLocation((int)(r.x+x),(int)(r.y+y));                
    public static void main (String[] args) 
         new MoveO();
    }Noah

  • Frozen icons - files will not move with mouse.

    Hello, all.
    All of a sudden I noticed that the icons on notebook's desktop have stopped responding to my mouse commands. I've tried with the built-in trackpad as well as with an external mouse. I've also tried to create a test user but the same happens.
    I have also booted the system with Leopard's installation DVD and verified/repaired permissions to ensure that it wasn't the problem but the same continues to happen. I also verified read and write rights to these folders but nothing seems to be out of order (at least nothing that I can see).
    This started happening after the internal drive became full and my wife deleted several photos stored in iPhoto.
    Any ideas of what might be the cause and how to fix it ?
    Thank you in advance for your help.
    Joseph

    Just upgraded to 10.5.8 today and suddenly this problem appeared.
    The thread starter tried his best to explain the problem but everyone seemed to be suggesting things that were not responsive to this problem (yet plausible overlooks for a n00b on OS X)
    Let me first say that I have been using a Mac since 1988, and OS X since the day the Public Beta released. So, I know all the little tricks to try like zapping the PRAM, checking the align to grid settings and other stuff we used to try, just before disabling extensions and rebuilding the desktop :P
    Here's what I am seeing: Any icon displayed in icon view is selectable, but immovable. You can double click on the icon to launch it, right click on it...do everything except move it. Even tried some of the old favorites like holding down a modifier key and dragging. Nope, still locked down.
    I looked deep to find if there wasn't some kind of "Lock icons in place" setting in the 10.5.8 update that accidentally got enabled by default.
    Also tried starting up with no battery or other accessories plugged in (after zapping the PRAM a few times).
    Tried logging in to another account on the machine to see if it was my user account that was corrupted - same results in my son's account.
    I am running a MacBook Pro 2.33 with 10.5.8 and 2GB of RAM.
    This seems suspiciously tied to the update, but it may be a nasty coincidence. I have been holding off on installing my Snow Leopard upgrade because of the CS3 issues I have heard about. I hope this is not the "force point" on that. And rebuilding my drive from scratch? Ugh! I think I would just move to an "off the grid" commune instead of facing that one.
    Any help would be greatly appreciated.
    Thanks!
    Mike
    UPDATE: this problem is affecting all dragging, not just icons. I just tried to drag the URL for this thread from the address bar to the desktop for later reference. Nope. Allows me to double click to select it, but not drag it.
    Message was edited by: Fiodan
    Message was edited by: Fiodan

  • Trouble with mouse click

    I used to have a LOGITECH but reverted to the wired APPLE mouse that came with my computer. My problem is when I try to click on a file ONCE to : let's say change a file name or edit a file ...it'll open a file in a program I didn't command it to. A good example are sound files I'll try to edit an audio file name but the single click causes QUICKTIME to open the file and play it...very annoying when you are try to navigate your files. Thanks

    yes but the problem exists even going back to the old mouse with wire. I need to reiterate that the problem didn';t exist in early summer when I was using the LOGITECH mouse only sometime during June I began to notice the trouble and it's there with the (wireless) LOGITECH and/or APPLE mouse (wired USB). Mystery unsolved. Thanks for responding.

  • Desktop files won't open with mouse click

    I'm sure this has an obvious solution, but I can't find it.
    (I'm on a new Powerbook G4 running Tiger)
    When I try to open a folder or file on my desktop by double clicking it, nothing happens.
    However, when I click once on the folder/icon to highlight it, then hit Command + O, it opens/launches.
    I want to be able to open files with my touchpad without having to use the keys. Is there a setting for this?
    Thanks!!!!
    Powerbook G4 15"   Mac OS X (10.4.1)  

    You might want to go into the System Preferences under "Keyboard & Mouse" and check your double-click settings. Also, if I understand correctly, to click using the trackpad itself, you can select "Clicking" under the trackpad options.
    PowerBook G4 12 1.5GHz and a bag full of goodies!   Mac OS X (10.4.4)   80GB HD, 512 MB ram, 300GB Maxtor OneTouch II FireWire 400 External HD.

  • Action Script to restart movie with mouse

    I added code using Script Assist in Flash CS4 to pause the
    movie at the end frame until the user clicks on the mouse to
    restart it. Seemed so easy but I have tried everything I can think
    of and nothing works. I actually did this in an earlier version of
    Action Script and now I am lost. The code I used is attached. Any
    help is greatly appreciated!
    Richard

    Installed and working. Thanks for the code!

  • Camera moves with mouse

    Hi guys,
    Just a quick post with HOPEFULLY a quick and helpful answer.
    See
    the background effect on this game which moves the background
    when the mouse is moved, I would love to know how to make something
    similar to that. Can you either, Direct me to a tutorial that will
    teach me how to do this OR do you know the script to do this?
    All help is appriciated!
    Thankyou!

    well, if you really want to duplicate the effect of your
    link, just use a scrolling movieclip whose scrolling is a function
    of the mouse position.
    the class below sets up the scrolling for a member of the
    class. usually a mask is used to display the scrolling movieclip
    and this class requires one. just use a mask movieclip the same
    dimensions as your stage:

  • ListBox/Xcontrol with mouse click but no dragging

    Bonjour,
    Je suis actuellement en train de paufiner mon IHM et j'aimerais résoudre un petit problème concernant ma listbox. Cette listbox est contenue dans un Xcontrol mais c'est un détail.
    Ce que j'aimerais effectuer c'est de pouvoir selectionner les élements de ma listbox avec la souris mais ne pas permettre de dérouller la scrollbar lorsque je glisse ma souris vers le bas ou vers le haut.
    Est ce que je peux faire celà avec un noeud de propriété (solution facilitée) ou bien suis je obligé de coder la fonctionnalité ?
    Si vous voulez un petit exemple, voilà ci dessous ce que je peux vous proposer.
    Oops, it's in the wrong community forum.
    So, I can try to explain that in english.
    Hello,
    I'm trying to create a listbox where I can select my item by clicking on it but I don't want to select an item, dragging down et seeing my scrollbar going down. 
    Is there a property node to do that or am I obbliged to developp this fonctionnality ?
    Thank you.
    Note : I don't want to disable it because I'll not be able to select an item by clicking on it.
    Attachments:
    Exemple defillement.vi ‏9 KB

    Sorry just got home from very long drive and tired . My question doesn't make sense. Restarted computer upon returning home . Prior to leaving house did lose power due to ice storm and wonder if that did something.
    Anyway, it's not the mouse that's not working it's the wireless keyboard. I can't get into my computer because I cannot even type my password. No icons are available to me to click on to open computer or check computer. No apple icon for example. Only thing is top right of screen showing WiFi on full bars and the time. The little green light in wireless keyboard flashes intermittently. I have tried shutting down computer, unplugging etc. Nothing is working. Really need computer tomorrow. Time sensitive work to do. Thinking I might have to take a trip to the Apple store in morning?
    Using my phone right now to type this.

Maybe you are looking for