A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: Enemies attacking.

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    7

    Enemies attacking.

    My enemies are in an array.

    I want my enemies to make me lose health points when they are on the stage for a certain time.


    So if an enemy pops up on the stage and is on the stage for 10 seconds or something without being killed it will make me lose HP.

    Right now I'm thinking of a timer. But it isn't working that great.

    Please help me

  2. #2
    Senior Member
    Join Date
    Jul 2008
    Posts
    391
    Why isn't it working that great?

  3. #3
    Junior Member
    Join Date
    Dec 2011
    Posts
    7
    Well because each enemy needs its separate timer in theory right?

    You saying if one of the enemies are on the stage for a certain they attack. I'm trying to do if one of them is killed the timer will reset. But another enemy could appear and the timer is counting for that enemy.

    Do you get what I mean 5Five555?

  4. #4
    Registered User
    Join Date
    Jul 2011
    Posts
    4
    I'm a totally noob when it comes to FLASH and AS but I've met similar problem with my other coding and I think you can try this

    Only use one timer as the time keeper for the whole game.
    Create another array with the same number of nodes. Lets call this array time[] and your enemies array enemy[]
    So when enemy[i] is on stage time[i] = current value of timer
    When (current value of timer - time[i] > 10) then you should lose some HP

    Don't know if it works but maybe you can give it a try!
    Last edited by Darren Le; 12-15-2011 at 12:08 PM.

  5. #5
    Junior Member
    Join Date
    Dec 2011
    Posts
    7
    Quote Originally Posted by Darren Le View Post
    I'm a totally noob when it comes to FLASH and AS but I've met similar problem with my other coding and I think you can try this

    Only use one timer as the time keeper for the whole game.
    Create another array with the same number of nodes. Lets call this array time[] and your enemies array enemy[]
    So when enemy[i] is on stage time[i] = current value of timer
    When (current value of timer - time[i] > 10) then you should lose some HP

    Don't know if it works but maybe you can give it a try!
    I'll try it tomorrow in class!

    Thanks

  6. #6
    Senior Member
    Join Date
    Jul 2008
    Posts
    391
    No reason why it wouldn't work with a Timer. Whenever an enemy is created and added to the stage, you can create a Timer with a delay of 10000 which is 10 seconds. If the enemy is killed before the 10 seconds is up, you stop the timer. If the Timer fires after 10 seconds, that means it's still alive and you do what you have to do.

  7. #7
    Member
    Join Date
    Sep 2010
    Posts
    37
    It sounds a lot like you're using ONE timer for all the enemies.
    I would create separate timers. So, everytime you add an enemy to the stage, call something like: enemyTimer();

    Code:
    function enemyTimer():void{
    var eTimer:Timer = new Timer(10000); // 10 seconds
    eTimer.addEventListener(TimerEvent.TIMER, doDamage);
    eTimer.start;
    }
    
    doDamage(t:TimerEvent):void{
    //do damage here
    }
    Also you would have to tie the timer to the enemy. You could probably do this several ways, the easiest is probably to have this timer functionality within the enemy class (that is if you're going with an Object-Oriented approach), and just dispatch an event within the doDamage function and have the parent class listen to that. Else you could feed the enemy into the "enemyTimer( enemy:enemyToAssignTimerTo ):void" ...and find a way to add the timer to the enemy. Couldn't think of the right way off the top of my head. Sorry about that. Also, make sure to remove the timer when the enemy dies.

  8. #8
    Junior Member
    Join Date
    Dec 2011
    Posts
    7
    Its working thank you! All I have to do now is make it display externally! Here is my code if anyone else has this problem. Just the event listeners :P

    Actionscript Code:
    }
            public function targets(e:MouseEvent):void {
                iTarget=-1;
                for (i = 0; i<arTargets.length; i++) {
                    if (arTargets[i].hitTestPoint(mouseX,mouseY,true)) {
                        iTarget=i;
                    }
                }
                if (iTarget!=-1) {
                    nEnemyH-=1;
                }
                if (nEnemyH==0) {
                    removeChild(arTargets[iTarget]);
                    nEnemyH=5;
                    nH=int(txtHealth.text);
                    tmr2.reset();
                }
            }
            function addTargets(e:TimerEvent) {
                dX=Math.random()*875;
                dY=Math.random()*400;
                t=new Enemy2();
                t.x=dX;
                t.y=dY;
                addChild(t);
                arTargets.push(t);
                if (t.visible==true) {
                    tmr2.start();
                }
                if (tmr1.currentCount==30) {
                    tmr1.stop();
                }
            }
            function loseHealth(e:TimerEvent) {
                nH-=5;
                trace(nH);
            }
            public function cursor(evt:Event):void {
                crosshair.x=mouseX-15;
                crosshair.y=mouseY-15;
                addChild(crosshair);
                //trace(tmr2.currentCount);
            }
            public function Follow(event:Event):void {
                var dSwordx:Number,dSwordy:Number,dRad:Number;
                dSwordx=mouseX-sword.x;
                dSwordy=mouseY-sword.y;
                if (dSwordy+sword.y>=229) {
                    dSwordy=229-sword.y;
                }
                dRad=Math.atan2(dSwordy,dSwordx);
                addChild(sword);
                sword.rotation=dRad*180/Math.PI;
               

            }
        }
    }

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