Cocos2d-JS引入资源_Cocos2d-JS

以图片为例:

创建项目后,把图片放入res文件夹,修改

app.js

var HelloWorldLayer = cc.Layer.extend({
    sprite:null,
    ctor:function () {
        //////////////////////////////
        // 1. super init first
        this._super();
        
        //引入资源
        var size =cc.winSize;
        var startBtn=new cc.Sprite(res.start_N_png);
        this.addChild(startBtn);
        
        //放中间
        startBtn.setPositionX(size.width/2);
        startBtn.setPositionY(size.height/2);
        
        return true;
    }
});

var HelloWorldScene = cc.Scene.extend({
    onEnter:function () {
        this._super();
        var layer = new HelloWorldLayer();
        this.addChild(layer);
    }
});

resource.js

var res = {
    HelloWorld_png : "res/HelloWorld.png",
    CloseNormal_png : "res/CloseNormal.png",
    CloseSelected_png : "res/CloseSelected.png",
    //添加资源
        start_N_png:"res/start_N.png"
};

var g_resources = [];
for (var i in res) {
    g_resources.push(res[i]);
}

运行查看:

Cocos2d-JS引入资源_创建项目_02

Cocos2d-JS引入资源_创建项目_03