A Flash Developer Resource Site

Results 1 to 10 of 10

Thread: Rotating a gun and fireing.

  1. #1
    booooosted! DJ-Anakin's Avatar
    Join Date
    Oct 2000
    Location
    ridgecrest, ca [pm me if in cali desert]
    Posts
    149
    We are making a game with a gun that we wish to rotate and fire out of. We are able to rotate the gun but are having trouble getting a movieclip to move from the gun in the correct angle and continue to go at the same angle. Please help!

  2. #2
    Junior Member
    Join Date
    Jun 2001
    Posts
    26
    perhaps, try this;

    if you have the dergree of the gun in the variable n (0-360) then try this mathematical solution:

    if (n>180) {
    ys=180-(n-180);
    } else {
    ys = n;
    }
    if (n<90) {
    xs=180-n;
    } else {
    if (n>270) {
    xs=270-(n-270);
    } else {
    xs=n;
    }
    }
    yv=(ys-90)/90;
    xv=(xs-180)/90;

    what you get in yv and xv is between -1 and +1 so you can use these two variables as a vector. if you add yv constantly frame by frame to the ypos of your movie and do the same with xv and the xpos, then it'll move in the direction you want (I hope so, but have no time to try it...). you can multiply the two vectors with a higher value, so it'll move faster.

    have fun,
    .tobi.

  3. #3
    booooosted! DJ-Anakin's Avatar
    Join Date
    Oct 2000
    Location
    ridgecrest, ca [pm me if in cali desert]
    Posts
    149

    Thank you, but...

    I'm having a hard time implementing it to my movie. It would be great with an example or something. I guess I don't really understand how your script works.

  4. #4
    Junior Member
    Join Date
    Jun 2001
    Posts
    26
    I'll give it a try -

    you have a MC called "gun" and a MC called "bullet" all in the root-timeline, jump to that script if the fire-button is pressed (or use it as cript in a button or so).


    //getting the rotation of gun

    _root.n = _root.gun._rotation;
    _root.bullet._x = _root.gun._x;
    _root.bullet._y = _root.gun._y;

    // calculating the vectors

    if (_root.n>180) {
    _root.ys=180-(_root.n-180);
    } else {
    _root.ys = n;
    }
    if (_root.n<90) {
    _root.xs=180-_root.n;
    } else {
    if (_root.n>270) {
    _root.xs=270-(_root.n-270);
    } else {
    _root.xs=_root.n;
    }
    }
    _root.yv=(_root.ys-90)/90;
    _root.xv=(_root.xs-180)/90;

    gotoAndPlay (here is the place you came from....only when script is in individual frame);

    I hope, that flash can get the _rotation of an object, otherwise you have to buffer the rotations done by the user in a varaible like the _root.n ... now you should have the two vectors and you should add two your script in the frame, that is controlling the gun-rotation and your whole game (for 800x600 res):

    if ((_root.bullet._x > 0) and (_root.bullet._x < 800) and (_root.bullet._y > 0) and (_root.bullet._y < 600)) {
    _root.bullet._visible = true;
    _root.bullet._x += (_root.xv * 5);
    _root.bullet._y += (_root.yv * 5);
    } else {
    _root.bullet._visible = false;
    }


    okay, the 5 determines the speed of the bullet and can be changed. if you need more than one bullet, make a pointer-variable, that buffers the number of the last-used bullet and assign it to a for-loop, the syntax for an for on the variable i would be _root["bullet"+i]._x for example.

    good luck,
    .tobi.

  5. #5
    Senior Member
    Join Date
    May 2001
    Posts
    1,838

    Re: Thank you, but...

    There are many ways. I dont think there is "standard" answer. I do by my way.

    You want example. OK, I did one for you. Not beautiful, but just for example.

    www.geocities.com/~dr_ericlin/aaa/gun.html
    www.geocities.com/~dr_ericlin/aaa/gun.fla

  6. #6
    Junior Member
    Join Date
    Jan 2001
    Posts
    14

    Lightbulb really best script at ultimatearcade!!

    you can find it here:
    projectile motion - is the one
    http://www.ultimatearcade.com/ua/members/1/

  7. #7
    booooosted! DJ-Anakin's Avatar
    Join Date
    Oct 2000
    Location
    ridgecrest, ca [pm me if in cali desert]
    Posts
    149
    ok ericlin, i tried your movie clip, changed the button to a Key.Space blah blah, but the only thing is that i need it to stop rotating automaticly, and be able to change where its aiming, by key.Up or key.Down. then, that whole gun needs to go inside the MC of the character thats shooting the gun..ive tried chaging it, but no luck so far. you wrote it..any ideas?

  8. #8
    Senior Member
    Join Date
    May 2001
    Posts
    1,838
    If gun in tank, then the position of gun in _root coordinate will be _root.tank._x+_root.tank.gun._x; If tank also got modification of _rotation, then I will use localToGlobal function for calculation.

    I try change the codes as little as possible for comparison.

    www.geocities.com/~dr_ericlin/aaa/gun2.html

    http://www.geocities.com/~dr_ericlin/aaa/gun2.fla

  9. #9
    booooosted! DJ-Anakin's Avatar
    Join Date
    Oct 2000
    Location
    ridgecrest, ca [pm me if in cali desert]
    Posts
    149

    Ericlin: What's 'pop'?

    in your code, it says 'pop'. what does pop do? we've got everthing workin except that everytime you hit space to make it fire, it removes the previous shot you fired.

  10. #10
    Senior Member
    Join Date
    May 2001
    Posts
    1,838

    Re: Ericlin: What's 'pop'?

    In previous version, I had two line, trace(idArray) and trace(ID); Because, when I first made it go, it removed previous hit. So, I traced them, and the result is I got wrong path;I fixed it and then well. Please check the script where pop and push lies, besure the target path is right for the array.

    Why use push and pop ? My plan is "recycle";

    You can do as usual script, ID++, duplicateMovieClip("MC","MC"+ID,ID), and omit the array, delete pop and push things. When bullet disappear, you just say this.removeMovieClip(); That is OK. I like "recycle" just because I dont like the ID to be too large.

    I use "recycle". I explain here. I set an array containing 1-12 depth; You can create bullet of various depth. First one is MC12 in depth level 12, that is Array.pop(); Then the last element of the array will be 11. So, the second bullet pop the array and get 11; the third pop will get 10; Then the last element of the array will be 9; If at this time, the bullet MC12 dies, it would push "12" to the array, and dies. The last array will be 12; If you trace array, the value will be : 1,2,3,4,5,6,7,8,12; Now, you fire a bullet again, the bullet will be MC12, not MC8; That is what I mean "recycle";

    Recycle is not necessary, anyway.

    OK, I removed the recycle things. You can access the page in the same address as previous post.

    I do not know the definite URL syntax in this forum. Download of file by cliking the hyperlink sometimes works sometimes not. Anyway, the file is upload there.
    [Edited by ericlin on 06-23-2001 at 10:53 PM]

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