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 9037535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T09:16:06+00:00 2026-06-16T09:16:06+00:00

Below is a class to generate citizens in a street. They walk both left

  • 0

Below is a class to generate citizens in a street. They walk both left and right and are assigned what side they will be facing, what speed they move and their y position. I previously used the function setUpCitizens() inside the constructor but I wanted to use ADDED_TO_STAGE and REMOVED_FROM_STAGE because I am having trouble removing the level and listeners.

package 
{
import flash.display.*;
import flash.events.*;
import flash.utils.getTimer;
import flash.utils.Timer;

public class Citizen extends MovieClip
{
    private var dx:Number;// speed and direction
    private var lastTime:int;// animation time

    public function Citizen(side:String, _speed:Number, yPos:Number)
    {   
        var side = side;
        var speed = _speed;
        var yPos = yPos;

        addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
    }

    private function onAddedToStage(event:Event):void
    {
        setUpCitizens();
        addEventListener(Event.REMOVED_FROM_STAGE,    onRemovedFromStage);
    }

    private function onRemovedFromStage(event:Event):void
    {
        removeAllCitizens();
        this.removeEventListener(Event.ENTER_FRAME,moveCitizen);
    }

    private function setUpCitizens():void
    {
        if (side == "left")
        {
            this.x = -40;// start to the left
            dx = _speed;// walk left to right
            this.scaleX = 1;// reverse
        }
        else if (side == "right")
        {
            this.x = 5720;// start to the right
            dx =  -  _speed;// walk right to left
            this.scaleX = -1;
        }// not reverse

        this.y = yPos;// vertical position
        this.gotoAndStop(Math.floor(Math.random()*9+1));
        // set up animation;
        this.addEventListener(Event.ENTER_FRAME,moveCitizen);
        lastTime = getTimer();
    }

    private function removeAllCitizens():void
    {
        removeEventListener(Event.ENTER_FRAME,moveCitizen);
        parent.removeChild(this);
    }

I am getting this error:
errors

I call the class like this:

var c:Citizen = new Citizen(side,speed,yPos);

This is called from the parent class object Level1. I am also having trouble trying to reference all the generated children and remove them all to hopefully speed up the game…

Thanks for any suggestions?

  • 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-16T09:16:08+00:00Added an answer on June 16, 2026 at 9:16 am

    When you create variables inside a method, they only exist inside that method. Your Citizen constructor creates var speed, var size, and var yPos, but as soon as that method finishes, those variables are lost. You need to store them in class properties (like your dx and your lastTime). Add something like citizenSpeed:Number to the class properties, and then in your Citizen constructor, assign the passed parameter to that variable, i.e.

    public class Citizen extends MovieClip
    {
        private var dx:Number;// speed and direction
        private var lastTime:int;// animation time
        //added lines
        private var citizenSpeed:Number;
        private var citizenSide:String;
        private var citizenYPos:Number;
    
        public function Citizen(_side:String, _speed:Number, _yPos:Number)
        {   
            citizenSide = _side;
            citizenSpeed = _speed;
            citizenYPos = _yPos;
    
            addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
        }
    
    ...
    

    And then, in setUpCitizens(), just reference those member variables (citizenSpeed, etc.).

    The errors you’re getting are because setUpCitizens() contains code that is referencing variables it doesn’t know anything about, because they’re out of scope at that point.

    Hope that helps!

    B.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to generate an xml file using the below class, when an
I used http://json2csharp.com to generate the C# class from the below JSON string. {
Basically, the code below will generate divs which are then captured using the jquery
Does making the below class final (adding public final class) have any real impact
I have a class like the one below: class Foo { private: std::map<std::string, Bar*>
When I try to execute the below class using ICriteria, if (_userGroupId > 0
i need Enumarable value from below class but give error public static Enumerable LoadDataByName(string
I have a model defined as below: class Example(models.Model): user = models.ForeignKey(User, null=True) other
I am writing an AsyncTask as below: class Load extends AsyncTask<String, String, String> {
I've a created a class called Animal as shown below: class Animal{ int eyes,legs;

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.