-1

Forgive me for my noobyness on this (I am new to Flash), but I have searched everywhere for 3 days to find this, and I have tried so many different things, and nothing seems to work. This should be simple...

scenario: simple game, the player fly's through space shooting bad guys. (my starting steps where this great tutorial: http://www.makeflashgames.com/tutorials/tut4.php )

I now would like to make my enemies explode when a collision is detected. I have made a new movieclip with my explosion animation, dropped that movieclip into a frame on my Enemy movieclip. This frame I labeled Explode.

On the GameController.as I went down to the code for removing the Enemy when a bullet collision is detected, and removed the removechild code, and replaced it with: Enemy.gotoAndPlay("Explode"); this gives me error 1061: Call to a possibly undefined method gotoAndPlay through a reference with static type Class.

I've tried _root.Enemy.gotoAndPlay("Explode"); it didn't work (I read _root doesn't work in as3)

What am I missing?

the code I wrote is in a few different area's. in the gamecontroller.as i have

        for (var i=bullets.length - 1; i >= 0; i--)
        {
            for (var j=enemies.length - 1; j >= 0; j--)
            {
                if (bullets[i].hitTestObject(enemies[j].collisionArea))
                {

                    playerScore +=  enemies[j].getPointsWorth();

                    mcGameStage.removeChild(bullets[i]);
                    bullets.splice(i,1);

                    Enemy.gotoAndPlay("Explode");

                    break;
                }
            }
        }

If I remove the gotoandplay, and just put in a

mcGameStage.removeChild(enemies[j]); enemies.splice(j,1);

then the game runs fine (without the explosion animation)

@Sunil D.: Your code works like a charm. It has, however , left me with a new problem. I need to remove my enemy when the Explode animation finishes (frame 35) I tried this code

enemy.addEventListener(Event.ENTER_FRAME,checkFrame);

function checkFrame(event:Event):void
    {
        if (enemy.currentFrame  ==  35)
            {
                  mcGameStage.removeChild(enemies[j]);
                  enemies.splice(j,1);
                  enemy.removeEventListener(Event.ENTER_FRAME,checkFrame);
            }
    }

This is giving me Error #2007: Parameter child must be non-null. at flash.display::DisplayObjectContainer/removeChild() at MethodInfo-243()

Any Ideas?

1 Answers1

0

The error you're getting is that when you do Enemy.gotoAndPlay("Explode"); you're trying to call the gotoAndPlay() method on the Enemy class itself. Instead, you need to call the gotoAndPlay() method on an instance of the Enemy class.

From the code you have shown, it looks like you have instances of the Enemy class in your enemies array (it's an array of Enemy objects). So you probably should be able to explode the enemy by doing this:

var enemy:Enemy = enemies[j] as Enemy;
enemy.gotoAndPlay("Explode");
Sunil D.
  • 17,983
  • 6
  • 53
  • 65