对Cocos2d-JS程序的简单剖析
发布时间:2020-12-14 16:38:49 所属栏目:百科 来源:网络整理
导读:当我们创建好一个游戏工程后,引擎自动为我们创建了一个场景文件,即 src 文件夹下的 app.js 。这应该是一个典型的构建游戏场景的文件,以后创建自己的游戏场景,代码内容应该与此相似: //创建一个层(Layer) var HelloWorldLayer = cc.Layer.extend({ sprit
当我们创建好一个游戏工程后,引擎自动为我们创建了一个场景文件,即 //创建一个层(Layer)
var HelloWorldLayer = cc.Layer.extend({
sprite:null,ctor:function () {
// 1. super init first
this._super();
// 2. add a menu item with "X" image,which is clicked to quit the program
// ask the window size
var size = cc.winSize;
// add a label shows "Hello World"
// create and initialize a label
var helloLabel = new cc.LabelTTF("Hello World","Arial",38);
// position the label on the center of the screen
helloLabel.x = size.width / 2;
helloLabel.y = size.height / 2 + 200;
// add the label as a child to this layer
this.addChild(helloLabel,5);
// add "HelloWorld" splash screen"
this.sprite = new cc.Sprite(res.HelloWorld_png);
this.sprite.attr({
x: size.width / 2,y: size.height / 2
});
this.addChild(this.sprite,0);
return true;
}
});
//创建一个游戏场景(Scene)
var HelloWorldScene = cc.Scene.extend({
onEnter:function () {
this._super();
var layer = new HelloWorldLayer();
this.addChild(layer);
}
});
以上代码中,创建了一个层(Layer)和一个场景(Scene),然后把层添加到场景中。 方法讲解:
名词解释: 1.场景( Cocos2d-JS引擎抽象的一个对象,用Cocos2d-JS制作游戏就如同拍电影,事实上所有东西都必须放置到一个场景容器中才能最终被显示出来。游戏中我们通常需要构建不同的场景(至少一个),游戏里关卡、界面的切换其实就是一个一个场景的切换,就像在电影中变换舞台或场地一样。 2.层( 通常包含的是直接在屏幕上呈现的内容,并且可以接受用户的输入事件,包括触摸,加速度计和键盘输入等。每个游戏场景都可以有 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |