拼图游戏在今天下午也是完美结束,上午产生的 图碎片没锁定效果以及 拼图完毕却不能结束游戏的代码更正完毕。
下午同时也优化了拼图游戏的代码,以及拼图游戏中碎片的位置也在了它们该在的地方,下面是碎片精灵的类属性小段代码:
var Debris = cc.Sprite.extend({
//坐标
_targetPoint: null,
//是否可以拖动
reachTarget: null,
//回调方法
_callHandler: null,
//圆片 切割区域 以及移动的目标地点
ctor:function(texture, rect, targetPoint, callHandler){
this._super(texture, rect);
this._targetPoint = targetPoint;
this._callHandler = callHandler;
this._init();
this._addEvent();
},
/*
初始化
*/
_init:function(){
this.reachTarget = false;
this.anchorX = 0;
this.anchorY = 1;
this.x = 1600 + Math.random()*250;
this.y = 235 + Math.random()*500;
},
/*添加点击事件*/
_addEvent:function(){
var that = this;
var listener = cc.EventListener.create({
event:cc.EventListener.TOUCH_ONE_BY_ONE,
swallowTouches: true,
onTouchBegan:function(touch, event){
if(that.reachTarget) return false;
var target = event.getCurrentTarget();
var locationInNode = target.convertToNodeSpace(touch.getLocation());
var s = target.getContentSize();
var rect = cc.rect(0,0, s.width, s.height);
if(cc.rectContainsPoint(rect,locationInNode)){
target.setZOrder(10);
return true;
}
return false;
},
onTouchMoved:function(touch, event){
var target = event.getCurrentTarget();
var delta = touch.getDelta();
target.x += delta.x;
target.y += delta.y;
},
onTouchEnded:function(touch, event){
var target = event.getCurrentTarget();
if(cc.pDistance(target.getPosition(), that._targetPoint)<50){
target.setPosition(that._targetPoint);
that.reachTarget = true;
that._callHandler();
}
}
});
cc.eventManager.addListener(listener, this);
}
});
终于结束了一周的忙碌能休息一会了~