Jump to content

Children of Sprite doesn't seem on the screen


Recommended Posts



I'm trying to make a tetris-alike game. So, I want to generate my blocks with only one square image. I'm creating a sprite and adding others to it, so that they all move together when dragged. But I couldn't solve the problem that I cannot see children sprites on the screen.. Only one square. I've checked their coordinates with console.log and they seem to be different and in the screen but I can't see anything but one square. So, here is my code:


My box class:

Box = function (game, id, place) {         this.myid = id;        var assetName = id.toString();    assetName = 'id' + assetName;    this.yPos = 1000;        var color;    //place assignment    switch (place){        case 1:            this.xPos = 126.5;            break;        case 2:            this.xPos = 320;            break;        case 3:            this.xPos = 511.5;            break;        default:            break;    }    Phaser.Sprite.call(this, game, this.xPos, this.yPos, 'kare');        this.inputEnabled = true;        this.input.enableDrag();    this.input.enableSnap(55, 55, false, true, 45, 323.5);    //color assignment    if (id > 0 && id < 10)        color = 0xcf0a2c;    else if(id > 9 && id < 14)        color = 0xffbf3b;    else if (id == 14 && id == 15)        color = 0x43a39f;    else        color = 0x055154;        this.generateSquares(game);        };

Here children are generated and added to main sprite according to their coordinate array. "this" is Box. I've skipped some parts because they are unrelated and long.

         for (var i = 0; i<5; i++){        for(var j = 0; j<5; j++){            if (this.coordinates[i][j] == true){                var x = this.xPos+i*55;                var y = this.yPos+j*55;                                var spriteToAdd = game.make.sprite(x, y, 'kare');                this.addChild( spriteToAdd );            }        }    }

I create them in game.js this way:

box1 = new Box(this, 4, 1);this.add.existing(box1);box2 = new Box(this, 15, 2);this.add.existing(box2);box3 = new Box(this, 18, 3); this.add.existing(box3);

I see only three square at given coordinates. I tried to log coordinates of every sprite that is added as children and they seem fine, no problems like being out of screen etc.


Link to comment
Share on other sites

Yeah, it worked. I deleted xPos and yPos from the coordinates of the children. Thank you.


But I really looked at shole Sprite documentation and could not find much information about children, especially about their placement. So, it would be better to add some info there.


Also, one piece of recommendation is in documentation if two or three lines of code were given as example and then a link to full example would do better.

Link to comment
Share on other sites


  • Recently Browsing   0 members

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