Skip to content

Instantly share code, notes, and snippets.

@Seasons7
Created December 29, 2013 17:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Seasons7/8172934 to your computer and use it in GitHub Desktop.
Save Seasons7/8172934 to your computer and use it in GitHub Desktop.
touchevent_coocs2djsb
require("jsb.js");
try {
director = cc.Director.getInstance();
winSize = director.getWinSize();
centerPos = cc.p(winSize.width / 2, winSize.height / 2);
var IconSprite = cc.Sprite.extend({
_rect: null,
_id: 0,
ctor: function(id) {
this._super();
this.init("Icon.png");
var rc = this.getTextureRect();
this._rect = cc.rect(0, 0, rc.width, rc.height);
this._id = id;
},
onEnter: function() {
cc.log("onEnter :IconSprite");
cc.registerTargettedDelegate(0, true, this);
this._super();
},
onExit: function() {
cc.log("onExit :IconSprite");
cc.unregisterTargettedDelegate(this);
this._super();
},
rect: function() {
return cc.rect(-this._rect.width / 2, -this._rect.height / 2,
this._rect.width,
this._rect.height);
},
containsTouchLocation: function(touch) {
var getPoint = touch.getLocation();
var myRect = this.rect();
myRect.x += this.getPosition().x;
myRect.y += this.getPosition().y;
return cc.rectContainsPoint(myRect, getPoint);
},
onTouchBegan: function(touch, event) {
if (this.containsTouchLocation(touch)) {
cc.log("sprite touch!!" + this._id);
return true; /* 下のオブジェクトにもタッチ処理を流すには、false */
// standard touchの対象にもなる
}
return false;
},
});
var GameLayer = cc.Layer.extend({
ctor: function() {
this._super();
var icon = new IconSprite(1);
this.addChild(icon);
icon.setPosition(cc.p(200, 200));
var icon2 = new IconSprite(2);
this.addChild(icon2);
icon2.setPosition(cc.p(180, 180));
// Label
var label = cc.LabelTTF.create("Hello cocos2d-x", "Arial", 20);
label.setPosition(cc.p(winSize.width / 2, winSize.height / 2));
this.addChild(label);
this.setTouchEnabled(true);
},
onTouchesBegan: function(touches, event) {
var c = touches.length;
var touch = touches[0];
var pos = touch.getLocation();
cc.log("touch!!" + "count[" + c + "]" + pos.x + "," + pos.y);
}
});
var game = new GameLayer();
__jsc__.garbageCollect();
// LOADING PLAY SCENE UNTILL CCBREADER IS FIXED
director.runWithScene(game);
} catch (e) {
log(e);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment