A Flash Developer Resource Site

Results 1 to 18 of 18

Thread: Help in my code

  1. #1
    Member
    Join Date
    Sep 2004
    Posts
    98

    Help in my code

    Hiya,
    I am trying to make points, and this is a code that I put on a mc named as bullet-
    code:

    onClipEvent (enterFrame) {
    if (this.hitTest( _root.mc1) ){
    _root.points = +1;
    } else {
    _root.points = _root.points;
    }
    }



    mc1 is the mc that when the bullet hit him, the points should be increased in 1.
    points is a dynamic text that the points should be displayed in there.

    -thanks.

  2. #2
    Senior Member lowestformofwit's Avatar
    Join Date
    Jun 2004
    Location
    Scotland
    Posts
    160
    To add 1 to your score the code should read:
    code:

    _root.points += 1;

    This is the equivalent of saying: _root.points = _root.points + 1.

    Hope that helps.

  3. #3
    Member
    Join Date
    Sep 2004
    Posts
    98
    it is not working...
    this is the code that I put in my bullet mc:
    code:

    onClipEvent (enterFrame) {
    if (this.hitTest( _root.mc1) ){
    _root.points += 1;
    } else {
    _root.points = _root.points;
    }
    }


  4. #4
    Senior Member lowestformofwit's Avatar
    Join Date
    Jun 2004
    Location
    Scotland
    Posts
    160
    Do you have a .fla that I could look at?

    Oh, and looking at the code again you don't need the else condition. _root.points will always equal _root.points.

  5. #5
    Senior Member lowestformofwit's Avatar
    Join Date
    Jun 2004
    Location
    Scotland
    Posts
    160
    Here's a little example I knocked up for you.

    Obviously the bullet will be fired from something and will be removed as soon as it hits, but I gives you the code you are looking for.
    Attached Files Attached Files

  6. #6
    Member
    Join Date
    Sep 2004
    Posts
    98
    still it is not working... I saw ur fla and from ur fla I put this code on the bullet-
    code:

    onClipEvent(load){
    points = 0;
    }

    onClipEvent (enterFrame) {
    if (this.hitTest( _root.mc1) ){
    _root.points += 1;
    }

    }


    so how can I fix it?

    -thanks.

    PS: how can I make the bullet and the mc1 disapear when they hit each other?
    Last edited by gootche; 09-19-2004 at 12:24 PM.

  7. #7
    Flash hates me. crashlanding's Avatar
    Join Date
    Nov 2003
    Location
    UK
    Posts
    439
    if mc1 and bullet were created dynamicly, i.e. with attachMovie of duplicateMovieClip, then you can use
    code:
    mc1.removeMovieClip();
    bullet.removeMovieClip();

    "wen i found my gerbil dead my other gerbil was eating it i just cried and screamed"
    http://www.livescripts.net

    --------------------------------------------------------------------------------
    Last edited by some moderator : Today at 9:01 PM.

  8. #8
    Member
    Join Date
    Sep 2004
    Posts
    98
    no they are not created dynamicly, they are on the stage.

  9. #9
    Senior Member lowestformofwit's Avatar
    Join Date
    Jun 2004
    Location
    Scotland
    Posts
    160
    It depends on how you are authoring your game.

    If you are creating the movieclips dynamically using "attachMovie" or "duplicateMovieClip" commands, then you would add the following to your 'if' statement:

    code:
    removeMovieClip(_root.mc1); // removes MC 'mc1'.
    this.removeMovieClip(); // removes the bullet



    If you added the movieclips to the stage at author time, then you use these instead:

    code:
    unloadMovie(_root.mc1); // removes MC 'mc1'.
    this.unloadMovie(); // removes the bullet



    Glad to help.

  10. #10
    Senior Member lowestformofwit's Avatar
    Join Date
    Jun 2004
    Location
    Scotland
    Posts
    160
    Have you checked your instance names Gootche?

  11. #11
    Member
    Join Date
    Sep 2004
    Posts
    98
    thanks guys but I still need to put a hitTest thing... and it is not working and I dont know why...

    I attached the fla, can u please make the points and the bullet and mc1 disapear?

    -thanks.
    Attached Files Attached Files

  12. #12
    Senior Member dogtown08's Avatar
    Join Date
    Jul 2004
    Location
    In a later dimension
    Posts
    201
    Back to your original problem:
    Your code is fine. there is something else wrong and it is hard to tell without a fla, but here are some things that could go wrong:

    a. make sure you are placing your code in the right movie clip (NOT mc1 in your case)

    b. Make sure the Instance name is "mc1" not only the name you as-sinned it when you clicked convert to symbol or new symbol.

    c. Make sure the dynamic text box (or wherever you are displaying the points) has "_root.points" in the "Var" field, NOT the instance name.

    d. If none of the above work, try adding this line under "_root.points+=1;" :
    code:
    trace(_root.points)


  13. #13
    Member
    Join Date
    Sep 2004
    Posts
    98
    I was silly... I didnt changed the var in the text...
    but, I changed the var and then in the text filed I have some tihng like that-
    levl10p
    WTF? LOL how is that got to there!?

    I attached the fla.
    Attached Files Attached Files

  14. #14
    Senior Member lowestformofwit's Avatar
    Join Date
    Jun 2004
    Location
    Scotland
    Posts
    160
    Had a quick look.

    The reason is that you're using duplicateMovieClip to generate fresh instances of your enemies (ie. mc1 clips).

    So when you test for a collision against "mc1" it doesnt exist because its a duplicate of "mc1".

    You need to use a for loop to loop through all instance name combinations.

    If you dont get any help I'll knock some code up for you later.

    Sorry, very busy at the moment my friend.

  15. #15
    Member
    Join Date
    Sep 2004
    Posts
    98
    thanks but what do u mean use a for loop to loop through all instance name combinations?

    -thanks

  16. #16
    the Dervish of Q??
    Join Date
    Mar 2004
    Location
    Abbotsford, BC
    Posts
    103
    Couldn't look at your code, seemed to be the wrong format, not MX, but I'll tell you some stuff about for loops, what he means. Say you attach bullets like this:
    code:

    on(keyPress("<Space>")){
    duplicateMovieClip("bullet","bullet_"+amtofBullets ,++amtofBullets)
    }


    and enemies like this:
    code:

    duplicateMovieClip("mc1","enemy_"+amtofEnemies, ++(amtofBullets+amtofEnemies));


    add them to an array say like
    code:

    myEnemies.push(root["enemy_"+amtofEnemies]);
    myBullets.push(root["bullet_"+amtofBullets]);


    then every frame do a check like:
    code:

    for(i=0;i<myEnemies.length){
    for(j=0;j<myBullets.length){
    if(myBullets[j].hitTest(myEnemies[i]){
    _root.points++;
    myBullets[j].removeMovieClip;
    myEnemies[i].removeMovieClip;
    }
    }
    }


    note that this code is very rough, and would probably have to be modified heavily to make it fit. Maybe upload the fla in MX format and I'll see how I could refine it.
    Hope this helps.
    Derek.

  17. #17
    Member
    Join Date
    Sep 2004
    Posts
    98
    thankx Derek, u sure explained what is the loop but still I cant make the points...
    Here, I attached the fla in MX format, plz see if u can help me.

    -Thanks
    Attached Files Attached Files

  18. #18
    Member
    Join Date
    Sep 2004
    Posts
    98
    still need help...

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