COCOS2D-HTML5--helloWord

先建立本身的JS文件,在文件中添加场景,在场景中设计布局。app

HelloWord.js:布局

var helloLayer = cc.Layer.extend({
    sprite: null,
    helloLabel: null,
    init: function () {
        this._super();
        // 得到画布的大小,其实能够理解为得到游戏的边界大小
        var size = cc.Director.getInstance().getWinSize();
        // 添加一个Label
        this.helloLabel = cc.LabelTTF.create("Hello World", "Arial", 38);
        this.helloLabel.setAnchorPoint(cc.p(0.5,0)); //将锚点设置到:中下
        this.helloLabel.setPosition(cc.p(size.width / 2, 0));
        this.addChild(this.helloLabel, 5);
        // 添加一张图片(精灵)
        this.sprite = cc.Sprite.create("res/HelloWorld.jpg");
        this.sprite.setPosition(cc.p(size.width / 2, size.height / 2));
        this.addChild(this.sprite, 0);
        return true;
    }
});
var helloScene = cc.Scene.extend({
    onEnter : function(){
        this._super();
        var layer = new helloLayer();
        layer.init();
        this.addChild(layer);
    }
});

将本身的JS文件引入到cocos2d.js中this

appFiles:[
            'src/resource.js',
            'src/HelloWord.js'//add your own files in order here
        ]

在main.js中设计初始场景为本身的场景
设计

var myApp = new cocos2dApp(helloScene);
相关文章
相关标签/搜索
本站公众号
   欢迎关注本站公众号,获取更多信息