How do you interact with all instances of a class from a seperate class?
I've been hitting dead ends while programming lately all over, and I finally came to the realization all the code I have that is broken is because I don't know how to interact with instances of one class from within another. Here's the gist of what I am trying to do:
if(this.hitTestObject(targetClip)){
trace("hit!");
}
The problem is, if I address a single instance that is on the canvas, I get "access to undefined property targetClip", even though the document class can interact with it fine. If I target the class file o开发者_开发知识库f targetClip, I get "Implicit coercion of a value of type Class to an unrelated type flash.display:DisplayObject". It isn't just a problem with hitTest either, I'm having this problem with other actions, so there is some basic syntax I'm failing to comprehend. Please show me what I'm missing.
I'm not 100% on your question but I'll assume that what you are asking for is a way to call the hitTestObject from any one instance of "ClassA" on all instances of "ClassB" right? So something like having a Bullet object check if it hit any Player object.
If that is indeed the question, there is no "global list" of all instances of a class (that I am aware of). However, that doesn't stop you from doing it yourself by using a static vector of the instances of the class. Be warned though as this will require a little bit more cleaning up of your code than normal. Here is a quick example using Bullets and Players.
class Bullet extends Sprite //or w/e it is your objects extend
{
public Bullet()
{
}
public function checkCollisionsWithPlayers() : void
{
for(var i : int = 0; i < Player.AllPlayers.length; ++i)
{
var player : Player = Player.AllPlayers[i];
if(this.hitTestObject(player))
{
trace("hit!");
}
}
}
}
class Player extends Sprite
{
static public const AllPlayers : Vector.<Player> = new Vector.<Player>();
public Player()
{
AllPlayers.push(this);
}
//remove unused Players from the AllPlayers list
public function free() : void
{
var index : int = AllPlayers.indexOf(this);
if(index >= 0)
{
AllPlayers.splice(index, 1);
}
}
}
This allows you to have a list of all Players that are created. Calling the checkCollisionsWithPlayers function on any instance of a Bullet will check for a hit with all currently instanciated Player objects.
However, this will create memory leaks if you do not clean unused Players by calling the free() function once they are no longer of any use (since the Vector keeps a reference on the instance which doesn't allow the garbage collector to clean the memory not to mention that it may return false hits on dead players). Just be careful if you use a pattern like this one to correctly clean the list as it is quite bug prone.
You need to remember that separate classes don't know anything about one another. To access a variable (in, for example, Class A) from a different class (class B), you'll need to make a 'getter' method in class B:
getVariable(){return variable;}
and call that method from class A, either statically (B.getVariable()) or dynamically (B bbb = new B(); bbb.getVariable();)
Classes near explicit directions in order to access information held in other classes
精彩评论