futabooo blog

色々手をつけすぎてすぐに忘れるので備忘録

cocos2d-xハンズオンを受けたのでメモ

頭にCCがつくのが最初気持ち悪かった。
iOSの開発やってる人は馴染みやすそう。
クロスプラットフォーム開発すげぇ!
Unityよりも固めたアプリが軽量。


// シングルタップでは必ずoverride
virtual bool ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent);

// cocosのタッチポイントをopenGLのタップポイントにする。touchPointは構造体(x, y)
CCPoint touchPoint = CCDirector::sharedDirector()->convertToGL(pTouch->getLocationInView());

// 指定された四角形の中にタッチポイントが含まれているかどうかをtruefalseで返す
node->boundingBox().containsPoint(

// フォントの指定で空にするとシステムフォント
CCLabelTTF* timerLabel = CCLabelTTF::create("0.000s", "", 32.0f);touchPoint)

// CCDirectorはシングルトンになっている。画面を統括する唯一のクラス。sharedDirectorでインスタンスを取得してgetWinSizeで画面サイズ取得
CCSize bgSize = CCDirector::sharedDirector()->getWinSize();

// scheduleを使うと毎フレーム呼び出す
this->schedule(schedule_selector(HelloWorld::countTimer));

// getChildByTagの返り値がnodeなのでキャストする
CCLabelTTF* timerLabel = (CCLabelTTF*)this->getChildByTag(kTagTimerLabel);

// flush()までやってsetしたものが次回使えるようになる
CCUserDefault::sharedUserDefault()->setFloatForKey("highscore", gameTime);
CCUserDefault::sharedUserDefault()->flush();

// ボタンが10こあったら最後にNULLを入れる
CCMenu* menu = CCMenu::create(retryLabel, NULL);

Unity4.3のプロジェクトをcodebreakで管理する

Unityのプロジェクトを複数人で共有する必要が出てきたので、
無料でプライベートリポジトリが作れるcodebreak;を使ってみました。
Free Git Hosting | codebreak;


とりあえず一人で2PCでやってみたらできたので備忘録。

1.Unityプロジェクト側の設定

Edit>Project Settings>Editor
Version Control>ModeをVisible Meta Filesへ変更する

f:id:futabooo:20131208200841j:plain

2.codebreak;のリポジトリへコミット

事前にcodebreak;上でリポジトリを作成しておく。


Unityのプロジェクト内にあるLibraryフォルダを丸ごと削除する(必要ないかも)
f:id:futabooo:20131208201158p:plain

プロジェクトのフォルダ内に移動して下記コマンド

$git init
$git add Assets
$git add ProjectSettings
$git commit -m "first commit"
$git remote add origin [リモートリポジトリ]
$git push -u origin master

これでcodebreak;上にリモートリポジトリ完成。

3.ローカルにクローン

$git clone [リモートリポジトリ]

プロジェクトがローカルにコピーされる。
Unityでプロジェクトを開く。
なんか適当に変更加える。

4.変更内容をリモートリポジトリへコミット

変更内容の確認をしてみる。
今回はマテリアルの色を変更してみただけなので、下記のファイルが変更されていた。

$git status
$On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified: Assets/FloorMaterial.mat
        modified: Assets/Main.unity

untracked files:
  (use "git add <file>..." to include in what will be committed)

        Library/
no changes added to commit(use "git add" and/or "git commit -a")
$git add Assets/FloorMaterial.mat
$git add Assets/Main.unity
$git commit -m "floormaterial color changed"
$git push origin master

4.ローカルにリモートリポジトリの変更を取り込む

$git pull

以上!

SurfaceViewのsurfaceCreated()が呼ばれるタイミング

ライフサイクルとかそこら辺とのかねあいの話ではないです。

結論
XMLで下の2つの状態でsetContentViewした時、
1つ目の場合だとsetContentViewのタイミングでsurfaceCreated()が呼ばれる。
2つ目の場合だとsetVisibility(View.VISIBLE)したタイミングでsurfaceCreated()が呼ばれる。

android:visibility="visible"
android:visibility="invisible"

AndroidのOSバージョンを表す定数たち

OSのバージョンごとに処理を分けたい時とかに使える。

3 = Build.VERSION_CODES.CUPCAKE
4 = Build.VERSION_CODES.DONUT
5 = Build.VERSION_CODES.ECLAIR
6 = Build.VERSION_CODES.ECLAIR_0_1
7 = Build.VERSION_CODES.ECLAIR_MR1
8 = Build.VERSION_CODES.FROYO
9 = Build.VERSION_CODES.GINGERBREAD
10 = Build.VERSION_CODES.GINGERBREAD_MR1
11 = Build.VERSION_CODES.HONEYCOMB
12 = Build.VERSION_CODES.HONEYCOMB_MR1
13 = Build.VERSION_CODES.HONEYCOMB_MR2
14 = Build.VERSION_CODES.ICE_CREAM_SANDWICH
15 = Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1
16 = Build.VERSION_CODES.JELLY_BEAN
17 = Build.VERSION_CODES.JELLY_BEAN_MR1
18 = Build.VERSION_CODES.JELLY_BEAN_MR2
19 = Build.VERSION_CODES.KITKAT



参考
http://developer.android.com/reference/android/os/Build.VERSION_CODES.html

Androidでカスタムdialog

背景画像がデフォのアイコン画像なのでださいけども。
こんなかんじのが最終的にできます。
f:id:futabooo:20131108014918p:plain


MainActivity

public class MainActivity extends Activity implements View.OnClickListener {

    private Context context;
    private Button button;
    private Dialog dialog;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this;
        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(this);
    }

    @Override
    protected void onDestroy() {
        if (dialog != null) {
            dialog.dismiss();
            button.setEnabled(true);
        }
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        v.setEnabled(false);
        AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {

            @Override
            protected void onPreExecute() {
                dialog = new Dialog(context, R.style.AppBaseTheme_CustomDialog);
                dialog.setContentView(R.layout.custom_dialog);
                dialog.setCancelable(false);
                dialog.getWindow().setBackgroundDrawable(getResources().getDrawable(R.drawable.ic_launcher));
                dialog.getWindow().getAttributes().width = ViewGroup.LayoutParams.FILL_PARENT;
                dialog.getWindow().getAttributes().height = ViewGroup.LayoutParams.FILL_PARENT;
                dialog.show();
            }

            @Override
            protected Void doInBackground(Void... arg0) {
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return null;
            }

            @Override
            protected void onPostExecute(Void result) {
                if (dialog != null) {
                    dialog.dismiss();
                    button.setEnabled(true);
                }
            }

        };
        task.execute((Void[]) null);
    }


main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:gravity="center">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="show dialog" />
</LinearLayout>


theme_custom_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="AppBaseTheme.CustomDialog" parent="android:Theme.DeviceDefault.Dialog">

        <item name="android:windowNoTitle">true</item>
        <item name="android:windowBackground">@android:color/transparent</item>
    </style>

</resources>


custom_dialog.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ProgressBar
        android:layout_centerInParent="true"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content" />

</RelativeLayout>


引数にActivityを渡す方法

MainActivityという名前のActivityクラスを渡す場合。

まずはNG

AsyncHttpRequest asyncHttpRequest = new AsyncHttpRequest(this);

こっちが正解

AsyncHttpRequest asyncHttpRequest = new AsyncHttpRequest(MainActivity.this);



AndroidStudio0.3.0で「Local path doesn't exist」

AndroidStudioがなんかちょっと大きめのアップデートしたみたいで、
0.2から0.3へなっていました。

なんも考えずにアップデートしたらrunできなくなりました\(^o^)/

Uploading file
    local path:/Users/futabooo/AndroidStudioProjects/CustomDialogProject/CustomDialog/build/classes/debug/CustomDialog.apk
    remote path: /data/local/tmp/com.futabooo.customdialog
Local path doesn't exist.

どうやらapkがそんなとこには無いらしい。
ぐぐ立ったら先人がいました。

Android Studio – Local path doesn’t exist


僕の場合はここにあった

/Users/futabooo/AndroidStudioProjects/CustomDialogProject/CustomDialog/build/apk/CustomDi
alog-debug-unaligned.apk

このapkをdebugに使うことをAndroidStudioは望んでいない??みたいに参考記事にはありました。
とはいえデバッグしたいので、下記ファイルにオプションを追加します。

 /Users/futabooo/AndroidStudioProjects/CustomDialogProject/CustomDialog/CustomDialog.iml
<option name="APK_PATH" value="/build/apk/CustomDialog-debug-unaligned.apk" />


runできた\(^o^)/