Jump to content

Child sprites updating too late


3ddy
 Share

Recommended Posts

tl;dr

when I move parent, child position is updated too late, how to manually force to update child position when I move parent?

 

Hello,
in my game I have a group of sprite objects (Ships)

this.shipsGroup = game.add.group();

Each Ship before adding to this.shipsGroup have two other groups as children :

var actualShip = game.add.group();
var surrounding = game.add.group();

for (var m = 0; m < 3; m++) {
	var temp = game.add.sprite(-halfSize - 50 + l*50, tempy, 'red');
	temp.anchor.setTo(0,1);
	tempy+=50;
	if(m == 1 && l > 0 && l < ship.unitSize+1) {
		actualShip.add(temp);
	} else {
		surrounding.add(temp);
	}
}

ship.addChild(actualShip);
ship.addChild(surrounding);

this.shipsGroup.add(ship);

Note: every child object has position relative to parent (so child position (0,0) is on parent)

Everything is fine up to this point. I'm moving my ships using something like that in every possible direction (I'm moving them on 50x50grid). This check is in update method of game.

if(this.draggedShip.position.x - game.input.mousePointer.x >= 50 && some_boundary_condition) {
	this.draggedShip.position.x-=50;
	this.checkOverlap();
}

So, after my ship has moved to next grid, I'm calling checkOverlap function to check on top of which grid fields actually is the ship. (I have seperate group of sprites for each grid field)

checkOverlap: function() {
	for (var i = 0; i < this.draggedShip.children[0].length; i++) {
		for(var j = 0; j < this.fieldsGroup.children.length; j++) {
			if(this.draggedShip.children[0].children[i].overlap(this.fieldsGroup.children[j])) {
					this.fieldsGroup.children[j].alpha = 1;
			}
		}
	}
},

What is wrong :
it looks like sprite.overlap uses OLD children positions (before parent move). So when I move my ship 1 field to the left, the result of this checkOverlap function are fields where the ship was in previous step.

How it should be:
children positions should be updated before my checkOverlap call. How can I force to do it? I was trying to call update on childrens but it didn't work.

Phaser 2.6.2

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...