Jump to content

Why my last created sprite moves with my last created group?


thiezar
 Share

Recommended Posts

Hi all.

I have  a problem with my game I can't understand. Here the code:
 

function createTitleView() {    //Creates title    title = game.add.sprite(game.world.centerX, game.world.centerY-60, 'title');     title.anchor = new Phaser.Point(0.5,0.5);    title.name= "title";    //Creates play button    playBtn = game.add.sprite(game.world.centerX, game.world.centerY+60, 'playBtn');    playBtn.anchor = new Phaser.Point(0.5,0.5)    playBtn.name = "play button";    //Creates game view    titleView = game.add.group(); titleView.name = "title view";    titleView.add(title);    titleView.add(playBtn);    //Assigns event listener to play button    playBtn.inputEnabled = true;    playBtn.events.onInputDown.add(startGame, this);}function startGame() {     // Creates the player     gameView = game.add.group();     gameView.name = "game view";     unicorn = game.add.sprite(100, 100, 'unicorn'); unicorn.name = "unicorn";     gameView.add(unicorn);     // Slides out the title view     titleViewSlideOut = game.add.tween(titleView).to({y: -game.world.height}, 500, Phaser.Easing.Exponential.In, true);    createGameView();}

Now the fact is that the unicorn's position is relative to the playerBtn and moves with it. Why is that if they're on two separate groups?

Here is a dump of the titleView

        Node        |        Next        |      Previous      |       First        |        Last           ----------     |     ----------     |     ----------     |     ----------     |     ----------          group        |       title        |   credits button   |       group        |    play button         > title       |    play button     |       group        |       title        |       title          play button     |       group        |       title        |    play button     |    play button      

and here is a dump of the game view
 

        Node        |        Next        |      Previous      |       First        |        Last           ----------     |     ----------     |     ----------     |     ----------     |     ----------          group        |      unicorn       |    play button     |       group        |      unicorn          > unicorn      |         -          |       group        |      unicorn       |      unicorn    

As you can see the play button is somehow part of the gameView. How could it be possible?

Link to comment
Share on other sites

not entirely sure about the behavior you're seeing, but your sprites are all being added to multiple groups, because calling the function 'game.add.sprite( x, y, key, frame, group )' adds sprites to the 'world' group if no group is specified in the 5th parameter. so

playBtn = game.add.sprite(game.world.centerX, game.world.centerY+60, 'playBtn');

adds 'playBtn' to the 'world' group, and then you are also adding it to another group.

 

if you want to create a sprite without adding it to any group (including the 'world' group), just create it using 'new'. e.g.:

playBtn = new Phaser.Sprite( game, x, y, key, frame );

and then add it to the group you want it in.

 

OR use 'group.create()' (which is what game.add.sprite() is calling), e.g.

playBtn = someViewICreated.create( x, y, key, frame );
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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