Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: AS2 Help Request - Line that follows mouse.

  1. #1
    Jaxn Fury
    Guest

    AS2 Help Request - Line that follows mouse.

    Hi guys, i'm a new member here on theCAFE, i've been browsing the forums looking for help on a particular piece of code i am stuck on. Its for a college project and i see that you guys seem to know your stuff so i thought you may be able to help me.

    I'm making a fairly small puzzle game for college and one of the parts of it is a wire game, here is a pseudocode description of it.

    1.You click on a coloured connector, when you do it draws a coloured wire (a simple line) to your mouse pointer like a piece of string.

    2.You must take that coloured wire and click on the corresponding coloured connector at the other side.

    3.If the connector is the right colour the line snaps to the connector and stays there.

    4.If the connector is the wrong colour the line snaps back to the original connector and you must start again from step 1.

    This would be required to work in ActionScript 2.0.

    At the moment i have virtually no idea how to do this as this is probably quite ambitious for what is required for my course (i like to try and be fancy).

    I have found code for simulated string but it is extremely complex and i also don't know if i could add the interactivity to it that i need.

    Any help would be great, and if someone could throw together some working code that i could reuse that would be even better.

    Regards,

    Jaxn Fury

  2. #2
    Programming Ninja mkeefe's Avatar
    Join Date
    Feb 2003
    Location
    Boston
    Posts
    7,794
    I recommend you break the application into its pieces, such as:

    1. Dragging an object with a mouse
    2. Detecting when an object is over another (hit test)
    3. Verifying the correct object is being hit (conditional statement)
    4. Animation or scripted motion for the wire

    Hope this helps,

    Matt

  3. #3
    Jaxn Fury
    Guest
    Hi mkeefe,

    Thanks for the reply.

    The breaking it down is fairly simple, it's the actual code that puzzles me, i just don't know what code to use.

    The parts that i am mostly stuck with is the code for;

    When connector is clicked....
    Set a start point for the wire (line)

    Mouse cursor is moved.....
    Draws a coloured line to mouse cursor

    Next connector clicked....
    The line snaps to connector if correct colour
    else......
    The line disapears/clears if wrong colour is clicked (start again)

    Some notes;

    The wire isn't an object, so it isnt drag and drop.
    The wire just needs to be a simple, straight line - drawn by flash.
    The connectors are objects (the start and end point of the line).

    I know i'm asking for you to do alot of this code for me but if i can be provided with the basics of what i need i should be able to take it further.

    Thanks,

    Jaxn Fury

  4. #4
    Boss Man kiwicolin's Avatar
    Join Date
    Aug 2002
    Location
    Irvine, California, United States
    Posts
    10,302
    Hmm been a while since I wrote this code, but here's the parts I remember off the top of my head...

    init();
    function init ()
    {
    lineStyle(2,1,60);
    }
    function onMouseDown ()
    {
    moveTo(_xmouse,_ymouse);
    onMouseMove = draw;
    }
    function onMouseUp()
    {
    delete onMouseMove;
    }
    function draw()
    {
    lineTo(_xmouse,_ymouse);
    }



    To get rid of mouse cursor and replace with a dot (create the dot symbol in the library first and use properties to name it dot)


    function onEnterFrame()
    {
    attachMovie("dot","dot",100);
    dot._x =_xmouse;
    dot._y = _ymouse;
    Mouse.hide();
    }

  5. #5
    Jaxn Fury
    Guest
    Hi kiwicolin,

    Thanks also for the reply, however this is a little different than what i was thinking of.

    I decided to throw together a small pictoral guide (the swf file below) to help show you what i mean.

    Have a look and let me know if you have any questions.

    Regards,

    Jaxn Fury

    Wire Example.swf

  6. #6
    Programming Ninja mkeefe's Avatar
    Join Date
    Feb 2003
    Location
    Boston
    Posts
    7,794
    Here is some starter code that may help you:

    Code:
    // Create a container
    var line:MovieClip = this.createEmptyMovieClip("lineContainer_mc", 10000);
    
    // Create mouse listeners
    var mouseListener:Object	= new Object();
    mouseListener.onMouseDown	= showLine;
    mouseListener.onMouseUp		= deleteLine;
    Mouse.addListener(mouseListener);
    
    // Called when the user presses the mouse down. Creates the line
    function showLine():Void
    {
    	line._x = _xmouse;
    	line._y = _ymouse;
    	line._visible = true;
    	line.createEmptyMovieClip("line_mc", 2);
    	line.onEnterFrame = drawLine;
    }
    
    // Called when the mouse is released. Deletes the line and line drawing event
    function deleteLine():Void
    {
    	delete line.onEnterFrame;
    	line.line_mc.removeMovieClip();
    }
    
    // Redraws the line, snapping to the location of the mouse cursor
    function drawLine():Void
    {
    	with(line.line_mc)
    	{
    		clear();
    		lineStyle(5, 0x000000); // stroke width, color
    		moveTo(0, 0);
    		lineTo(_xmouse, _ymouse);
    	}
    }
    PS: Congratulations, this is the first AS2 I had to write this year... I was like a lost coder in a jungle of variables I didn't know!

    AS3 forever

    Matt

    [Edited on 2/24/2009 by mkeefe]

  7. #7
    Jaxn Fury
    Guest
    Excellent mkeefe, had a quick look at this and its great. I'll play about with it and let you know how i get on.

    Thanks very much,

    Jaxn Fury

    P.S. Only using AS2 as tutors don't know enough AS3 yet - spent loads of money on AS3 books for college too..... sigh.

    (EDIT)

    Also - thank you so much for your time guys, really appreciate the quick responses.

    JF

    [Edited on 2/25/2009 by Jaxn Fury]

  8. #8
    Boss Man kiwicolin's Avatar
    Join Date
    Aug 2002
    Location
    Irvine, California, United States
    Posts
    10,302
    LOL I hear you Matt. Haven't done AS2 in a while either

  9. #9
    Programming Ninja mkeefe's Avatar
    Join Date
    Feb 2003
    Location
    Boston
    Posts
    7,794
    Originally posted by Jaxn Fury
    P.S. Only using AS2 as tutors don't know enough AS3 yet - spent loads of money on AS3 books for college too..... sigh.
    This has always annoyed me both as a part-time teacher and developer, that schools do not keep up to date. You gotta work with what you have, but I strongly recommend you learn AS3. However, be careful you cannot simply "move over".

    I recommend picking up the DVDs that Colin offers here at PhotoshopCD.com

    Good luck

    Matt

  10. #10
    Jaxn Fury
    Guest
    Yeah i fully intend to learn AS3, i just need to know enough AS2 to get these college projects done.

    After all - i did spend all that money.

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
  •