Flash: hitTest on multiple targets

  • Velo
  • Born
  • Born
  • Velo
  • Posts: 2
  • Loc: beside Heaven

Post 3+ Months Ago

Hello everybody. This is my game , in which the aircraft replaces the mouse, drops off the bomb when you click, once the bomb touch the tank , tank explodes.

Now the game is uncompleted. I `ve got troubles in the part of Action Script. The bomb falls successfully. But only one tank can be bombed and explodes, whose instance name is "tank" . When the bomb touchs other tanks ( "tank2" "tank3"....... ), these tanks don`t work . How to make a Action Script that can bomb every tank ?


Please show me the correct action script . Thanks very much ! ! !

The file below is the game . My action script has been written down which is not correct. ( I `m a tiro. So, Could you modify my file below? It`s urgent for me. In fact , it`s a birthday gift for my friend . So , I`ll show my great appreciation of your help )

:?:
http://www.were-over-there.com/board/index.php?s=7ea292bf4b270d04a2b6803c79046e7b&act=Attach&type=post&id=17955
  • digitalMedia
  • a.k.a. dM
  • Genius
  • User avatar
  • Posts: 5148
  • Loc: SC-USA

Post 3+ Months Ago

I didn't download your .fla, but, from your description, I would think you would want to track a variable for the number of tanks you create on the screen, then run your hitTest in a for/next loop.

Code: [ Select ]
for(i=1;i<=numberOfTanks;i++){
  if(bomb.hitTest("tank"+i)===true){
   eval("tank"+i).gotoAndPlay("boom");
  }
}
  1. for(i=1;i<=numberOfTanks;i++){
  2.   if(bomb.hitTest("tank"+i)===true){
  3.    eval("tank"+i).gotoAndPlay("boom");
  4.   }
  5. }
  • Velo
  • Born
  • Born
  • Velo
  • Posts: 2
  • Loc: beside Heaven

Post 3+ Months Ago

Thank you very much for your help. I should have made the question clear . I`ll show you my work below:

There are 5 tank in total. Their instance name is "tank" "tank2" "tank3" "tank4" "tank5" . Beside, I made 5 movie clips which are the exploding animation. (Instance name --"tank_explode" ,"tank2_explode",.... "tank5_explode") They are different for each other . when the bomb ("bomb") form the aircraft("aircraft") touch tank, the "tank_expolde" will replace the "tank", so is to others.

this is the AS which I put in the 1st frame of movie clip "bomb"

Code: [ Select ]
this.onEnterFrame = function() {
    // trace(this.hitTest(_root.
    if (this.hitTest(_root.tank)) {
        flag = 1;
        tellTarget (_root.tank) {
            gotoAndPlay(2);
        }
        removeMovieClip(this);
    }
};
  1. this.onEnterFrame = function() {
  2.     // trace(this.hitTest(_root.
  3.     if (this.hitTest(_root.tank)) {
  4.         flag = 1;
  5.         tellTarget (_root.tank) {
  6.             gotoAndPlay(2);
  7.         }
  8.         removeMovieClip(this);
  9.     }
  10. };



AS in the last frame

Code: [ Select ]
removeMovieClip(this);




This is the AS I put in the Frame Action of MC"aircaft"

Code: [ Select ]
Mouse.hide();
bomb._visible = false;
i = 0;
startDrag("aircraft", true);
// function checker(){
// for(j=1;j<=i;j++){
// //trace(_root["bomb"+i].hitTest(_root.tank));
// if (_root["bomb"+i].flag == 1){
// tank.gotoAndStop(2);
// }
// }
// }
//


_root.onMouseDown = function() {
    duplicateMovieClip(bomb, "bomb"+i, i);
    _root["bomb"+i]._x = _xmouse;
    _root["bomb"+i]._y = _ymouse;
    // _root["bomb"+i].gotoAndPlay("play");
    // tellTarget(eval("bomb"+i)){
    // gotoAndPlay(11);
    // }
    ++i;
};
// setInterval(checker,100);
  1. Mouse.hide();
  2. bomb._visible = false;
  3. i = 0;
  4. startDrag("aircraft", true);
  5. // function checker(){
  6. // for(j=1;j<=i;j++){
  7. // //trace(_root["bomb"+i].hitTest(_root.tank));
  8. // if (_root["bomb"+i].flag == 1){
  9. // tank.gotoAndStop(2);
  10. // }
  11. // }
  12. // }
  13. //
  14. _root.onMouseDown = function() {
  15.     duplicateMovieClip(bomb, "bomb"+i, i);
  16.     _root["bomb"+i]._x = _xmouse;
  17.     _root["bomb"+i]._y = _ymouse;
  18.     // _root["bomb"+i].gotoAndPlay("play");
  19.     // tellTarget(eval("bomb"+i)){
  20.     // gotoAndPlay(11);
  21.     // }
  22.     ++i;
  23. };
  24. // setInterval(checker,100);




Please show me the correct Action Script and step . Thanks again !!

http://www/docs/xxkj/lrgs/uploadtp/Bomb_MX6.fla
  • IceCold
  • Guru
  • Guru
  • User avatar
  • Posts: 1254
  • Loc: Ro

Post 3+ Months Ago

use the code that digitalMedia provided, with a bit of modification. (remember, _root["tankV"+iTankIndex] represent a visible tank, not a type of tank.
so this loops through all the visible tanks that you have on the map.
so, for you bomb mc,
Code: [ Select ]
this.onEnterFrame = function() {
 for(iTankIndex =1;i<=numberOfTanks ;iTankIndex++){
  if(this.hitTest(_root["tankV"+iTankIndex])){
      flag = 1;
     _root["tankV"+iTankIndex].gotoAndPlay(2);
   }
   removeMovieClip(this);
  }
};
  1. this.onEnterFrame = function() {
  2.  for(iTankIndex =1;i<=numberOfTanks ;iTankIndex++){
  3.   if(this.hitTest(_root["tankV"+iTankIndex])){
  4.       flag = 1;
  5.      _root["tankV"+iTankIndex].gotoAndPlay(2);
  6.    }
  7.    removeMovieClip(this);
  8.   }
  9. };

also i suppose that in frame 2 of each tank movieclip you have the code for destroying the tank and removing the mc.

btw, one thing that i will do.
create a movie clip called tankProto, in which i will store the tankID, tankImage, tankBehaviour ... and whatever i need.
then, in the first frame of the tank, load the tankImage related to the tankID.
This way, you will have only one tank prototype and the necessary actionscript.
  • pzurich14
  • Born
  • Born
  • pzurich14
  • Posts: 1

Post 3+ Months Ago

I am currently stuck in a project wherein I have created 2 shapes using action script and the main goal was if shape1 collides with shape2-shape2's height & width are displayed on a label.. Is it possible to retrieve shape2 variables when shape1 collides with it?please help i really don't know what to do next.....

Post Information

  • Total Posts in this topic: 5 posts
  • Users browsing this forum: No registered users and 4 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.