Sprite screen position

I’m trying to create a chess board, and place it in the middle of the screen, so far i cannot get it to be directly in the center. i don’t want to hard code the position to the screen because i’m going to be dealing with different screen sizes.

    var winsize = cc.director.getWinSize();
    var centerpos = cc.p(winsize.width / 2, winsize.height / 2);
    
    for (i=0; i<64; i++){
    	var tile = cc.Sprite.create(res.tile_png);
    	this.addChild(tile,0);
    	tile.setPosition(winsize.width+i%8*50/-10, winsize.height-Math.floor(i/8)*50);
    	
    }

But the tiles and positioning is completely off

Why do you calculate the center position and never use it? Are you meant to use the center position?

The math I would use would be something like

x = centerpos.x + ((i % 8) - 3.5) * tile.getBoundingBox().size.width
y = centerpos.y + (Math.floor(i / 8) - 3.5) * tile.getBoundingBox().height

Not sure if that’s right, cos I’m just trying to work it out in my head.

1 Like