World.create() and Point


(Jacob) #1

Hellooooooooo everybody!

Using world.create(), in recycled entity’s added(), its private var targetPoint:Point is given new values and trace confirms this. Soon later, these values are changed back to what they were the first time the entity was created. Can someone shed some light on why?

package falling_block
{
    private var targetPoint:Point = new Point;

    public function FallingBlock()
    {
    }

    override public function added():void
    {
        targetPoint = getNewCoords();
        trace(targetPoint); // Confirms new coordinates
    }

    // Soon later, in update:
    override public function update():void 
    {
         if (targetPoint != getCorrectCoords()) trace(targetPoint); // Values have changed????
    }
}

I have since fixed the issue by:

override public function added():void 
{
    targetPoint = new Point;
    targetPoint = getNewCoords();
}

But I’m still curious why this happened. Or, maybe this has nothing to do with world.create()? Thanks in advance for any input!


(Jacob Albano) #2

It’s hard to say without knowing what code is in getCorrectCoords() and getNewCoords().


(Jacob) #3

I actually just made those up to show that I’m using some method to give new coordinates to the entity. The real method used happens when the entity is created with world.create():

In FallingBlock's parent class FallingBlockColumn:

fbList.push(FallingBlock(world.create(FallingBlock)).fbSetup(this));

In FallingBlock:

public function fbSetup(host:FallingBlockColumn):FallingBlock
{
	this.host = host;
	x = host.x, y = host.y;
	
	if (name == null)
	{
		name = type + host.host.fbIds.length.toString();
		host.host.fbIds.push(name);
	}
	
	// targetCoords = new Point(host.x, host.y); // The fix
	
	setTargetCoords();
	
	return this;
	
	
	function setTargetCoords():void
	{
		switch (host.host.dirVal)
		{
			case 0: // Spawn from Top
				
				targetCoords.y = host.baseTargetCoords.y - (38 * host.fbList.length);
				break;
				
			case 1: // Spawn from Right
				
				targetCoords.x = host.baseTargetCoords.x + (38 * host.fbList.length);
				break;
				
			case 2: // Spawn from Bottom
				
				targetCoords.y = host.baseTargetCoords.y + (38 * host.fbList.length);
				break;
				
			case 3: // Spawn from Left
				
				targetCoords.x = host.baseTargetCoords.x - (38 * host.fbList.length);
				break;
		}
	}
}

And since they all have names, traces have been run in fbSetup() and added to confirm they have the new correct coordinates. Thanks again for taking a look at this.


(Martí Angelats i Ribera) #4

I think your code is wrongly structured.