您的位置:首页 > Web前端 > JavaScript

js笔记--1

2015-07-16 17:37 543 查看
1.创建一个layer层
var GameLayer = cc.Layer.extend({
_time:null,
_ship:null,
_backSky:null,
// 构造函数
ctor:function(){
this._super();
this.init();
}, //注意要有逗号

// 初始化函数
init:function () {
return true;
},

// 成员函数
scoreCounter:function () {
if (this._state == STATE_PLAYING) {
this._time++;
this._levelManager.loadLevelResource(this._time);
}
},

// 带参数
collide:function (a, b) {
var ax = a.x, ay = a.y, bx = b.x, by = b.y;
if (Math.abs(ax - bx) > MAX_CONTAINT_WIDTH || Math.abs(ay - by) > MAX_CONTAINT_HEIGHT)
return false;

var aRect = a.collideRect(ax, ay);
var bRect = b.collideRect(bx, by);
return cc.rectIntersectsRect(aRect, bRect);
} // 最后一个可以不需要逗号

});

// 如果有需要通过切换场景的方式来进入下一个界面则可以创建这个方法、
// 在得到scene之后可以通过这个方式来进入
// cc.director.runScene(new cc.TransitionFade(1.2, scene));
GameLayer.scene = function () {
var scene = new cc.Scene();
var layer = new GameLayer();
scene.addChild(layer, 1);
return scene;
};

// 点击开始新游戏时会加载主游戏界面,这时候可能需要加载的资源会比较多
// 所以会用到预加载的方式、这也是成员函数,在点击按钮的时候响应
onNewGame:function (pSender) {
//load resources
cc.LoaderScene.preload(g_maingame, function () {
cc.audioEngine.stopMusic();
cc.audioEngine.stopAllEffects();
var scene = new cc.Scene();
scene.addChild(new GameLayer());
scene.addChild(new GameControlMenu());
cc.director.runScene(new cc.TransitionFade(1.2, scene));
}, this);
},

// 加载plist文件
cc.spriteFrameCache.addSpriteFrames(res.textureTransparentPack_plist);

// 得到屏幕宽高
winSize = cc.director.getWinSize();

// 创建精灵,方式一
var sp = new cc.Sprite(res.loading_png);
sp.anchorX = 0;
sp.anchorY = 0;
sp.scale = MW.SCALE;
this.addChild(sp, 0, 1); //这里的this相当于lua中的self

// 创建精灵方式二
var logo = new cc.Sprite(res.logo_png);
logo.attr({
anchorX: 0,
anchorY: 0,
x: 0,
y: MW.LOGOY,
scale: MW.SCALE
});
this.addChild(logo, 10, 1);

// 创建按钮
var newGameNormal = new cc.Sprite(res.menu_png, cc.rect(0, 0, singalWidth, singalHeight));
var newGameSelected = new cc.Sprite(res.menu_png, cc.rect(0, singalHeight, singalWidth, singalHeight));
var newGameDisabled = new cc.Sprite(res.menu_png, cc.rect(0, singalHeight * 2, singalWidth, singalHeight));

var newGame = new cc.MenuItemSprite(newGameNormal, newGameSelected, newGameDisabled, function () {
// 按钮的响应函数
}.bind(this));

newGame.scale = MW.SCALE; // 设置属性
var menu = new cc.Menu(newGame, gameSettings, about);
menu.alignItemsVerticallyWithPadding(15);
this.addChild(menu, 1, 2);

// 帧事件、
this.schedule(this.update, 0.1);

update:function () {
if (this._ship.y > 750) {
this._ship.x = Math.random() * winSize.width;
this._ship.y = 10;
this._ship.runAction(cc.moveBy(
parseInt(5 * Math.random(), 10),
cc.p(Math.random() * winSize.width, this._ship.y + 750)
));
}
},

// 动作
this._ship = new cc.Sprite("#ship03.png");
this._ship.runAction(cc.moveBy(2, cc.p(Math.random() * winSize.width, this._ship.y + winSize.height + 100)));

// js数组 http://www.w3school.com.cn/jsref/jsref_obj_array.asp // js Boolean 对象 http://www.w3school.com.cn/jsref/jsref_obj_boolean.asp 
// this指针
//关于Javascript的this指针,和C++/Java很类似。我们来看个示例:(这个示例很简单了,我就不多说了)

function print(text){
document.write(this.value + ' - ' + text+ '<br>');
//这里的this是调用print函数的对象
}

var a = {value: 10, print : print};
var b = {value: 20, print : print};

print('hello');// this => global, output "undefined - hello"

a.print('a');// this => a, output "10 - a"
b.print('b'); // this => b, output "20 - b"

a['print']('a'); // this => a, output "10 - a"

http://www.php100.com/html/webkaifa/javascript/2012/0228/9927.html http://www.ibm.com/developerworks/cn/web/1304_zengyz_jsoo/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: