標籤:java ext int c get javascript
# 時間軸
## director
一切從director開始。director是一個每個遊戲所需要的基本對象,它串連所有網頁上的Limejs邏輯到單個地方。如果你來自flash世界你可以把它當做一個舞台,Cocos2d使用者可以當做Cocos2d-s自己的director。所有其他人可以把它當做一個前部的控制者。
每一個遊戲只有一個director的例圖。它操控遊戲的全程視口,控制螢幕上的可見物。在你的邏輯湧現之初你得建立一個director的例圖。建構函式的方法的參數是container DOM元素,舞台寬度和高度以像素表示。
#!JavaScript
var director = new dfkit.Director(document.body,320,460);
## scene
scene是一個可視元素的不獨立的部分,覆蓋了所有的視口,這說明在同一時間只有一個scene能被啟用。比如說,在一般的遊戲邏輯中你將會有菜單的scene,遊戲的scene和遊戲結束的scene。要使一個scene可見,你調用‘director.replaceScene(scene)’或者‘director.pushScene(scene)’。區別就在於*pushScene* 不會去除之前的scene,但是將他保留在一個隱藏的stack中,因此你可以通過“director.popScene()”使它可見
#!JavaScript
var scene = new lime.Scene();
director.replaceScene(scene);
## 過度
僅僅使用‘replaceScene()’只能做到快速的scene間的切換,以至於在視覺上不可接受。你可以通過設定‘replaceScene()’的*transition*和*duration*屬性以使它更好。transition定義了當前的scene被解除和新的scene被啟用的動畫製作。現成的有多種多樣的*Silde*和*Move*的過度,還有用於淡入效果的*Dissolve*
#!JavaScript
director.replaceScene(menuscene,lime.transition.SlideInRight);
director.replaceScene(gamescene,lime.transition.Dissolve,2);
## scheduleManager
Lime中的所有東西都是以repaint-dirty模式繪製的,這說明每次你改變某些東西,你的方法調用不會嚴格地與對等的DOM或Canvas2dContext調用相互關聯,Instead property you set is marked as dirty and will be redrawn in the next frame. This allows us to update only once, update only what is necessary and keep all updates stateless,最後一條準則允許我們在任何時候在渲染的方法之間切換。由於在Javascript中沒有任何的on-enter-frame事件,我們坐了一個*lime.sheduleManager*靜態對象來類比它。以下是它提供的方法:
- schedule(callback, context)
- Call a function in every frame. Context is object that represents *this*.
this固定指向運行時的當前對象
- unschedule(callback, context)
- Clear previously scheduled function.
- scheduleWithDelay(callback, context, delay, opt_limit)
- Same as *schedule* but function is only called if if more than *delay* seconds has passed from last execution.
- callAfter(callback, context, delay)
- Only call function once after the delay.
你將不會在你的遊戲代碼中直接使用Javascript內建的方法‘setTimeout()’和‘setInterval()’,*lime.schduleManager*為你提供了有相同功能和額外特性。你的回呼函數通過同一個回呼函數最近的一次執行在幾毫秒內所延遲的參數被調用(Your callback is called with a parameter that is the delay from the last execution of the same callback in milliseconds)。這允許你做出順利的動畫效果即使CPU的表現大大改變
#!JavaScript
var velocity = 2;
lime.scheduleManager.schedule(function(dt){
var position = this.getPosition();
position.x += velocity * dt; // if dt is bigger we just move more
this.setPosition(position);
},ball);
## Pausing
使用scheduleManager而不是timer函數也給我們帶來支援暫停好處。當你希望暫停你的遊戲你只要在你的director對象上調用‘setPaused(true)’,這能暫停所有的時刻表函數和動畫效果。一旦通過調用‘setPaused(false)’你恢複,你的所有代碼將照常恢複。當你的遊戲處於暫停狀態,*lime.PauseScene*的例圖被放置在director的一個啟用的scene中。如果你希望在這個狀態有一些定製的外觀,你可以無視這個類功能。
#!JavaScript
mygame.director.setPaused(true);
## 層
現在我們準備在螢幕上加一些東西。為了更好地展示對象我們引入*lime.Layer*對象。你可以把它當做ps中的層。層是用來承載物品的,它們就像任何其他對象,區別就在於它們沒有身體或大小。它們的唯一內容就是它們的子物件。所以如果需要的話,你建立它們,將它加到tree,位置,並將子物件加到它們身上。只需明白,層不是必須的 - 你可以添加任何展示的對象到scene上,而層使你的檔案更清晰。
#!JavaScript
var layer = new lime.Layer().setPosition(100,100);
scene.appendChild(layer);
for(var i=0;i<5;i++){
var box = customMakeBoxFunc().setPosition(i*50,0);
layer.appendChild(box);
}