Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8412597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:42:44+00:00 2026-06-10T00:42:44+00:00

im attempting to make a simple game where zombies spawn out randomly from the

  • 0

im attempting to make a simple game where zombies spawn out randomly from the edges of the screen and once they have spawned, they will get the last position of the player and keep moving towards that direction until they hit the stage and gets removed. However, i seem to be having a problem with the chase code. Even after reading tons of articles that offer the same chunk of code, im still unable to make it work (IM DOING IT WRONG ARRGH).

This is the main class

package 
{
import flash.display.*;
import flash.events.*;
import flash.utils.*;
import flash.ui.*;

public class main extends MovieClip
{
    private var left,right,up,down,fire,mouseRight,mouseLeft:Boolean;
    private var speedX,speedY,mobPosX,mobPosY:int;
    private var num:Number;
    private var wizard:Player;
    private var crosshair:Crosshair;
    private var mobArray,magicArray:Array;

    public function main()
    {
        //Hide mouse for crosshair
        Mouse.hide();
        crosshair = new Crosshair();
        addChild(crosshair);
        crosshair.x = stage.stageWidth;
        crosshair.y = stage.stageHeight;

        //Set initial speed
        speedX = 0;
        speedY = 0;

        //Wizard stuff
        fire = false;
        wizard = new Player();
        addChild(wizard);
        wizard.x = stage.stageWidth / 2;
        wizard.y = stage.stageHeight / 2;

        //Mob stuff
        mobArray = new Array();
        magicArray = new Array();


        //Initialize bool so movement doesn't get stuck on startup
        up = false;
        down = false;
        left = false;
        right = false;

    }

    public function startGame()
    {



        addEventListener(Event.ENTER_FRAME,update);
        stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownHandler);
        stage.addEventListener(KeyboardEvent.KEY_UP,keyUpHandler);
        stage.addEventListener(MouseEvent.CLICK, myClick);


    }

    private function keyDownHandler(evt:KeyboardEvent)
    {

        if (evt.keyCode == 37)
        {
            left = true;
        }

        if (evt.keyCode == 38)
        {
            up = true;

        }

        if (evt.keyCode == 39)
        {
            right = true;
        }

        if (evt.keyCode == 40)
        {
            down = true;
        }

        if (evt.keyCode == 67)
        {
            trace(mobArray.length);
        }

    }

    private function keyUpHandler(evt:KeyboardEvent)
    {
        if (evt.keyCode == 37)
        {
            left = false;
            speedX = 0;
        }

        if (evt.keyCode == 38)
        {
            up = false;
            speedY = 0;
        }

        if (evt.keyCode == 39)
        {
            right = false;
            speedX = 0;
        }

        if (evt.keyCode == 40)
        {
            down = false;
            speedY = 0;
        }

    }

    function myClick(eventObject:MouseEvent)
    {
        fire = true;
    }


    public function update(evt:Event)
    {
        //UI
        crosshair.x = mouseX;
        crosshair.y = mouseY;

        //Start player
        if (left && right == false)
        {
            speedX = -8;
        }

        if (up && down == false)
        {
            speedY = -8;
        }

        if (down && up == false)
        {
            speedY = 8;
        }

        if (right && left == false)
        {
            speedX = 8;
        }

        wizard.x +=  speedX;
        wizard.y +=  speedY;

        for (var i = mobArray.length - 1; i >= 0; i--)
        {
            //Start mob                                 //if X is zero and Y > 10, spawn.

            var m:Zombie = new Zombie();
            m.chase((Math.atan2(mobArray[i].y - wizard.y,mobArray[i].x - wizard.x)/Math.PI * 180)); //applies trigo
            num = Math.random();
            if (num < 0.1)
            {
                //when x = 0
                mobPosX = 10;
                mobPosY = Math.floor(Math.random() * 590) + 10;
            }
            else if (num < 0.3)
            {
                //when y = 0
                mobPosX = Math.floor(Math.random() * 790) + 10;
                mobPosY = 10;
            }
            else if (num < 0.6)
            {
                mobPosX = 800;
                mobPosY = Math.floor(Math.random() * 590) + 10;
                //when x width of screen
            }
            else if (num < 0.9)
            {
                //y is height of screen
                mobPosX = Math.floor(Math.random() * 790) + 10;
                mobPosY = 590;
            }
            m.x = mobPosX;
            m.y = mobPosY;
            mobArray.push(m);
            addChild(m);
        }




    }

    private function gameOver()
    {
        removeEventListener(Event.ENTER_FRAME,update);
        stage.removeEventListener(KeyboardEvent.KEY_DOWN,keyDownHandler);
        stage.removeEventListener(KeyboardEvent.KEY_UP,keyUpHandler);
    }



}//end class
 }//end package

This is the zombie class

package 
  {
import flash.display.*;
import flash.events.*;

public class Zombie extends MovieClip
{
    private var zombSpd:Number;
    private var angle:Number;

    public function Zombie()
    {
        zombSpd = 10;
    }

    public function chase(chaseAngle:Number)
    {
        angle = chaseAngle;
    }


    public function update()
    {
        this.x+=Math.cos(angle*Math.PI/180)*zombSpd;
        this.y+=Math.sin(angle*Math.PI/180)*zombSpd;
    }


}//end class
   }//end package

Thank you 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T00:42:45+00:00Added an answer on June 10, 2026 at 12:42 am

    I just reread your code and see that you must read and learn more about programming before trying to create a game like this.

    the reason your zombies are not spawning are because you never get inside the for loop. mob array length will be zero to start with, so the for loop will never happen.

    take that for loop out and create a function to spawn enemies in your main class. you can use the code you use in your for loop as the code for this function example:

    function spawn():void{
        //code directly from your for loop, with a small change            
            num = Math.random();
            if (num < 0.1)
            {
                //when x = 0
                mobPosX = 10;
                mobPosY = Math.floor(Math.random() * 590) + 10;
            }
            else if (num < 0.3)
            {
                //when y = 0
                mobPosX = Math.floor(Math.random() * 790) + 10;
                mobPosY = 10;
            }
            else if (num < 0.6)
            {
                mobPosX = 800;
                mobPosY = Math.floor(Math.random() * 590) + 10;
                //when x width of screen
            }
            else if (num < 0.9)
            {
                //y is height of screen
                mobPosX = Math.floor(Math.random() * 790) + 10;
                mobPosY = 590;
            }
            var m:Zombie = new Zombie();
            m.x = mobPosX;
            m.y = mobPosY;
            m.chase((Math.atan2(m.y - wizard.y,m.x - wizard.x)/Math.PI * 180)); //applies trigo
            mobArray.push(m);
            addChild(m);
    }
    

    now in your update function you must determine when you want enemies to spawn. you can do this by using a counter. Ill let you figure that part out, but if you want zombies to spawn continuously, (where your forloop in the update function was) add this:

    //use a counter variable to determine when to execute spawn
    spawn();
    //loop through all zombies
    for(var i:int = 0; i < mobArray.length; i++){
        mobArray[i].update();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm attempting to make a simple homebrew game engine/framework in android. I have the
I'm attempting to make a simple linear text game that will display inside a
I was attempting to make a simple pokemon text game in c++. I created
I'm attempting to make a simple timer from 15 minutes to 0 seconds. I'm
I am attempting to make a simple game using C++ and OpenGL and I
I'm attempting to make a simple game of Pairs for Android. Program Structure Menu.java
I'm attempting to make a simple game of Pairs for Android. Program Structure: Menu.java
I am attempting to make a simple accordion where the user clicks on level
I am attempting to make a game using java, and I need a plugin
I am attempting to make a simple class that serializes itself to disk when

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.