7 Replies Latest reply on Aug 6, 2009 6:36 PM by kglad

    Not repeating attacking

    dalcde Level 1

      I have made a game which you need to shoot fireballs to enemies. Once it hits the ground and explodes, the explosion reduces the enemies it hit. I want it to just reduce the enemies health once per enemy, but how do you write the code? There is a function in the enemy called reduceHealth(reduce). "reduce" is how much it reduces. If it is 10, the health is reduced by 10. The enemies are put in an array. Just call it yourArray?

      Please help!

        • 1. Re: Not repeating attacking
          kglad Adobe Community Professional & MVP

          why is there a problem and what is the problem?

          • 2. Re: Not repeating attacking
            dalcde Level 1

            I've written a code that worked, but it is totally bugged. I want to know if there is another way to do it. Or another concept. This is my concept: Check if it hit the enemy. If it hit the enemy, it adds array number of the enemy into another array. On the next frame, if it hits another enemy, it will check if it is in the array. If it isn't it attacks. If it is, it doesn't attack.

            • 3. Re: Not repeating attacking
              kglad Adobe Community Professional & MVP

              that won't work, as you know.

               

              each time an enemy is hit by a fireball check if that fireball is in the enemies fireball array property.  if not, subtract health and push the fireball into the enemies fireball array.   push the hit enemy into a fireball array property.

               

              when a fireball is no longer needed check which enemies have that fireball in their fireball array (by checking the fireball's enemy array) and splice the fireball from the enemy arrays and finally remove the fireball.

              • 4. Re: Not repeating attacking
                dalcde Level 1

                I may not be telling clearly, but it is the fireball checking whether it hit the enemy.

                • 5. Re: Not repeating attacking
                  kglad Adobe Community Professional & MVP

                  it doesn't matter.

                  • 6. Re: Not repeating attacking
                    dalcde Level 1

                    I don't understand much about what you are saying, but can you write the code here, or tell me more specificly. And there is one problem, which is when two fireballs are shot together, the fireballs shot later will get the array from the older fireballs. It seems that I have to post the code up here.

                    P.S. It has an animation of exploding, and frame 1-30 is showing the explosion, and after 30, it is the smoke, which doesn't attack.

                     

                     

                     

                    class attack1Explosion extends MovieClip {
                        var attack;
                        var enemiesKilled;
                        var enemyKill;
                        function kill() {
                            for (var i in _root.ship.enemies) {
                                if (this.enemiesKilled.length == 0) {
                                    if (this.hitTest(_root.ship.enemies[i])) {
                                        _root.ship.enemies[i].health -= attack;
                                        this.enemiesKilled.push("_root.ship.enemies["+i+"]");
                                    }
                                } else {
                                    for (var j in enemiesKilled) {
                                        if (enemiesKilled[j] != "_root.ship.enemies["+i+"]") {
                                            enemyKill++;
                                        }
                                    }
                                    if (this.hitTest(_root.ship.enemies[i]) && enemyKill == enemiesKilled.length) {
                                        _root.ship.enemies[i].health -= attack;
                                        this.enemiesKilled.push("_root.ship.enemies["+i+"]");
                                    }
                                }
                                enemyKill = 0;
                            }
                        }
                        function onLoad() {
                            attack = _root.storage.attack2damage;
                            enemiesKilled = [];
                        }
                        function onEnterFrame() {
                            if (this._currentframe<30) {
                                kill();
                            }
                            if (this._currentframe == this._totalframes) {
                                this.removeMovieClip();
                            }
                        }
                    }

                    • 7. Re: Not repeating attacking
                      kglad Adobe Community Professional & MVP

                       

                       

                       

                      class attack1Explosion extends MovieClip {
                          var attack;
                          var enemiesKilled;
                          var enemyKill;
                          function kill() {
                              for (var i in _root.ship.enemies) {
                                  if (this.enemiesKilled.length == 0) {
                                      if (this.hitTest(_root.ship.enemies[i])) {

                      if(!memberOf(_root.ship.enemies[i].fireballA,this)){  // create a memberOf function that returns the obvious boolean
                      _root.ship.enemies[i].fireballA.push(this);
                      this.enemyA.push(_root.ship.enemies[i])


                                          _root.ship.enemies[i].health -= attack;
                                        

                                      }

                      }

                       

                      // i don't know what this other stuff is doing.

                       

                                  } else {
                                      for (var j in enemiesKilled) {
                                          if (enemiesKilled[j] != "_root.ship.enemies["+i+"]") {
                                              enemyKill++;
                                          }
                                      }
                                      if (this.hitTest(_root.ship.enemies[i]) && enemyKill == enemiesKilled.length) {
                                          _root.ship.enemies[i].health -= attack;
                                          this.enemiesKilled.push("_root.ship.enemies["+i+"]");
                                      }
                                  }
                                  enemyKill = 0;
                              }
                          }
                          function onLoad() {
                              attack = _root.storage.attack2damage;
                              enemiesKilled = [];
                          }
                          function onEnterFrame() {
                              if (this._currentframe<30) {
                                  kill();
                              }
                              if (this._currentframe == this._totalframes) {
                                  this.removeMovieClip();
                              }
                          }
                      }

                      1 person found this helpful