dcsimg
A Flash Developer Resource Site

Results 1 to 14 of 14

Thread: [F8] Problem with continual hit test

  1. #1
    Senior Member
    Join Date
    Apr 2004
    Posts
    132

    [F8] Problem with continual hit test

    I have amended edmacks flash file which enables a cannon to follow the mouse pointer and shoot bullets at varying speeds dependant on how long the mouse click is held for.

    I have 3 problems that need addressing :-


    1) CONTINUAL HIT TEST

    I have placed a circle on screen (as an MC) and have named it "hitarea".
    The hit test works, because I have traced "hit" - but it only works on the first fire. Not the second or third or whatever.

    I need some sort of amendment to ensure that no matter how many times the bullet is fired, the hittest will take place.


    2) DISAPPEARING BULLET

    Once the hittest is complete and the test returns true, I need the bullet to disappear. How is that achieved ?


    3) PLACING A SPLASH AT THE HIT AREA


    If the hittest returns true, I also want another MC to play at EXACTLY the position where the "hit" occurred.

    I have created 2 animation MC's - Splash 1 & Splash 2. How do I go about placing those animations randomly (So either 1 or 2 appears) at the co-ordinates of the hit ?!?!

    Any help will be most appreciated. Even if you can only answer 1, it will certainly alleviate my frustration.


    I have uploaded a .rar file at :-
    http://www.koit.pwp.blueyonder.co.uk...llet_mouse.rar
    for all those who don't want to open a random .fla from this site.



    Thanks one and all

    Koit
    Attached Files Attached Files

  2. #2
    Senior Member
    Join Date
    Apr 2004
    Posts
    132
    Can I also add in the following :-

    How can I restrict the angle of fire so that the turret only points certain places.
    Imagine 0 degrees is the turret pointing straight up, I want the turret to go from 10 degrees to around 110 degrees. So in essence, from almost up to pointing at an angle downwards.....

    How is that achieved ?

  3. #3
    Member
    Join Date
    Feb 2008
    Posts
    83
    On hittest, where you currently have trace ("hit"), add a removeMovieClip.
    As for why the hittest only works once, make the hittest use the instance of the movieclip, instead of just the first movie clip.

  4. #4
    Senior Member
    Join Date
    Apr 2004
    Posts
    132
    Thanks for the response.

    I tried this code

    _root.Bullet.removeMovieClip();

    but no joy.


    Additionally, I don't understand what you mean by the second part of your reply. Can you post an example so I can learn from ?

  5. #5
    Senior Member
    Join Date
    Apr 2004
    Posts
    132
    Please, anyone ?

  6. #6
    Student
    Join Date
    Apr 2001
    Location
    -
    Posts
    4,756
    how about just posting the code that is relevant? I rather avoid downloading and compiling otherŽs files. Only if there is no other way iŽd give it a look eventually

  7. #7
    Senior Member
    Join Date
    Apr 2004
    Posts
    132
    Thanks for the response. There's a lot of code there and to be fair, I wouldn't know which parts are relevant to the whole thing working.

    I know I'm being a pain but could you please look at the flash ?

  8. #8
    Senior Member
    Join Date
    Apr 2004
    Posts
    132
    Ok, here is the code that is in my Cannon movie clip

    --------------------
    onClipEvent(EnterFrame){
    //segLength = length of a segment. Increase or decrease as appropriate.
    _root.segLength = 49;
    _root.dx = hand._x - _root.upperarm._x + 200;
    _root.dy = hand._y - _root.upperarm._y + 200;
    _root.dist = Math.sqrt(_root.dx * _root.dx + _root.dy * _root.dy);

    _root.a = _root.segLength;
    _root.b = _root.segLength;
    _root.c = Math.min(_root.dist, _root.a + _root.b);

    _root.B = Math.acos((_root.a * _root.a + _root.c * _root.c - _root.b * _root.b) / (2 * _root.a * _root.c));
    _root.C = Math.acos((_root.a * _root.a + _root.b * _root.b - _root.c * _root.c) / (2 * _root.a * _root.b));
    _root.D = Math.atan2(_root.dy, _root.dx);
    _root.En = _root.D + _root.B + Math.PI + _root.C;

    _root.upperarm._rotation = (_root.D + _root.B) * 180 / Math.PI;

    _root.forearm._x = _root.upperarm._x + Math.cos(_root.D + _root.B) * _root.segLength;
    _root.forearm._y = _root.upperarm._y + Math.sin(_root.D + _root.B) * _root.segLength;

    _root.forearm._rotation = _root.En * 180 / Math.PI;
    }

    onClipEvent (enterFrame) {
    //calculate mouse point and adjust the angle of the mc to match.
    angle = (Math.atan2(_root._ymouse-_y,_root._xmouse-_x)*180/Math.PI)+90;
    _rotation = angle;
    idle;

    if(speed <0){
    speed++;
    }
    if (md) {
    if(speed < 50){
    speed += 2;
    }
    }
    else if(speed > 0){
    _root.idle = false;
    var n = "Bullet"+bc;
    speed += 10;
    _root.attachMovie("Bullet",n, bc);
    _root[n].vx = Math.sin(angle*(Math.PI/180))*speed;
    _root[n].vy = -Math.cos(angle*(Math.PI/180))*speed;
    _root[n]._x = _x;
    _root[n]._y = _y;
    _root[n]._rotation = angle;
    this.swapDepths(_root[n]);
    speed = -15;
    bc++;
    }
    }

    onClipEvent(mouseDown){
    md = true;
    }

    onClipEvent(mouseUp){
    md = false;
    }

    --------------------------------------

    HERE IS THE CODE THAT IS IN THE HITAREA, WHERE I WISH TO TEST FOR A HIT....
    ---------------------------------------

    onClipEvent (enterFrame) {
    if (this.hitTest(_root.Bullet._x, _root.Bullet._y, true)) {
    trace("hit");
    trace(_root.idle);
    _root.Bullet.removeMovieClip();
    bob++;
    }
    }


    Does that help in identifying where I'm going wrong !?!?

  9. #9
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,227
    In the line:

    if (this.hitTest(_root.Bullet._x, _root.Bullet._y, true)) {

    _root.Bullet is the name of movie clip that will be used for hitTesting. Since it is the name of first bullet, obviously all other bullets that have different names are not being tested.

    You will need to set up loop to test all bullets, something like this:
    PHP Code:
    for(0100n++){
       if (
    this.hitTest(_root["Bullet"+n]._x_root["Bullet"+n]._ytrue)) {
          
    //trace, remove etc here
       
    }

    Here the example expects you to reset bullet counter after it reaches 100, if you dont reset it the loop will slow down the game with more and more bullets. Other ways to test multiple bullets would be using array and object where each bullet is added when created and removed when it leaves the game.

    This kind of collisions have been discussed many times before, you may want to try searching the board.

  10. #10
    Senior Member
    Join Date
    Apr 2004
    Posts
    132
    That's great.

    BUT, my first bullet no longer works. I assume that this is something to do with swapdepths ?

    I have spent the last few hours trawling this site for answers without having to resort to more questions......but here I find myself once again.

    So :-

    A) How can I get my first bullet to work again (in a hit test type of way)
    B) How can I remove these bullets ?
    Once the hit test is complete (and I've traced it so I know they work) the removeMovieClip(); doesn't work.

    Here is my amended code :
    --------------------------------

    onClipEvent (enterFrame) {
    for(n = 0; n < 999; n++){
    if (this.hitTest(_root["Bullet"+n]._x, _root["Bullet"+n]._y, true)) {
    _root.amount = String(n);
    _root.newname = "Bullet" + _root.amount;
    _root.newname.removeMovieClip();
    trace("hit");
    trace(_root.idle);
    }
    }
    }

    ------------------------

    Lastly : C)

    How do I place a movie clip at the hit point ? - I have a movie clip in my library called "hit", how do I get that placed at every hit point from the bullet. They are essentially small explosion animations.



    Any help appreciated.

  11. #11
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,227
    1. Your variable bc is undefined so first bullet gets the name "Bullet", after its created bc gets value of 0 and is also increased by 1 so next bullet is Bullet1. Give default value 0 to the bc if its undefined:
    Code:
    if(bc == undefined){
    	bc = 0;
    }
    var n = "Bullet" + bc;
    2. _root["Bullet"+n].removeMovieClip();

  12. #12
    Senior Member
    Join Date
    Apr 2004
    Posts
    132
    Thanks for this tonypa - I thank you for the explanation too, that helps more than the code.

    Much obliged. The hit animations now play for every hit, which is nice.
    The removeClip also works but the very first bullet does not get removed on a hit. The rest do......

    Any thoughts ?

    Is there also a way to get my hit animation (explosion) play exactly at the hit point, rather than the registration point of the hit tested MC ?

    Imagine a large area to be tested for a hit, I could hit it at the top, middle and bottom and the hit explosion would always play in the middle. Anyway I can get it to play at the EXACT hit co-ordinates ?

  13. #13
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,227
    Try tracing the names of bullets or check them in preview window (list objects...).

    To make the animation appear at exact point of collision you would first need to find that collision point. Hittest function only says if 2 objects are covering each other, it will not give any information about the point of collision. Finding real collision point can be done using math but its much more complicated. You could look at my vector tutorials to see what kind of math gets involved:

    http://www.tonypa.pri.ee/vectors/index.html

  14. #14
    Senior Member
    Join Date
    Apr 2004
    Posts
    132
    Thanks for this - I think that I need to spend a lot of time digesting your examples from your tutorials.

    For now methinks I will break my target areas down into smaller MC's and achieve the same end results that way.

    Thank you.

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