js筆記--1

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.htmljavascript

http://www.ibm.com/developerworks/cn/web/1304_zengyz_jsoo/php

相關文章
相關標籤/搜索