Sign in to follow this  
3ddy

Child sprites updating too late

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

Share this post


Link to post
Share on other sites

If possible, run `checkOverlap()` at the very start of the update, before moving any objects. That usually avoids the issue.

Otherwise, you can call `updateTransform()` on the moved objects (or its ancestors; I'm not sure) before testing the overlap. 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.