1.添加图片背景
var bg = new cc.Sprite("res/res/bg2.jpg");
this.addChild(bg);
bg.setAnchorPoint(0,0);
bg.setPosition(cc.winSize.width*0.2,0);
2.添加敌方小怪地板
for(var i = 0; i<GAME_CONFIG.SHRED_NUM_W; i++) {
for(var j = 0; j<GAME_CONFIG.SHRED_NUM_H; j++){
var sp = new CellSprite();
this.addChild(sp);
sp.setAnchorPoint(0.5,0.5);
sp.setPosition(i*sp.width*0.7+300,320-j*sp.height*0.7);
cc.eventManager.dispatchCustomEvent(GAME_EVENT.MAKE_ENEMY_PLAYER,sp.getPosition());
//cc.log("抛出了事件")
cc.log(sp.x,sp.y)
}
}
3.添加敌方管理层,接受地板生成时传来的坐标生成敌方小怪
_init: function () {
this._listener = cc.eventManager.addCustomListener(GAME_EVENT.MAKE_ENEMY_PLAYER,this._makeEnemy.bind(this))
//cc.log(123,"添加了侦听器")
},
_makeEnemy:function(event){
var po =event.getUserData();
//cc.log(123);
//cc.log(po.x,po.y);
var enemy = new EnemySprite();
this.addChild(enemy);
enemy.setPosition(po);
},
4.在敌方小怪类里面封装属性和点击碰撞检测
cc.spriteFrameCache.addSpriteFrames("res/res/kuaiqiangshoudafeiji.plist");
this.type = parseInt(Math.random() * 4);
this.initWithSpriteFrame(cc.spriteFrameCache.getSpriteFrame(GAME_ENEMY.LGX[this.type] + ".png"));
this.scale = 2/3;
var that = this;
this.schedule(function () {
Math.random()>0.4?that.visible = true:that.visible = false;
that.type = parseInt(Math.random() * 4);
that.initWithSpriteFrame(cc.spriteFrameCache.getSpriteFrame(GAME_ENEMY.LGX[that.type] + ".png"));
},GAME_CONFIG.ENEMY_SPEED);
var listener = cc.EventListener.create({
event:cc.EventListener.TOUCH_ONE_BY_ONE,
swallowTouches:true, //吞没事件
onTouchBegan: function (touch, event) {
var target = event.getCurrentTarget(); //当前点击的对象
var pos = touch.getLocation(); //获取当前全局点击点坐标
var s = target.getContentSize(); //获取点击对象的尺寸
var rect = cc.rect(0, 0, s.width, s.height); //对象的框型区域
var localPoint = target.convertToNodeSpace(pos); //转换为本地坐标
if (cc.rectContainsPoint(rect, localPoint)) { //检测碰撞
if(that.visible){
that.visible = false;
target.initWithSpriteFrame(cc.spriteFrameCache.getSpriteFrame(GAME_ENEMY.LGX[that.type] + ".png"));
if(that.type == 0){
cc.eventManager.dispatchCustomEvent("SCORE",100)
}else{
cc.eventManager.dispatchCustomEvent("SCORE",-50)
}
}
return true
} else {
return false
}
}
});
this._listener = cc.eventManager.addListener(listener,this)
5.接受怪被消灭时抛出的时间获得分数
this._listener = cc.eventManager.addCustomListener("SCORE",this._setScore.bind(this))
},
_setScore:function(event){
var data = event.getUserData();
if(this._madeScore == 0&&data<this._madeScore) return;
this._madeScore += data;
this._labelScore.setString(this._madeScore);
},
6.消灭怪物时添加特效
this.setSpriteFrame(cc.spriteFrameCache.getSpriteFrame("100.png"));
this.setPosition(po);
var move = cc.moveBy(0.1,cc.p(0,50));
var fade = cc.fadeOut(0.1);
this.runAction(cc.sequence(cc.spawn(move,fade),cc.callFunc(function(){
this.removeFromParent(true)
})));
注意这样写会直接移除掉这个sprite类,所以需要在这个类里面新建一个空sprite的节点来添加动画
this.setPosition(po);
var sp = new cc.Sprite();
sp.setSpriteFrame(cc.spriteFrameCache.getSpriteFrame("100.png"));
this.addChild(sp);
var move = cc.moveBy(0.1,cc.p(0,50));
var fade = cc.fadeOut(0.1);
sp.runAction(cc.sequence(cc.spawn(move,fade),cc.callFunc(function(){
sp.removeFromParent(true)
})));
打地鼠游戏核心算法
最新推荐文章于 2022-04-23 21:21:54 发布