From fa06296616595159d5a1ea91a6d68e7bbe70b526 Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Thu, 5 May 2016 15:29:44 +0800 Subject: [PATCH 1/2] fix button target spriteframe change issue disable button event when button is disabled improve event register code refine code --- cocos2d/core/components/CCButton.js | 20 ++++++++++++++++++++ cocos2d/core/components/CCSprite.js | 6 ++++++ 2 files changed, 26 insertions(+) diff --git a/cocos2d/core/components/CCButton.js b/cocos2d/core/components/CCButton.js index fe5028196f..767b712188 100644 --- a/cocos2d/core/components/CCButton.js +++ b/cocos2d/core/components/CCButton.js @@ -314,9 +314,17 @@ var Button = cc.Class({ if (!this.target) { this.target = this.node; } + }, + onEnable: function () { if (!CC_EDITOR) { this._registerEvent(); + } else { + this.node.on('spriteframe-changed', function(event) { + if (this.transition === Transition.SPRITE) { + this.normalSprite = event.detail.spriteFrame; + } + }.bind(this)); } }, @@ -446,6 +454,18 @@ var Button = cc.Class({ onDisable: function() { this._hovered = false; this._pressed = false; + + if (!CC_EDITOR) { + this.node.off(cc.Node.EventType.TOUCH_START); + this.node.off(cc.Node.EventType.TOUCH_MOVE); + this.node.off(cc.Node.EventType.TOUCH_END); + this.node.off(cc.Node.EventType.TOUCH_CANCEL); + + this.node.off(cc.Node.EventType.MOUSE_ENTER); + this.node.off(cc.Node.EventType.MOUSE_LEAVE); + } else { + this.node.off('spriteframe-changed'); + } }, _applyTransition: function (color, sprite) { diff --git a/cocos2d/core/components/CCSprite.js b/cocos2d/core/components/CCSprite.js index a67c1a6667..7be4f437ae 100644 --- a/cocos2d/core/components/CCSprite.js +++ b/cocos2d/core/components/CCSprite.js @@ -161,9 +161,15 @@ var Sprite = cc.Class({ return this._spriteFrame; }, set: function (value, force) { + if (this._spriteFrame === value) { + return; + } var lastSprite = this._spriteFrame; this._spriteFrame = value; this._applySpriteFrame(lastSprite); + if (CC_EDITOR) { + this.node.emit('spriteframe-changed', this); + } }, type: cc.SpriteFrame, }, From 831187ec4bb667afcbc0f1c0460d60bf832df9bf Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Thu, 5 May 2016 17:24:15 +0800 Subject: [PATCH 2/2] improve node event --- cocos2d/core/components/CCButton.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/cocos2d/core/components/CCButton.js b/cocos2d/core/components/CCButton.js index 767b712188..cb0137f80e 100644 --- a/cocos2d/core/components/CCButton.js +++ b/cocos2d/core/components/CCButton.js @@ -456,13 +456,13 @@ var Button = cc.Class({ this._pressed = false; if (!CC_EDITOR) { - this.node.off(cc.Node.EventType.TOUCH_START); - this.node.off(cc.Node.EventType.TOUCH_MOVE); - this.node.off(cc.Node.EventType.TOUCH_END); - this.node.off(cc.Node.EventType.TOUCH_CANCEL); + this.node.off(cc.Node.EventType.TOUCH_START, this._onTouchBegan, this); + this.node.off(cc.Node.EventType.TOUCH_MOVE, this._onTouchMove, this); + this.node.off(cc.Node.EventType.TOUCH_END, this._onTouchEnded, this); + this.node.off(cc.Node.EventType.TOUCH_CANCEL, this._onTouchCancel, this); - this.node.off(cc.Node.EventType.MOUSE_ENTER); - this.node.off(cc.Node.EventType.MOUSE_LEAVE); + this.node.off(cc.Node.EventType.MOUSE_ENTER, this._onMouseMoveIn, this); + this.node.off(cc.Node.EventType.MOUSE_LEAVE, this._onMouseMoveOut, this); } else { this.node.off('spriteframe-changed'); }