[Cocos2d-x] 在 Mac 上設置 Cocos2d-x (Cocos2d-x 3 .x)

環境 OSX 10.9 , Cocos2d-x 3.2

下載Cocos2d-x http://cocos2d-x.org/download

JAVA http://www.oracle.com/technetwork/java/javase/downloads/index.html
安裝 AndroidSDK+ADT(android) http://developer.android.com/sdk/index.html
SDK要用2.3.3 其它會報錯

NDK http://developer.android.com/tools/sdk/ndk/index.html

安裝 Ant
終端機
$ ruby -e “$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"
$ brew update
$ brew install ant

設置cocos2d-x
終端機
到cocos2d-x 解開的目錄下
$ python setup.py
輸入android sdk, ndk, ant的目錄
$ source /Users/[osx user name]/.bash_profile

建立Project

$ cocos new MyGame -p com.your_company.mygame -l cpp -d NEW_PROJECTS_DIR
$ cd NEW_PROJECTS_DIR/MyGame

就可以去目錄下開 xcode 專案來 run 了

其它參考指令
Build and run new project for android
$ cocos run -p android -j 4

Build and run new project for iOS
$ cocos run -p ios
Build and run new project for OSX
$ cocos run -p mac
Build and run new project for linux
if you never run cocos2d-x on linux, you need to install all dependences by the script in cocos2d/build/install-deps-linux.sh
$ cd cocos2d-x/build
$ ./install-deps-linux.sh
Then
$ cd NEW_PROJECTS_DIR/MyGame
$ cocos run -p linux
Run
$ bin/MyGame

Build and run new project for win32
$ cocos run -p win32

[Unity] Assetbundle Shader 遺失的解決方案

物件打包成 AssetBundle 後有的物件上的材質的shader 會 lost 變一塊紫色

//assetbundle後shader會lost的處理
gameObject.renderer.material.shader =  Shader.Find(gameObject.renderer.material.shader.name);
或是
    using UnityEngine;
    using System.Collections;
    public class ApplyShader : MonoBehaviour
    {
        private Material[] thisMaterial;
        private string[] shaders;
        void Start () {
            thisMaterial = this.renderer.sharedMaterials;             
            shaders =  new string[thisMaterial.Length];
            for( int i = 0; i < thisMaterial.Length; i++){
                shaders[i] = thisMaterial[i].shader.name;
            }    

            for( int i = 0; i < thisMaterial.Length; i++){
                thisMaterial[i].shader = Shader.Find(shaders[i]);  
            }
        }
    }

[Unity] Assetbundle Shader 遺失的解決方案

物件打包成 AssetBundle 後有的物件上的材質的shader 會 lost 變一塊紫色

//assetbundle後shader會lost的處理
gameObject.renderer.material.shader =  Shader.Find(gameObject.renderer.material.shader.name);
或是
    using UnityEngine;
    using System.Collections;
    public class ApplyShader : MonoBehaviour
    {
        private Material[] thisMaterial;
        private string[] shaders;
        void Start () {
            thisMaterial = this.renderer.sharedMaterials;             
            shaders =  new string[thisMaterial.Length];
            for( int i = 0; i < thisMaterial.Length; i++){
                shaders[i] = thisMaterial[i].shader.name;
            }    

            for( int i = 0; i < thisMaterial.Length; i++){
                thisMaterial[i].shader = Shader.Find(shaders[i]);  
            }
        }
    }

[MAC] 在 MAC 上安裝 nginx

安裝 Homebrew

ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"
安裝 nginx
brew install nginx
讓 nginx 在登入時自動載入
ln -sfv /usr/local/opt/nginx/*.plist ~/Library/LaunchAgents
立刻載入
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist
sudo nginx
網頁根目錄
/usr/local/var/www
nginx 的設定檔
/usr/local/etc/nginx/nginx.conf
關掉 apache
sudo apachectl stop

[iOS開發] UIImageView 加點擊範例

簡單的讓 UIImageView 變成 ImageButton imgBtn.png 放在 Resources 資料夾

//生成 UIImage 並指定圖片
UIImage *image = [UIImage imageNamed:@"imgBtn.png"];
UIImageView *iv = [[UIImageView alloc] initWithImage:image];
//指定位置 x,y,width,height
iv.frame = CGRectMake(10, 10, image.size.width, image.size.height);

//讓 UIImage 能接受使用者事件
iv.userInteractionEnabled = YES;

//UITapGestureRecognizer 是觸控控制類
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapDetected)];
singleTap.numberOfTapsRequired = 1;
[iv addGestureRecognizer:singleTap];

[self.view addSubview:iv];

-(void)tapDetected{
  NSLog(@"Click!!");
}

[Android] 開啟另一個Activity

開啟另一個Activity ( A 呼 B

Intent intent = new Intent();
intent.setClass(A.this, B.class);
startActivity(intent);
不同 Activity 之間傳值 A.class
Intent intent = new Intent();
intent.setClass(this, Report.class);
Bundle bundle = new Bundle();
bundle.putString("VAR_A", field_height.getText().toString());
bundle.putString("VAR_B", field_weight.getText().toString());
intent.putExtras(bundle);
startActivity(intent);
B.class
Bundle bundle = this.getIntent().getExtras();
double a= Double.parseDouble(bundle.getString("VAR_A"));
double b= Double.parseDouble(bundle.getString("VAR_B"));

回傳資料給前一個 Activity 並回復其狀態
A.class

//呼叫B
startActivityForResult(intent);
//接受B的回傳值
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
             Bundle bundleResult = data.getExtras();
        }
}
B.class
//回傳bundle 給前一個 activity
B.this.setResult(RESULT_OK, intent);
B.this.finish();

不重複啟動相同的Activity
Manifest.xml配置成android:launchMode=”singleTask”

[Cocos2D] 在文字旁加上黑邊 CCLabelTTF and CCSprite

 static CCRenderTexture* createStroke(CCSprite* label, int size, ccColor3B color, GLubyte opacity)
 {

   CCRenderTexture* rt = CCRenderTexture::create(label->getTexture()->getContentSize().width + size * 2,
       label->getTexture()->getContentSize().height+size * 2);
   CCPoint originalPos = label->getPosition();

   ccColor3B originalColor = label->getColor();

   GLubyte originalOpacity = label->getOpacity();

   label->setColor(color);

   label->setOpacity(opacity);

   bool originalVisibility = label->isVisible();

   ccBlendFunc originalBlend = label->getBlendFunc();

   ccBlendFunc bf = {GL_SRC_ALPHA, GL_ONE};

   label->setBlendFunc(bf);

   CCPoint bottomLeft = ccp(
       label->getTexture()->getContentSize().width * label->getAnchorPoint().x + size,
       label->getTexture()->getContentSize().height * label->getAnchorPoint().y + size);

 //原來沒有判斷AnchorPoint的寫法
 //  CCPoint positionOffset= ccp(   - label->getTexture()->getContentSize().width / 2,
 //                                  - label->getTexture()->getContentSize().height / 2);

 //SetAnchorPoint會影響到positionOffset,所以要做判斷
   CCPoint positionOffset = CCPointZero;
   if(label->getAnchorPoint().x == 0.5f){
       positionOffset.x = 0;
   }else if(label->getAnchorPoint().x == 0.0f){
       positionOffset.x =- label->getTexture()->getContentSize().width / 2;
   }else{
       positionOffset.x = label->getTexture()->getContentSize().width/2;
   }
   if(label->getAnchorPoint().y == 0.5f){
       positionOffset.y = 0;
   }else if(label->getAnchorPoint().y == 0.0f){
       positionOffset.y =- label->getTexture()->getContentSize().height / 2;
   }else{
       positionOffset.y =  label->getTexture()->getContentSize().height/2;
   }

   CCPoint position = ccpSub(originalPos, positionOffset);

   rt->begin();

   for (int i=0; i<360 data-blogger-escaped-for="" data-blogger-escaped-i="" data-blogger-escaped-label-="" data-blogger-escaped-needs="" data-blogger-escaped-optimize="" data-blogger-escaped-should="" data-blogger-escaped-that="" data-blogger-escaped-you="" data-blogger-escaped-your="">setPosition(
       ccp(bottomLeft.x + sin(CC_DEGREES_TO_RADIANS(i))*size, bottomLeft.y + cos(CC_DEGREES_TO_RADIANS(i))*size)
       );
       label->visit();
   }
   rt->end();

   label->setPosition(originalPos);
   label->setColor(originalColor);
   label->setBlendFunc(originalBlend);
   label->setVisible(originalVisibility);
   label->setOpacity(originalOpacity);

   rt->setPosition(position);
 //反踞齒
   rt->getSprite()->getTexture()->setAntiAliasTexParameters();

   return rt;
}

[C#] 一次一行讀取文字檔

這個範例會使用 StreamReader 類別的 ReadLine 方法,將文字檔的內容一次一行讀入字串中。每一個文字行都會儲存到字串 line 中並顯示在螢幕上。

範例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int counter = 0;
string line;

// Read the file and display it line by line.
System.IO.StreamReader file =
new System.IO.StreamReader("c:\test.txt");
while((line = file.ReadLine()) != null)
{
Console.WriteLine (line);
counter++;
}

file.Close();

// Suspend the screen.
Console.ReadLine();