A Flash Developer Resource Site

Results 1 to 14 of 14

Thread: collision script

  1. #1
    Junior Member
    Join Date
    Jul 2003
    Posts
    6

    collision script

    hiya,

    i was wondering, how do u make objects detect collision in a script, and then add 1 to a box named counter? im terrible at java but im ok at html, so i know what abouts the script is doing, but not sure how to do it (if that makes any sense)

    if anybody would be kind enough to write a script along theese lines and point out which each bit is doing, so i can get a slight understanding......

    thanks for your time, steve

  2. #2
    Senior Member pellepiano's Avatar
    Join Date
    Feb 2000
    Location
    Stockholm, Sweden
    Posts
    15,151
    If you have two movieclips ( clip1 and clip 2) you can have this code on one of them,

    onClipEvent (enterFrame) {
    // Check collision
    if (this.hitTest(_root.clip2)) {
    // Add to counter
    _root.counter++;
    }
    }

    -Pelle Piano
    // Image Gallery
    www.studiobild.com
    // Photo Blog
    http://talesofthepixel.blogspot.com

  3. #3
    Junior Member
    Join Date
    Jul 2003
    Posts
    6
    movieclips....(eh?)

    i have two elemnts i wud like to use, one which follows the mouse on the mouseY() and one elemnt that moves past it, (u av to catch em)

  4. #4
    A Senior Newbie - How Odd ForumNewbie's Avatar
    Join Date
    Mar 2003
    Posts
    590
    Not sure where the movieclips example was going.

    I would just add a script to the movie, running every 1 frame, doing the following:

    if (element1.collideRect(element2))
    {
    hitcounter+=1
    }

    I attach an example.

    M.
    Attached Files Attached Files
    Please note that my domain has changed to http://www.morganmultinational.com

  5. #5
    Senior Member pellepiano's Avatar
    Join Date
    Feb 2000
    Location
    Stockholm, Sweden
    Posts
    15,151
    I didnt know theres another way to check colission exept hitTest or checking x and y positions. Unfortunately your file would not open on my computer.
    Could you explain please. What is collideRect?

    -Pelle Piano
    // Image Gallery
    www.studiobild.com
    // Photo Blog
    http://talesofthepixel.blogspot.com

  6. #6
    A Senior Newbie - How Odd ForumNewbie's Avatar
    Join Date
    Mar 2003
    Posts
    590
    Hi Pellepiano,

    My example should run on 3D Flash Animator, version 4, release 4.
    See http://www.3dfa.com for a free downloadable demo version.

    3dfa has lots of nice, easy to use functionality for games, 3D stuff, and other Flash development. 'collideRect' is a command that tests if the bounding box of 1 element is in collision with that of another.

    ie. 'element1.collideRect(element2)' would return true if the two elements were in collision.

    Hope this helps to clarify,

    Morgan.
    Please note that my domain has changed to http://www.morganmultinational.com

  7. #7
    Senior Member pellepiano's Avatar
    Join Date
    Feb 2000
    Location
    Stockholm, Sweden
    Posts
    15,151
    Thanks!

    I noticed I am in the wrong forum ( Im a Flash guy ). We do the same but with hitTest.
    Sorry for the confusion.

    Seems like a good and cheap alternative to Flash. Ive seen the demos and visited the site.

    -Pelle Piano
    // Image Gallery
    www.studiobild.com
    // Photo Blog
    http://talesofthepixel.blogspot.com

  8. #8
    Junior Member
    Join Date
    Jul 2003
    Posts
    6
    hiya, thanks for your help. I did the collide.rect method of things, and changed the variables in the script (element1) to the names of my elements, but when i played the movie, i got this error message: collideRect() is not a method of Null on line 1 if (dark.collideRect(ball)) 1 error found. can anybody help? If poosible, could sombody dl the example i have, and "fiddle" with it to see where it went wrong. thanks


    ps: theres also a readme to explain what im wanting

    thanx
    Attached Files Attached Files

  9. #9
    KoolMoves Moderator blanius's Avatar
    Join Date
    Jul 2001
    Location
    Atlanta GA
    Posts
    5,244
    Originally posted by dontdothat
    hiya, thanks for your help. I did the collide.rect method of things, and changed the variables in the script (element1) to the names of my elements, but when i played the movie, i got this error message: collideRect() is not a method of Null on line 1 if (dark.collideRect(ball)) 1 error found. can anybody help? If poosible, could sombody dl the example i have, and "fiddle" with it to see where it went wrong. thanks


    ps: theres also a readme to explain what im wanting

    thanx
    I didn't look at your code but you first have to get a 'handle' for your element or access it with the element function
    assuming an element called dark and one called light (make sure that the "has properties for each is set"

    Direct:
    element ("dark").collideRect (element ("light"))

    or in you start script assign handles to the elements

    dark=element ("dark")
    light.element ("light")

    then in you action you can now use the 'handles'

    dark.collideRect (light)


    Oh and if you want to get really fancy you can detect collision on circular objects by calculating their distance and comparing it to their diameter using this function:
    PHP Code:
    //*************************************
    //Circle Collide
    //*************************************
        
    function C_collide(element1,element2)
        {
        
    //get width of both elements
        
    width1=(element1.getBounds().xMax-element1.getBounds().xMin)-6
        width2
    =(element2.getBounds().xMax-element2.getBounds().xMin)-6
        
    //get distance 
        
    a=element1.element2.x;
        
    b=element1.element2.y;
        
    distsqrt (a*a+b*b);
        
    //account for different sizes
        
    width=(width1/2)+(width2/2);
        
    //check distance and size
        
        //return t/f
        
    return dist<=width
        

    Last edited by blanius; 07-11-2003 at 08:12 AM.

  10. #10
    A Senior Newbie - How Odd ForumNewbie's Avatar
    Join Date
    Mar 2003
    Posts
    590
    Hi,

    I attach a really simple example of how to start to add collisions to the movie:

    You'll need to put something in to avoid getting multiple collisions on each drop, but this should give the general idea. I didn't put any fancy stuff in, coz that might be a bit confusing to start with.

    M.

    Incidentally - When you post movies on here, make sure to include any GIF's etc that are linked.
    Attached Files Attached Files
    Last edited by ForumNewbie; 07-19-2003 at 05:26 AM.
    Please note that my domain has changed to http://www.morganmultinational.com

  11. #11
    Junior Member
    Join Date
    Jul 2003
    Posts
    6
    hiya

    thanx for that example forumNewbie

    is there anyway to add actions on collisions, like wen it hits it plays a sound?
    and also can u write if else commands
    so if it dusnt collide a point is deducted and a diff sound is played


    thanks

  12. #12
    Junior Member
    Join Date
    Jul 2003
    Posts
    6
    hiya again

    forumnewbies example was spot on, so i decieded to use that, so i copied it exactly but in place of my own images, but id get it adding 6 to the counter (the time the object would take to pass over the counter) starngly enough this never happend on the orignial.... (?????)

    anyways i set to work to solve this, and came up with this, yet again my attempt hit the ground like a cow from mars...

    can any1 help on this??

    var counter

    if(player.collideRect(moon))
    {
    setTimeout('counter+=2',5000);
    }

    i wrote this so that when the object collides it wait for 5 seconds before adding the score, i thought this would solve it, but im getting this error:

    collideRect() is not a method of Null on line 11 if(player.collideRect(moon))
    1 errors found

    thanx yet again

  13. #13
    KoolMoves Moderator blanius's Avatar
    Join Date
    Jul 2001
    Location
    Atlanta GA
    Posts
    5,244
    that won't work.

    You can do a number of things:

    [list=1][*]Slow down how often you check for collision[*]Upon collision set a flag/variable to true and when no longer colliding set to false and only add once while true[*]When collision happens remove/move on of the objects so that they are no longer colliding[/list=1]

    Each option has value and you have to decide in the context of your program which would make the most sense.

    #2 would be my usual way.
    PHP Code:
    if (player.collideRect(moon)){//Did we collide
            
    if (!player.hitcheck){//Make sure we weren't already colliding
                   
    player.hitcheck=true;//if not the set true we are
                   
    _root.counter++;//update counter
                   
    }         
          }else{
             
    player.hitcheck=false
           
            

    something like that or
    in start script set
    player.hitcheck=false;
    then

    PHP Code:
    if (player.collideRect(moon) && player.hitcheck!=true){
    //if colliding and not already colliding
            
    root.counter++;
            
    player.hitcheck=true;//set already colliding
    }
    if (!
    player.collideRect(moon) && player.hitcheck==true){
    //if NOT colliding and hitcheck is true set to false.
          
    playerhitcheck=false;


    These are off the cuff and untested but should at least give you the idea.

  14. #14
    A Senior Newbie - How Odd ForumNewbie's Avatar
    Join Date
    Mar 2003
    Posts
    590
    I put idea #2 into the example, and added a sound for when the right hand element hits the player.

    Good luck with the rest of it.

    M.
    Attached Files Attached Files
    Please note that my domain has changed to http://www.morganmultinational.com

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