Targeting Instances from Class

I’m working on a project where I want to target multiple movieclip instances on the stage from an external class. Any ideas on how to do this? Do you have to pass the variables through a document class? I tried that, but couldn’t get it to work.

The structure of what I am working on is:

Stage - with enemy instances
Main document file - basic actions
External AS GunTurret file - with turret that needs to be able to target enemy instances
External AS Enemy file - actions for each enemy instance (already on stage)

Any help would be greatly appreciated.

anyone?

You have to pass reference of the enemies to the turret class. That is, GunTurret should have a public array or some public properties that you pass reference of the enemies to (ie: gunTurretInstance.enemies[0] = enemyInstance172; gunTurretInstance.enemies[1] = enemyInstance189;). Also, due to scope, those assignments obviously have to be done on the stage or whatever object is holding both the enemies and the GunTurret instance.

From my understanding of what you are saying, I should make an array to pass through the enemy instance names. However, it isn’t the document class. From what I know, the file has to be the document class to reference objects on the stage. My error is 1120: access of undefined property. Maybe I’m missing something?

Not to reference them but to refer directly to them, yes the file has to be the document class. But the document class can pass reference of an object to another class. That class can then act on the object even though it is outside of the class’s scope.

It may be that the objects have to be programatically generated to be accessed by classes other than the document. You should try passing reference to a programatically created enemy and an enemy added to the stage to see which or if both work.

Okay, so, I tried passing through one enemy instance through the document file to the GunTurret file, but it still is undefined. I also added the enemy with addChild, not dragging it on the stage. I need to be able to target the enmy from the GunTurret file so I can perform either a hitTest or a distance measurement from the turret its self. I attached the files in a zip if it helps at all. Thanks for the help

[quote=Ikinsey;2328323]Not to reference them but to refer directly to them, yes the file has to be the document class. But the document class can pass reference of an object to another class. That class can then act on the object even though it is outside of the class’s scope.

It may be that the objects have to be programatically generated to be accessed by classes other than the document. You should try passing reference to a programatically created enemy and an enemy added to the stage to see which or if both work.[/quote]

I can’t find your code in game. Your main actions frame has nothing in it.

Its in the TurretGame.as file

Where do you use that class in the game though?

Its just used to keep all of my actions. I only put it into the external as file to test the enemy variable problem. It isnt called since its the document class. If you click on the background, off the stage, you can see in the properties window that the document file is: TurretGame

No ideas anyone?

Ok habes, I see in here your GunTurret class has:

private var targetEnemy = addEnemy;

Where addEnemy is a public var of TurretGame. That’s a scope issue, classes have no access to the vars of other classes. Only instances of classes to which they have referance.

Now in your GunTurret you can make targetEnemy a public var and then when you create instances of GunTurret in TurretGame (as you already do) you can pass references of addEnemy like so.

newturret.targetEnemy = addEnemy;

Also, I wouldn’t use the syntax

addEnemy = addChild(new Enemy());

I would use the method:

var addedEnemy:Enemy = new Enemy();
addChild(addedEnemy);

Its more concise and addedEnemy still has reference to the new Enemy even though it was added.

Okay, I’ll try it out. Thanks for your help.

Alright! Got it! Thank you so much for your help!

S’what I’m here for.