FORUMS: list search recent posts

(AS2) Accesing another object from a class

COW Forums : Adobe Flash

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Paulo Jan
(AS2) Accesing another object from a class
on Jan 8, 2009 at 10:06:51 pm

Hi all:

I'm writing some (Actionscript 2) code where I need to generate several instances of a graphic on the stage, up to a maximum number. I've done is this way:

1) One "Counter" class, an object that carries the count of how many graphics have been created, among other things:

class Counter {
public static var num:Number;

public function Counter() {

(Some irrelevant code skipped for brevity. "num" would be the variable that carries the count).

2) A "MyGraphic" class, which generates a new graphic if there aren't enough of them:

class MyGraphic extends MovieClip {

public function start() {
var OK=false;
while (OK == false) {
var eleccion=this.randRange(1, 10);
if ((eleccion < 3) && (_root.Counter.num < 10)) {

(Where "randRange" returns a random number. If the random number is smaller than 3, and there are less than 10 objects created, we create a new one).

And the main code has, among other things:

import Counter;
import MyGraphic;

var Counter=new Counter();
var MyG=new MyGraphic();

However, the "while" loop in the start() function never ends, and tracing it reveals that "_root.Counter.num" is undefined. In other words, it looks as if the code inside this class can't access the property in the other object.

What am I doing wrong?

Return to posts index

Paulo Jan
Re: (AS2) Accesing another object from a class
on Jan 8, 2009 at 10:56:49 pm

Nevermind. Found out about static variables. Now I have other things that don't work, but they will have to wait until another post...

Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2018 All Rights Reserved