Wednesday, March 27, 2013

cocos2d-html5 setOpacity doesn't work?

I am trying out cocos2d-html5 and implementing a game similar to Word Drift so people can try it on the web.  At one point I want to make a sprite transparent initially.  So I do this:

    this.ss = cc.Sprite.create("res/select.png");
    this.ss.setPosition(65/2, 65/2);
    this.ss.setOpacity(0);


but it doesn't work.  The sprite is still showing on screen.  Turn out that, to make it works, the image has to be preloaded.  That is, if you using the project template bundled within cocos2d-html5 package,  you will have to put this image in g_resource in resource.js

var s_select = "res/select.png";
var g_ressources = [ {type:"image", src:s_select} ];

The images in g_resources get loaded in main.js

cc.Loader.getInstance().preload(g_ressources);

and that makes setOpacity() works.

I'm sure there are documents somewhere explaining why the images needs to be preloaded, but I'm too lazy to find it out for now.  It's more fun coding than hunting for document :)  Anyway if you know why please post it in the comment.

No comments:

Post a Comment