A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: Help me with this problem plz...

  1. #1
    XRave tongxn's Avatar
    Join Date
    Apr 2005
    Location
    Somewhere near Here
    Posts
    870

    Help me with this problem plz...

    Good morning, good afternoon and good evening.

    i am developing a platform game, but I currently faced the problem of laser shots being fired in the direction of the mouse.

    Is there any way of getting the mouse angle and duplicate the clip?

    My laser mc is made by a tween inside movieclip. with the laser starting at CenterCrosshair of my blob, and ending 800 pixles to the top of the movie. with the AS (stop()

    there are no GUN movieclips. the laser should be originated at the character alone (character is a blob).

    so does anyone know how to duplicate MCs to mouse angles? and to get the mouse angle from the blob at least??

    The blob will be moving.. so how does the mouse-angle detection work?

    this is the AS i'm usin NOW for i can't seem to get the mouse angle. the laser is a MC with a tween of a laser shooting straight up... so -

    onClipEvent (enterFrame) {
    if (Key.isDown(Key.SPACE)) {
    duplicateMovieClip("laser", laser1, 1);
    _root.laser1._y = _root.hero._y;
    _root.laser1._x = _root.hero._x;
    }
    if (_root.laser1.hitTest(ufo, true)) {
    removeMovieClip(ufo);
    }
    }

    mmm i exported laser..

    what's wrong?

    Regards
    TONGXN
    When you actually know what "OMG I have so much homework!" means, you won't want to be me.
    Xrave

  2. #2
    All 1s and 0s dmonkey's Avatar
    Join Date
    Nov 2005
    Location
    Leeds, UK
    Posts
    606
    Hi,

    Getting the mouse angle is actually quite simple. The geniuses over at Macromedia gave us a dead useful function called atan2(). It returns the angle of the mouse to whatever you want as long as you put the right things in. The syntax is:

    code:

    Math.atan2(difference in y , difference in x);



    So in your case, the angle would be found by:

    code:

    angle = Math.atan2( (_root._ymouse - _root.hero._y) , (_root._xmouse - _root.hero._x) );



    Flash likes to calculate angles in radians, but likes to rotate things using degrees, so you need to convert.

    code:

    angle = angle / Math.PI * 180;



    You might also find you need to add or subtract 90 degrees, since Flash measures angles from the horizontal, not from the vertical.

    Hope this has helped!
    Last edited by dmonkey; 04-01-2006 at 06:29 PM. Reason: Made a mistake with the syntax
    "If I have seen further, it is by standing on the shoulders of giants." - Sir Isaac Newton

  3. #3
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,448
    Never mind, i deleted what i wrote, too long at the computer.
    Last edited by cancerinform; 04-01-2006 at 01:52 PM.
    - The right of the People to create Flash movies shall not be infringed. -

  4. #4
    XRave tongxn's Avatar
    Join Date
    Apr 2005
    Location
    Somewhere near Here
    Posts
    870
    so - what about my code? I am a complete to much of an amature...
    When you actually know what "OMG I have so much homework!" means, you won't want to be me.
    Xrave

  5. #5
    All 1s and 0s dmonkey's Avatar
    Join Date
    Nov 2005
    Location
    Leeds, UK
    Posts
    606
    Hi,

    In your code, you need to write the full path to "laser". DuplicateMovieClip doensn't load exported clips. If you want to do this, use attachMovie. However, if you do use attach movie, the movie will be attached to the clip you are writing the code on, so the path is not longer _root.laser1, it's this.laser1.

    Secondly, "laser1" should be in double quotes.

    Finally, you've written the hitTest wrong. Here's how it should all look.

    code:

    onClipEvent (enterFrame) {
    if (Key.isDown(Key.SPACE)) {
    attachMovie("laser", "laser1", 1);
    this.laser1._y = _root.hero._y;
    this.laser1._x = _root.hero._x;

    angle = Math.atan2( (_root._ymouse - _root.hero._y) , (_root._xmouse - _root.hero._x) );
    angle = angle / Math.PI * 180; //you might have to add or takeaway 90

    this.laser1._rotation = angle;
    }
    if (this.laser1.hitTest(ufo) == true) {
    removeMovieClip(ufo);
    }
    }



    Hope this helps.
    "If I have seen further, it is by standing on the shoulders of giants." - Sir Isaac Newton

  6. #6
    XRave tongxn's Avatar
    Join Date
    Apr 2005
    Location
    Somewhere near Here
    Posts
    870
    onClipEvent (enterFrame) {
    if (Key.isDown(Key.SPACE)) {
    attachMovie("laser","laser1",1);
    this.laser1._y = _root.hero._y;
    this.laser1._x = _root.hero._x;
    angle = Math.atan2((_root._ymouse-_root.hero._y), (_root._xmouse-_root.hero._x));
    angle = angle/Math.PI*180;
    //you might have to add or takeaway 90
    this.laser1._rotation = angle;
    }
    if (this.laser1.hitTest(ufo) == true) {
    removeMovieClip(ufo);
    }
    }

    nope not working, i think something's wrong.
    When you actually know what "OMG I have so much homework!" means, you won't want to be me.
    Xrave

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