Using static methods with cocos2d-html5

Using static methods with cocos2d-html5
0.0 0


Hi, I want to use static methods like in cocos2d-x (c++) with cocos2d-html5 (javascript).

I want to do something like that

var buttonStar = Helloworld.standardButtonWithTitle("My Button");

I don’t want to create a ‘standardButtonWithTitle’ method for each class that I have.

How I can do that?


I did it,

simple use this snippet code

function Redirect(){}

Redirect.goToOptions = function()
{   cc.Director.getInstance().replaceScene(new OptionsScene());     }
Redirect.goToShop = function()
{   cc.Director.getInstance().replaceScene(new ShopScene());        }


I have seen two games in iTunes.
Are they both released by Cocos2d-x JSB?


No, I use cocos2d-x, but not use javscript binding, but I begin to learn how to use it, and I pretend to use it.
My idea is to develop games using cocs2d-html5 and use the JSB to create native apps.


Man I am having the same problem here… I extended the LayerColor as a new class (myLayerColor) and now I want to create it using the .create(cc4b Color4B)…

I could not understand how you solve your problem :/…

In this case using the create is important, but I can not use it direct from the LayerColor.creat, othewise the object created will not be a MyLayerColor, in the other hand I could not manage to access the statics methods from my new class.

Grande abraço do Brasil :wink:


Hi Pedro,
I don’t understand exactly what you want.

But I think you create something like this.

var MundoUm = cc.Layer.extend({

//lots of code


var MundoUmScene = cc.Scene.extend({
    onEnter:function () {
        var layer = new MundoUm();

About the static methods, you can create it in any file that you want.

I create my inside my mainmenu.js

var MainMenu = cc.Layer.extend({
   init:function () {
      //lots of code
//register the class name
function Utils(){}

Redirect.goToGame = function()
{   cc.Director.getInstance().replaceScene(new HelloWorldScene());      }
Redirect.goToOptions = function()
{   cc.Director.getInstance().replaceScene(new OptionsScene());     }
Redirect.goToShop = function()
{   cc.Director.getInstance().replaceScene(new ShopScene());        }
Redirect.goToMainMenu = function()
{   cc.Director.getInstance().replaceScene(new MainMenuScene());    }
Redirect.goToMundoUm = function()
{   cc.Director.getInstance().replaceScene(new MundoUmScene()); }
Redirect.goToMundoDois = function()
{   cc.Director.getInstance().replaceScene(new MundoDoisScene());   }

//Global values - constant values
var KDamageShuriken =111;
var KSpeedShuriken =112;
var KRangeShuriken =113;
var KEquippedShuriken= 121;

It’s that you want?

Or your problem is to use the Color4B to create the ColorLayer?


Sorry if I wasn’t clear. What I have is:

myLayerColor = cc.LayerColor.extend({
ctor:function() {
init: function {
— more code —
— more methods —

But when creating this layer from anywhere in my code I can’t call it as:

var myLayer = myLayerColor.create(color);

Because it can not identify create as a static method from MyLayer, even it being from the LayerColor (what is not wrong, but it would be nice to have).

My workaround is to copy the method create from LayerColor to MyLayer, but I would like to know if there is any other “better way” to solve it.

PS: Parabéns pelos games!


I look in the reference documentation for cocos2d-html5, I see the create method for cc.Layer don’t receive parameters and the create method for cc.LayerColor receive 3 parameters, instead of 1.
No one create method from or inherited by cc.Layer use 1 parameter.
The other two parameters are width and heigth.
Take a look in the documents reference and see this can help you.

ps: que bom q vc gostou dos meus jogos
take a look in the link above, it’s my game in Chrome Store (I create it using cocs2d-html5)


As you can check at

The method create from LayerColor can indeed receive just a Color4B, it has some default values for width and height, so they are not needed.

Anyways, even using the 3 parameters it doesn’t work… IMO my question is more regarding the OO in JS than about cocos2D in fact :confused:

Nice job with your game athe Google Store, nice to see cocos being used in many ways!
Did u use the binding for Chrome Native or it is just a HTML5 game running?


You are right about the parameters in create method.

I try to reproduce your problem and it checked console, it says that create method don’t exist for the object.

Try to use init method, it have the same parameters.

Sincerely I don’t understand why create not work, if init not work, I’ll can’t help you.

ps: my game is just use HTML5, not Chrome native


Fabio, check your server… your html5 game is not working here… maybe some problem with my firewall, not sure… it would be better to check anyway.

Yeap, init is pretty similar, but I really would like to use create, since it is a better standard and it auto alloc memory for that object.


The init method works for you.
You could use init and release the memory later.

I checked my server now, the game works ok, it only leave a time to begin the first time.
My game only works in Chrome and IE 9(using Windows), safari and firefox got problems.

In Mac, it not works properly in any browser. I don’t know why.

Try to use this direct link

I’ll test my game in other computers, thanks for wanrning.