GregorII
GregorII

Reputation: 221

converting element by using AS operator

So im creating something that now is finished and i want not to create every time elements, but to Pool them (ObjectPooling)

The problem comes that my object from the pool doesnt have the variable from the class it mimics, or at least i understand it that way, cause it doesnt do what it should.

Can someone tell me does this

var myNewBox:Box = Pool_myBox.getSprite() as Box;

mean that all the proparties and parameters that the class Box() has will be given and can be used by the new variable myNewBox or it`s a little more tricky that this?

or in other words is var myNewBox:Box = new Box();

the same as

var myNewBox:Box = Pool_myBox.getSprite() as Box;

------------EDIT-----------

so i do private var Pool_myBox:SpritePool; in the Main Class .

and set Pool_myBox = new SpritePool(Bullet,20); so in the 1st moment it has generated 20 objects. The whole SpritePool class is this

package {
import flash.display.DisplayObject;

public class SpritePool {
    private var pool:Array;
    private var counter:int;
    private var classRef:Class;

    public function SpritePool(type:Class, len:int) {
        pool = new Array();
        counter = len;
        classRef = type;

        var i:int = len;
        while (--i > -1) {
            pool[i] = new type();
        }
    }

    public function getSprite():DisplayObject {
        if (counter > 0) {

            return pool[--counter];
        } else {
            increasePool(10);
            return getSprite();
        }
    }

    public function increasePool(amount:int):void {
        counter +=  amount;
         while( --amount > -1 ) 
                pool.unshift ( new classRef() ); 
    }


    public function returnSprite(s:DisplayObject):void {
        pool[counter++] = s;

        //trace(pool.length)
    }
}

}

Upvotes: 0

Views: 38

Answers (2)

dhc
dhc

Reputation: 657

if Pool_myBox.getSprite returns only Box objects, then you don't need to cast. The getSprite function should be look something like:

public function getSprite():Box {
    var recycled_box:Box = ...  // get box from pool
    return recycled_box;
}

var myNewBox = Pool_myBox.getSprite();

Then, myNewBox will look and act like a Box. Note that any initialization or processing that happened on previous Box instances must be undone when it's returned to the pool if you need a "fresh" instance of Box.

OK, given the pool class, it looks like it should work with casting. Note that your text says you're passing in "Bullet" as the Class, while your code seems to want Box's (I assume this is either a typo, or Bullet is a superclass of Box?). If it works on the first 20, but not after you start recycling, then check what may need to be undone (as above).

What behavior are you seeing that makes you think it's not returning the right Class?

Upvotes: 0

Fygo
Fygo

Reputation: 4665

Absolutely not. If your getSprite() method does not return a Box instance (or some descendant of it), it will not 'inherit' the properties of Box. as is not performing any kind of internal magic - it is simply casting and telling the compiler that you know what you are doing and that the object indeed is a XXX instance (fill in). You should use casting only when going from a more general type to a more specific type, let's assume this:

var child:Sprite = parent.getChildAt(0); //what does this return? A display object instance => compiler will throw an error as Sprite is not DisplayObject
/*
Implicit coercion of a value with static type flash.display:DisplayObject to a possibly unrelated type flash.display:Sprite.
*/
//so you cast it:
var child:Sprite = parent.getChildAt(0) as Sprite; //this won't throw anything cos you casted it correctly

Also note that:

myObj as MyObj

is the same as:

MyObj(myObj)

Upvotes: 1

Related Questions