2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

cocos2d-jsのタッチイベントハンドラで苦労した結果

2
Last updated at Posted at 2015-04-07

サンプルの js-moonwarriors を元にというかそのままな感じで onTouchesMoved を onTouchesBegan へ置き換えただけの処理をしていたところ Android では動くが iOS では動かないという現象に遭遇。
サンプルでは touchId を操作していることが理由だったわけでほぼ自爆した感じなのだが、せっかくなので挙動確認結果のメモを残す。

公式サイトの記述を読みながら次のように書き方を整理して対応。
id の操作は必要に応じていずれ行うことにする。

var GameMainLayer = cc.Layer.extend({
	ctor: function(){
		this._super();
	},
	init: function () {
		if (cc.sys.capabilities.hasOwnProperty('touches')) {
			cc.eventManager.addListener({
				event: cc.EventListener.TOUCH_ALL_AT_ONCE,
				swallowTouches: true,
				onTouchesBegan: this.onTouchesBegan,
				onTouchesMoved: null,
				onTouchesEnded: null
			}, this)
		}
	},
	onTouchesBegan: function(touches, event) {
		// to do here
		return true;
	}
});

サンプルの記述がちょっと唐突な感じなのと(Began の function がないと id 操作の意味がわからない)、ネットに散在する情報が古めだったりで把握に手間取ったが cocos2d-js v3.5 の時点では次のように考えておけばよさそう。

  • マルチタッチを拾うのであればイベントのタイプを cc.EventListener.TOUCH_ALL_AT_ONCE にしてコールバック群は「onTouches?????」を使用。(複数形)
  • シングルでよいのであればイベントのタイプを cc.EventListener.TOUCH_ONE_BY_ONE にしてコールバック群は「onTouch????」を使用。(単数形)

このコールバック時のメソッド名とイベントタイプの関係に最初気づいていなかったため勝手に苦労してしまった。

2
2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?