0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

UnityエンジニアがCocos Creatorを1年触って引っかかったところ

Last updated at Posted at 2025-07-21

本職はUnityエンジニアで、ここ1年くらいCocosを触っています。
その中で感じた違いをつらつらと書いていこうと思います。

1. Nodeのontouchイベント伝播

  • 子Node(UnityでいうGameObject的なもの)でタッチイベントを受け取っても、親Nodeにもイベントが伝わる(イベントバブリング)
  • 意図しない親Nodeのイベント処理が起きることがある
  • 対策:event.stopPropagation()で伝播を止める

UnityのUIイベントはデフォルトで親に伝播しない(EventSystemで明示的に制御)。Cocosではイベントバブリングがデフォルトで発生する点が違う。

node.on('touchstart', (event) => {
  // 子で処理
  event.stopPropagation(); // ここで親への伝播を止める
});

2. TypeScriptのundefinedと==・===の違い

  • undefinedは値が「未定義」の状態
  • ==は型を変換して比較
  • ===は型も値も厳密に比較
  • 例:
let a; // undefined
console.log(a == null);  // true(nullとundefinedは==で等しい)
console.log(a === null); // false

参考までに==で比較可能なのは以下の通りです。
参考 https://ui.dev/javascript-double-equals-vs-triple-equals
image.png

3. Collider2Dだけだと当たり判定取れない

オブジェクト移Collider2D + Rigidbody2Dをつける必要あり

4. Vector系オブジェクトは参照渡しなので注意

Unity(C#)ではVector3などは構造体(値型)なのでコピーされるが、Cocosでは参照型。
間違えて変更すると他の箇所にも影響が出る

例:

let v1 = cc.v2(10, 20);
let v2 = v1; // 参照をコピーしただけ
v2.x = 30;
console.log(v1.x); // 30になる!

コピーしたい場合はv1.clone()を使う

let v2 = v1.clone();
v2.x = 40;
console.log(v1.x); // 30のまま変更されない

5.ブレークポイントを付けてデバッグすることはできない

毎回、console.logを打って確認している

Cocos Creatorのエディタ上では統合デバッグが未対応のことが多く、特にブラウザ実行時のブレークポイント設定は困難。

6. out はTypeScriptには存在しない

C#では以下のように関数で複数の値を返すために out パラメータを使える

bool GetValues(out int a, out int b) {
    a = 10;
    b = 20;
    return true;

TypeScriptには out や ref のような機能はなく、関数の戻り値でまとめて返す必要があった
↓こんな感じ

function getValues(): { a: number, b: number, suceess: boolean} {
    return { a: 10, b: 20, suceess:true };
}

const { a, b, suceess } = getValues();

※ちなみに、typescriptのコーディング規約では変数と関数名には camelCaseを使うのでここも違う…

まとめ

  • Unityと似ているので始めやすい
  • 日本語の記事はほぼない
  • まわりまわってUnityの勉強にもなる(比較文化的アプローチ)
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?