A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: Action if speed is greater than...

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    5

    Action if speed is greater than...

    Hello,

    I wonder if there is a way in actionscript 2 that makes something happen if an object hit another object with a specific speed. For exemple:

    If Object 1 hits Object 2 with a speed greater than 10 I want something to happen.



    Thank you!

  2. #2
    Senior Member
    Join Date
    Apr 2009
    Posts
    138
    Actionscript Code:
    function collisionDetection() {
    if (mc1.hitTest(mc2.testhit)) {
    if (mc1_speed > 10){
    trace ("Object one has collided with object two while traveling at "+mc1_speed);
    }else{
    trace("Object one and two have had a low speed collision (less then 10)");
    }
    }
    /*
    If you remove the above slash and star and the below slash and star, and variable flag01 = true, then the code will stop running. but flag01 will be "undefined" by default.
    if (flag01 == true){
    clearInterval(collisiondetect);
    }
    */

    }

    //this command starts the collisionDetectionfunction
    collisiondetect = setInterval(function () {
        collisionDetection();
    }, 50);

  3. #3
    Member Pepember
    Join Date
    Jul 2001
    Location
    Berlin
    Posts
    886
    what's m2.testhit? (the "testhit" part)
    mc1_speed should "read mc1._speed", and that only assuming that it has a variable called "_speed".
    also, you need to know mc1's speed AND mc2's speed in order to determine the actual speed at which the mcs are clashing.
    nice try, brick, but maybe you can refine on that?
    Please sign here

  4. #4
    Senior Member
    Join Date
    Apr 2009
    Posts
    138
    Actionscript Code:
    // mc is the instance name of the movie clip, if there is no instance name then all the _variablename's wont work
    //example with out the mc instance named mc, mc._speed will not work. (First time I even know about all this! exciting)
    //adds an object from the library with the linkage of object1  and names the instance obj1. the depth is 100 and x and y is also 100.
    attachMovie("object1", "mc1", 100, {_x:100, _y:100});
    attachMovie("object2", "mc2", 101, {_x:300, _y:300});
    //use positive and negitive numbers to chose direction
    mc1._yspeed = 0;
    mc1._xspeed = 0;
    mc2._yspeed = 0;
    mc2._xspeed = 0;
    function collisionDetection() {
        //sets the collision force
        final_YCollisionSpeed = mc1._yspeed-mc2._yspeed;
        final_XCollisionSpeed = mc1._xspeed-mc2._xspeed;
        if (mc1.hitTest(mc2)) {
            //this code will check and see what one is over 10 and will display a trace to the output.
            //also it will use the collision direction of the more intense collision
            //lastly i added it so it can check a diagonal collision of the x and y collision are the same speed/force
            if (final_YCollisionSpeed>10 && final_YCollisionSpeed>final_XCollisionSpeed) {
                trace("Object one has collided with object two while traveling at "+mc1._yspeed+" on the y axis.");
            } else {
                if (final_XCollisionSpeed>10 && final_XCollisionSpeed>final_YCollisionSpeed) {
                    trace("Object one has collided with object two while traveling at "+mc1._xspeed+" on the x axis.");
                } else {
                    if (final_XCollisionSpeed == final_YCollisionSpeed) {
                        trace("Object one has collided with object two while traveling at "+mc1._xspeed+" on the x axis, and "+mc1._yspeed+" on the y axis");
                    }
                }
            }
        }
        /*
        If you remove the above slash and star and the below slash and star, and variable flag01 = true, then the code will stop running. but flag01 will be "undefined" by default.
        if (flag01 == true){
        clearInterval(collisiondetect);
        }
        */

    }
    //this command starts the collisionDetectionfunction
    collisiondetect = setInterval(function () {
        collisionDetection();
    }, 50);


    how is this?


    btw you will have to add the movement of the car your self by using
    mc1.onEnterFrame = function() {
    }

    and

    mc2.onEnterFrame = function() {
    }

    although you don't have to attachmovie, you can also have them on the stage if you like!

    now I assume there are bugs in here, so let me know

  5. #5
    Member Pepember
    Join Date
    Jul 2001
    Location
    Berlin
    Posts
    886
    that's way better, thanks!
    although calculating the collision speed is more complicated than your example, depending on the kind of movement of the two objects.
    don't have a nice example at hand, but maybe this will do for an inspiration:
    http://www.kirupa.com/developer/as3/...isions_pg2.htm
    Please sign here

  6. #6
    Senior Member
    Join Date
    Apr 2009
    Posts
    138
    yea, sorry about that! I didn't work very hard at it :P Just a simple thing I created fast.
    (perhaps you can help me with mine: http://board.flashkit.com/board/showthread.php?t=820044)

    I'll upload a version of this code that has a correct speed detection and force detection.
    prob tomorrow after my job interview

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