Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

4.3.3 タッチに合わせてキャラクターを動かす #12

Open
niku opened this issue Feb 16, 2015 · 0 comments
Open

4.3.3 タッチに合わせてキャラクターを動かす #12

niku opened this issue Feb 16, 2015 · 0 comments

Comments

@niku
Copy link
Owner

niku commented Feb 16, 2015

Position に関連した処理

cocos2d-js の場合 Position オブジェクト にはメソッドがない.
そもそも Position オブジェクトの説明に new cc.Position (コンストラクタ) を使わずに cc.p メソッドを使ってと書いてあったことに気づかなかった.今までのコードも直す.

Position を足すには cc.pAdd を使い,
最小/最大を制限した Position を得るにはcc.pClamp を使う.

コールバックの中での this

コールバックの中での this は,そのままだと直感的に考えているような this にならない.
例えば今回の場合でいうと onTouchMoved の this がMainSceneLayer を指していると考えてしまうだろう.しかし,そうはならない.
JavaScript でよくあるハマりどころである.

これを回避するために関数に .bind(this) を追加して(この場合の this は MainSceneLayer を指している)
コールバックの中での this も MainSceneLayer を指すようにする.
イディオム(慣用表現)として,コールバックには全て .bind をつけてしまってかまわない.

niku added a commit that referenced this issue Feb 16, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant