Saturday, May 23, 2015

Implement Camera (former: CCCamera) v2 in cocos2d-x v3.3

Reference: Implement Camera (former: CCCamera) v2 in cocos2d-x v3.3

we want to port camera code from cocos2d-x version 2 to version 3.3 with the goal to imitate the behaviour of a CCCamera in cocos2d-x version 2 exactly.

You can assume that values for "eye" and "up" are calculated correctly.

Old code operating on CCCamera:
camera->setEyeXYZ(eye.x, eye.y, 0);
camera->setCenterXYZ(eye.x, eye.y, -1);
camera->setUpXYZ(up.x, up.y, 0);
New code operating on Camera:
camera->setPosition3D(Vec3(eye.x, eye.y, 0.0f));
camera->lookAt(Vec3(eye.x, eye.y, -1), Vec3(up.x, up.y, 0.0f));
The setPosition3D part works fine and this is also a good indicator that I've set up the camera correctly in terms of setCameraFlag and Mask, correct layers, etc. but for some reason the tilting (modification of the up vector) doesn't work fine, it's kind of hard to describe, but it doesn't seem that the view matrix is calculated as intended.

The examples only contain code with fixed up vectors, e.g. Vec3(0, 1, 0), but I really need a dynamic one.

Thanks in advance, Chris

 Cocos2d-x v3 C++ Tutorial 36 - Touch Began Using onTouchBegan - 學習筆記

資料來源: Cocos2d-x v3 C++ Tutorial 36 - Touch Began Using onTouchBegan

* 如何在 Cocos2d-x v3.6 C++ 收 onTouchBegan 事件
** 增加下列代碼在 cpp init 函數
auto listener = EventListenerTouchOneByOne::create();
listener->setSwallowTouches(true);
listener->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);

** 增加下列代碼(函數)在 cpp 檔
bool HelloWorld::onTouchBegan(cocos2d::Touch *touch, cocos2d::Event *event)
{
    CCLOG("onTouchBegan x = %f, y = %f", touch->getLocation().x, touch->getLocation().y);
    return true;
}

** 增加下列代碼(函數宣告) 在 header 檔
bool onTouchBegan(cocos2d::Touch *touch, cocos2d::Event *event);


Sunday, May 17, 2015

cocos2d android Java 游戲開發基礎 10 - 時間調度 學習視頻筆記

資料來源: 千鋒獨家出品《Android遊戲開發基礎視頻教程》http://www.mobiletrain.org/about/news/android_video3.html

* 為什麼需要時間調度
* schedule() 方法的使用
    this.schedule("func", 1)
// delta 往往用來表示增量,在當前的例子當中,該變量的值為上次調用 fun 函數與這次調用 fun 函數之間的時間差
public void fun(float delta) {
    System.out.println("調用的 fun 函數, delta 的值為 " + delta);
}

* delta 參數的作用
** delta 為上次和這次執行函數 func 的時間差
* unschedule() 方法的使用
    this.unschedule("func")
* 總結

Saturday, May 16, 2015

cocos2d android Java 游戲開發基礎 9 - 觸屏事件 學習視頻筆記

資料來源: 千鋒獨家出品《Android遊戲開發基礎視頻教程》http://www.mobiletrain.org/about/news/android_video3.html

* Android 平台觸摸事件回顧
* 設置圖層接收觸摸事件
** this.setIsTouchEnabled(true)
* 觸摸回調函數
** ccTouchesBegan(), ccTouchesMoved(), ccTouchesEnded()
* 觸摸點坐標轉換
** CCDirector.sharedDirector().convertToGL(CGPoint)
* 總結

cocos2d android Java 游戲開發基礎 8 - CCAction 初步 (四)學習視頻筆記

資料來源: 千鋒獨家出品《Android遊戲開發基礎視頻教程》http://www.mobiletrain.org/about/news/android_video3.html

* CCFadeIn 與 CCFadeOut
    CCFadeIn ccFadeIn = CCFadeIn.action(3);
    player.runAction(ccFadeIn);
    CCFadeOut ccFadeOut = CCFadeOut.action(3);
    player.runAction(ccFadeOut);

* CCTintTo 與 CCTintBy
    ccColor3B color3b = ccColor3B.ccc3(0, 0, 255);
    CCTintTo ccTintTo = CCTintTo.action(3, color3b);
    player.runAction(ccTintTo);

    ccColor3B color3b = ccColor3B.ccc3(-120, -255, -120);
    CCTintBy ccTintBy = CCTintBy.action(3, color3b);
    player.runAction(ccTintBy);

* CCRepeat
    CGPoint cgPoint = CGPoint.ccp(100, 300);
    CGPoint targetPoint = CGPoint.ccp(400, 300);
    CCMoveTo moveTo1 = CCMoveTo.action(2, targetPoint);
    CCMoveTo moveTo2 = CCMoveTo.action(2, cgPoint);
    CCSequence ccSequence = CCSequence.actions(moveTo1, moveTo2);

    CCRepeat ccRepeat = CCRepeat.action(ccSequence, 3);
    player.runAction(ccRepeat);

* CCRepeatForever
    CCRepeatForever ccRepeatForever = CCRepeatForever.action(ccSequence);
    player.runAction(ccRepeatForever);

Thursday, May 14, 2015

Priority inversion learning note

* Bounded priority inversion


* Unbounded priority inversion


How to solve
* Priority inheritance protocol
Raised the low priority task which locked shared resource to high priority to finish task and release shared resource soon.

* Priority ceiling protocol
** OCPP: Original Ceiling Priority Protocol
Raised the task (locked resource) priority to high priority when another task tries to acquire shared resource.

** ICPP: Immediate Ceiling Priority Protocol
Raised the task priority to high priority when it acquires resource.

Reference:
http://en.wikipedia.org/wiki/Priority_inheritance
http://en.wikipedia.org/wiki/Priority_ceiling_protocol
http://blog.linux.org.tw/~jserv/archives/001299.html
http://oss.org.cn/ossdocs/rt_embed/rt/priority.html
http://wen00072-blog.logdown.com/posts/183461-note-priority-inversion-on-mars
http://www.embedded.com/design/configurable-systems/4024970/How-to-use-priority-inheritance

cocos2d android Java 游戲開發基礎 7 - CCAction 初步 (三) 學習視頻筆記

資料來源: 千鋒獨家出品《Android遊戲開發基礎視頻教程》http://www.mobiletrain.org/about/news/android_video3.html

* CCSequence 的使用方法
** 一連串的動作依序執行
    CGPoint targetPoint = CGPoint.ccp(500, 500);
    CCMoveTo moveTo = CCMoveTo.action(5, targetPoint);
    CCRotateTo rotateTo = CCRotateTo.action(5, 180);
    CCScaleTo scaleTo = CCScaleTo.action(2, 2);
    CCSequence ccSequence = CCSequence.actions(moveTo, rotateTo, scaleTo);
    player.runAction(ccSequence);

* CCSpawn 的使用方法
** 一連串的動作同時間執行
    CCSpawn ccSpawn = CCSpawn.actions(moveTo, rotateTo, scaleTo);
    player.runAction(ccSpawn);

* CCCallfuncN 的使用方法
** 用於一個動作作完後所要執行的動作
    CCCallFuncN funcN = CCCallFuncN.action(this, "onActionFinished");
    player.runAction(funcN);

* CCFollow 的使用方法
    CCFollow ccFollow = CCFollow.action(followedNode, rect);
    player.runAction(ccFollow);
* 總結

cocos2d android Java 游戲開發基礎 6 - CCAction 初步 (二) 學習視頻筆記

資料來源: 千鋒獨家出品《Android遊戲開發基礎視頻教程》http://www.mobiletrain.org/about/news/android_video3.html


* 什麼是向量
* 基于向量的計算
** 向量的加減法
    CGPoint deltaPoint = CGPoint.ccp(0, 200);
    CGPoint targetPoint = CGPoint.ccpAdd(cgPoint, deltaPoint);
    CGPoint subPoint = CGPoint.ccp(200, 300);
    CGPoint tgPoint = CGPoint.ccpSub(targetPoint, subPoint);
** 向量的乘除法
    CGPoint newPoint = CGPoint.ccpMult(tgPoint, 2);
** 計算單位向量
* 使用 CGPoint 對象代表向量
    CGPoint anotherPoint = CGPoint.ccpNormalize(tgPoint);   
* 基于向量的運算
* CCMoveBy 與 CCJumpBy
    CGPoint onePoint = CGPoint.ccp(500, 300);
    CCMoveBy moveBy = CCMoveBy.action(5, onePoint);

    CGPoint jumpPoint = CGPoint.ccp(300, 100);
    CCJumpBy jumpBy = CCJumpBy.action(5, deltaPoint, 500, 5);
* 總結

Tuesday, May 12, 2015

cocos2d android Java 游戲開發基礎 5 - CCAction 初步 (一) 學習視頻筆記

資料來源: 千鋒獨家出品《Android遊戲開發基礎視頻教程》http://www.mobiletrain.org/about/news/android_video3.html

* 動作類的基本概念
** 動作類對象通常不會單獨存在
** 動作類對象需要作用在精靈,圖層等對象上才能發揮作用
** 動作類包含有很多種類型,例如位移,縮放和旋轉等

* 動作類的分類方法
** CCAction -- CCFiniteTimeAction -- CCActionInstant
**                 |                                   |- CCActionInterval
**                 |- CCFollow
**                 |- CCSpeed

* 基礎瞬時動作使用方法 - CCActionInstant
**   動作      描述
** CCFlipX  X軸鏡像翻轉
** CCFlipY  Y軸鏡像翻轉
** CCHide   隱藏
** CCShow   顯示

* 基礎延時動作使用方法 - CCActionInterval
**   動作        描述
** CCMoveTo    移動至目標
** CCRotateTo  旋轉至指定的角度
** CCScaleTo   縮放至指定的倍數
** CCBlink     閃爍

* 總結

cocos2d android Java 游戲開發基礎 4 - 精靈初體驗 學習視頻筆記

資料來源: 千鋒獨家出品《Android遊戲開發基礎視頻教程》http://www.mobiletrain.org/about/news/android_video3.html

* 什麼是精靈
** 精靈是游戲中的一個元素,通常用於代表畫面當中的一個事物,例如主人公,NPC 和背景元素等等
** 一個精靈對象通常都與一張圖片關聯
** 精靈對象通常可以通過運行動作對象 (CCAction) 來產生動畫效果

** 如何生成一個精靈對象
    CCSprite player;
    player = CCSprite.sprite("player.png");



** 讓精靈對象添加至布景層當中
    this.addChild(player);
** 運行動作對象 (CCAction)
    CGPoint target = CGPoint.ccp(400, 100); // 設定目標座標
    CCJumpTo jumpTo = CCJumpTo.action(3, target, 200, 3);  //設定 JumpTo(CCAction) 參數說明 (執行3秒, 跳到 target 座標, 跳動高度, 跳動次數)
    player.runAction(jumpTo);

** 總結

cocos2d android Java 遊戲開發基礎 3 - Cocos2d 創世紀 學習視頻筆記

資料來源: 千鋒獨家出品《Android遊戲開發基礎視頻教程》http://www.mobiletrain.org/about/news/android_video3.html

*創建第一個 Cocos2d 應用程序
** 創建一個 Android 應用程序
** 生成一個 SurfaceView 對象,作為 Activity 當中所顯示的內容
    view = new CCGLSurfaceView(this);
** 得到 CCDirector 對象,並通過該對象設置應用程序的各種屬性
    CCDirector director = CCDirector.sharedDirector();

** 設置應用程序基本屬性
*** 用於設置應用程序所使用的 GL 視圖對象
    director.attachInView(mGLSurfaceView)    
*** 用於設置應用程序的方向
    director.setDeviceOrientation(CCDirector.kCCDeviceOrientationLandscapeLeft);
*** 設置應用程序是否需要顯示應用程序的 FPS 值
    director.setDisplayFPS(true);
*** 設置每幀所需要的時間
    director.setAnimationInterval(1.0f / 30)

** 生成場景與布景層
*** 創建一個布景層類,繼承 CCLayer
    public class GameLayer extends CCLayer {
        public GameLayer() {
        }
    }
*** 調用 CCScene 類的 node() 方法生成場景對象
    CCScene scene = CCScene.node();
*** 生成布景層對象,並添加至場景對象當中
    GameLayer gameLayer = new GameLayer();
    scene.addChild(gameLayer);
*** 執行場景對象
    director.runWithScene(scene);

** 運行 Cocos2d 應用程序
** 總結

cocos2d android Java 遊戲開發基礎 2 - 安裝 學習視頻筆記

資料來源: 千鋒獨家出品《Android遊戲開發基礎視頻教程》http://www.mobiletrain.org/about/news/android_video3.html

抓包來安裝:到 github 找 cocos2d android, 抓 ZhouWeikuan 的 zip 檔下來套用到 Android 已存在項目.
修改 library 和把重覆的類改名後,即可編譯。

cocos2d android Java 遊戲開發基礎1 - 介紹 學習視頻筆記

資料來源: 千鋒獨家出品《Android遊戲開發基礎視頻教程》http://www.mobiletrain.org/about/news/android_video3.html

導演--場景1--布景層1 -- 精靈1
         |                   |                   |
         |                   |                   |-- 精靈2
         |                   |-- 布景層2   |-- 精靈3
         |-- 場景2       |-- 布景層3   |-- 精靈4
                                                  |
                                                  - 精靈5

CCDirector: 導演類
CCScene: 場景類
CCLayer: 布景層
CCSprite: 精靈