Help with calling the init method


(Oli) #1

For some reason my “init method” does not assign the value to the bullet. It gets called on but all the propreties stay at zero

This is my gun class

this is the bullet


(Martí Angelats i Ribera) #2

Try something like:

public function blastOff(angle:int, power:int):void
{
	trace("Blast Called");
	
	var bullet:Bullet = world.create(Bullet, true) as Bullet;
	bullet.init(angle, power, x, y);
	
	trace("Bulled finished.");
}

And then

public function init(angle:int, power:int, x, y):void
{
	trace("init called.");
	
	this.angle = angle;
	this.power = power;
	this.x = x;
	this.y = y;
	
	trace("end of init");
}

What is the log?


PD: Copy the code and mark it as code to be able to copy-past it :wink: (like i did)


(Oli) #3

Thx Ill try that and tell u :slight_smile:


(Oli) #4
public function init(angle:int, power:int, x:int, y:int):void
    {
            this.angle = angle;
            this.power = power;
            this.x = x;
            this.y = y;

 }
        
        public function Bullet()//Bullet Constructor
        {
        
            trace(angle+" "+power+" "+x+" "+y)

That dosent do it:(


(Jacob Albano) #5

Of course they’ll all be zero in the constructor. Trace the values out at the end of the init function instead.


(Oli) #6

yea in the init() its fine all the values but when the bullet is constructed everything seems to remain at zero:(


(Martí Angelats i Ribera) #7

That’s what is supposed to happen. They’ll be 0 untill the code

this.angle = angle;
this.power = power;
this.x = x;
this.y = y;

Here is where you are setting the values.


(Mike Evmm) #8

There seems to be some confusion about when the constructor function is ran. The constructor function, as the name indicates, is ran when the object is constructed. This means it’s ran at

new Bullet()

which, kind of is ran in world.create (it’s a bit more complicated than that, pooling, but whatever, it does create new bullets at a point).

So, breaking down what you’re doing:

var bullet:Bullet = Bullet(world.create(Bullet, true)); //new Bullet() goes here!
bullet.init();

You can see that you’re running the constructer first (in which you’re not setting any variables to anything, thus they’ll trace out 0) and only afterwards running the init() function (in which you do set variables). If you try tracing them out after all variables are set (i.e. bottom of init function), hopefully everything’s okay!


So, you may be wondering, How come I can’t just use new Bullet() and put my logic in the constructor?

Well, that’s the thing with world.create(). As I mentioned in a long parenthesis a couple of lines back, create() uses pooling, which basically:

  1. Checks if there are any X (in your case bullets) in the pool.
  2. If so, return that X, instead of creating a new one. Skip to 4.
  3. If not, create a new X, add it to the pool for future use, and returned it.
  4. The end.

You can see how this is a lot better than always creating new entities for stuff that could get reused a lot. Like, say, bullets. But if you’re fetching an old X from the pool, you can’t really re-run its constructor, since you’re not constructing it. That’s where a function like init() comes handy.

Calling world.recycle is what adds an entity to a pool.


Kinda went overboard on this one, been reading a lot (hurrah summer), but I hope it’ll be helpful in the future. Also that I haven’t made some horrible, horrible mistake.

Here’s a thing if you want to learn from someone who know’s what he’s talking about.


(Oli) #9

Thx I think I understand! Breaking it down like that really helped thx:)