Jump to content

Phaser swapping items


Bogdan
 Share

Recommended Posts

Hello, I am making my first Phaser game, simple match three clone, and ran into the problem, with swapping items. In theory my code should work, but its not :/

 

My swapping function:

function switchGems(from, to) {  // Get real selected item  for (var i = 0; i < TILES.children.length; i++) {    if (TILES.children[i] === from) {      console.log(TILES.children[i].gem.position);      console.log(to.gem.position);      TILES.children[i].gem = to.gem;      console.log(TILES.children[i].gem.position);      console.log(to.gem.position);    }  }}

And it does nothing visually, but console returns result as expected:

// Before swapPoint {x: 523, y: 275, type: 25}Point {x: 523, y: 330, type: 25}// After swapPoint {x: 523, y: 330, type: 25}Point {x: 523, y: 330, type: 25}

Expected result would be one gem sitting onto another, but it does nothing.

 

Is it because I am assigning gem property to tile that does not exist in Phaser core?

 

var gem = game.add.sprite(tile_x, tile_y, 'gem-' + Math.floor((Math.random() * 4)));gem.anchor.setTo(0.5, 0.5);tile.gem = gem;

Or am I missing something bigger?

Link to comment
Share on other sites

It's really hard to tell without seeing all your code, since we can't see what the TILES property is or what it's a child of, but if you want Phaser to render the sprite correctly, it needs to be somewhere Phaser can find it during its rendering operations. Generally, it needs to be a child of game.world, or a child of something that is in the game.world, etc:.

 

If the only place you indicate that the entity exists is in the "gem" property, Phaser doesn't know it should be trying to render that. It will walk down all the nodes from game.world.children (children), but it won't look in any of your custom properties. You can write the render in manually if you want, but it's probably more advisable to make sure the object exists somewhere in the chain of child nodes that link up to game.world.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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