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

03 | Cocos2D-JS | How to display a sprite on the screen

Open Gurigraphics opened this issue 7 years ago • 0 comments

03 | How to display a sprite on the screen

Game.resources.js

In the Game.resources.js set the path to the image

    Game.res = { 
	    HelloWorld_png : "HelloWorld.png" 
	};
	
    Game.g_resources = []; 
    
    for ( var i in Game.res ) {
	    Game.g_resources.push( Game.res[i] ); 
    }

Game.layers.js

In the file Game.layers.js:

a) Create a new sprite b) Define the positions x and y c) Add the sprite to the game

    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 sprite = new cc.Sprite ( "HelloWorld.png" ) ; // a
	    sprite.attr({ x: 0, y: 0 }); //b
	    game.addChild(sprite); //c 
    };

Gurigraphics avatar Apr 29 '18 21:04 Gurigraphics