Tuesday, May 12, 2015

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: 精靈

Monday, December 29, 2014

使用 xmllint 處理/驗證 xml



xmllint是一個很方便的處理及驗證xml的工具,linux下只要安裝libxml2就可以使用這個命令,下面整理一些常用功能
1. --format
此參數用於格式化xml,使其具有良好的可讀性。
假設有xml(person.xml)內容如下:

<person><name>ball</name><age>30</age><sex>male</sex></person>

執行:
xmllint --format person.xml  
得到易讀的xml

    <person>  
          <name>ball</name>  
          <age>30</age>  
          <sex>male</sex>  
    </person>
    2. --noblanks
    與--format相反,有時為了節省傳輸量,我們希望去掉xml中的空白,這時我們可以使用--noblanks命令。
    假設xml(person.xml)內容如下

    <?xml version="1.0"?>  
    <person>  
      <name>ball</name>  
      <age>30</age>  
      <sex>male</sex>  
    </person>
    
      執行:
      xmllint --noblanks person.xml  
      得到去掉空白後的xml

      ball30male
      



      3.--schema
      使用scheam驗證xml文件的正確性(瞭解schema的知識請猛擊這裡
      假設有xml文件(person.xml)和scheam文件(person.xsd)文件,內容分別如下
      person.xml

      <?xml version="1.0"?>  
      <person>  
        <name>ball</name>  
        <age>30</age>  
        <sex>male</sex> 
      </person>
      

      person.xsd

      <?xml version="1.0"?>  
      <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">  
        <xs:element name="name" type="xs:string"/>  
        <xs:element name="age" type="xs:integer"/>  
        <xs:element name="sex">  
          <xs:simpleType>  
            <xs:restriction base="xs:string">  
              <xs:enumeration value="male"/>  
              <xs:enumeration value="female"/>  
            </xs:restriction>  
          </xs:simpleType>  
        </xs:element>  
        <xs:element name="person">  
          <xs:complexType>  
            <xs:all>  
              <xs:element ref="name"/>  
              <xs:element ref="age"/>  
              <xs:element ref="sex"/>  
            </xs:all>  
          </xs:complexType>  
        </xs:element>  
      </xs:schema>
      
      執行:

      xmllint --schema person.xsd person.xml  
      得到

        
        ball  
        30  
        male  
        
      person.xml validates 
      
      注意,默認情況下,驗證後會輸出驗證的文件內容,可以使用 --noout選項去掉此輸出,這樣我們可以只得到最後的驗證結果。
      執行

      xmllint --noout --schema person.xsd person.xml   
      得到

      person.xml validates  

      下面我們改動person.xml
      <?xml version="1.0"?>  
      <person>  
        <name>ball</name>  
        <age>not age</age>  
        <sex>test</sex>  
      </person>
      


      顯然,這份文件age字段和sex都是不符合xsd定義的。
      執行

      xmllint --noout --schema person.xsd person.xml  
      得到:

      person.xml:4: element age: Schemas validity error : Element 'age': 'not age' is not a valid value of the atomic type 'xs:integer'.  
      person.xml:5: element sex: Schemas validity error : Element 'sex': [facet 'enumeration'] The value 'test' is not an element of the set {'male', 'female'}.  
      person.xml:5: element sex: Schemas validity error : Element 'sex': 'test' is not a valid value of the local atomic type.  
      person.xml fails to validate 
      
      good!xmllint成功的報出了錯誤!

      4. 關於--schema的輸出
      在講輸出之前先看下面一個場景,假如你想通過php執行xmllint然後拿到返回結果,你的代碼通常應該是這個樣子
      valid.php
      <?php  
      $command = "xmllint --noout --schema person.xsd person.xml";  
      exec($command, $output, $retval);  
      //出錯時返回值不為0  
      if ($retval != 0){   
              var_dump($output);      
      }  
      else{  
          echo "yeah!";  
      }
      
      我們保持上文中person.xml的錯誤。
      執行此代碼,你會發現,你拿到的output不是錯誤,而是array(0) {}, amazing!
      為什麼會這樣呢?
      因為xmllint --schema,如果驗證出錯誤,錯誤信息並不是通過標準輸出(stdout)顯示的,而是通過標準錯誤(stderr)進行顯示的。
      而exec的output參數拿到的,只能是標準輸出(stdout)顯示的內容。
      所以,為了拿到出錯信息,我們需要將標準錯誤重定向到標準輸出,對應修改代碼:
      $command = "xmllint --noout --schema person.xsd person.xml 2>&1";  

      再次執行valid.php,錯誤信息順利拿到!
      附小問題
      --noout參數關閉的是什麼輸出呢:)