Cocos2D-JS-Quick-Tutorials icon indicating copy to clipboard operation
Cocos2D-JS-Quick-Tutorials copied to clipboard

04 | Cocos2D-JS | How to center a sprite in Screen

Open Gurigraphics opened this issue 7 years ago • 0 comments

04 | How to center a sprite in Screen

Game.layers.js

In the Game.layers.js a) Take the dimensions of the canvas b) Define the positions x and y c) Define the value of the anchor

    Game.layers[1].extend = cc.Layer.extend({ 
	    init: function () { 
		    this._super(); 
		    var game = this; 
		    Game.layers[1].start( game ); 
	    } 
    }); 
    
    Game.layers[1].start = function( game ){ 
	    var size = cc.director.getWinSize(); //a  
	    var sprite = new cc.Sprite ( "HelloWorld.png" );
	    sprite.attr({ 
		    x: size.width / 2, //b
		    y: size.height / 2, 
		    anchorX: 0.5, //c
		    anchorY: 0.5 
	    }); 
	    game.addChild(sprite); 
    };

Gurigraphics avatar Apr 29 '18 22:04 Gurigraphics