CocosCreater教程-怪物要糖果(三)

这一讲我们来添加糖果的预制

游戏的玩法是随机从天下掉下糖果,玩家点击糖果,糖果消失,如果不小心让糖果掉落地下,游戏失败,所以我们要让糖果接收触摸事件。

1.在资源管理器里新建一个js脚本命名为Candy.js,在onload里面添加如下代码


     this.node.on(cc.Node.EventType.TOUCH_START,function(event){
            this.node.dispatchEvent( new cc.Event('candy_die', true) );
            this.node.removeFromParent();
        },this);

cc.Node.EventType.TOUCH_START对应 cocos2dx的TouchBegan,这里触发一个事件,交给Game.js去处理加分的逻辑

Game.js里面对应的接收事件的代码:

self.node.on('candy_die',function(event){
            self.addScore(1);//加分数的函数
            self._candiesCount--;
        },self);

//游戏加分
    addScore: function(score){
        
        this._score+=score;
        this.scoreLabel.string= this._score+ '';
        
    }

2.下面制作糖果的预制(prefab)

拖动资源管理器里面的candy_1到canvas里面,然后在属性检查器点击 添加组件 选择脚本Candy.js,然后把Canvas中的这个candy_1拖回资源管理器,这样就生成了一个预制,然后就可以把Canvas中的这个candy_1删除了。其余几个糖果同样的步骤。

3.都做完后,我们来修改Game.js脚本,在properties里面增加
candies: {
default: ],
type: cc.Prefab
},

然后回到编辑器,点击Canvas就可以看到

如图把candy预制依顺序拖入即可。

现在修改脚本增加创建糖果的代码


//生产糖果
    spawCandy: function(dt){
        
        if(this._isLaunch == false) return;
        
        if(this._candiesCount > 5) return;
        
        this._delta+=dt;
        if(this._delta<2) return;
        
        this._delta= 0;
        
        var candyLayer= this.candyLayer.node;
        
        var winSize= candyLayer.getContentSize();
        
        var candiesAmount= this.candies.length - 1;
        
        
        var candy= cc.instantiate(this.candies);
        
        candy.setPosition(this.newCandyPosition());
        
        var self= this;
        
        candy.runAction(cc.sequence(cc.moveBy(4,cc.p(0,-winSize.height-100)),
            cc.removeSelf(true),
            cc.callFunc(function(){self._candiesCount--;},this)));
        
        candyLayer.addChild(candy);
        
        this._candiesCount++;
        
    },

update: function (dt) {
        
        this.spawCandy(dt);
        
    },

4.为游戏添加暂停功能

在Game.js里面添加一个函数


onPause: function(){
        if(cc.director.isPaused()){
            cc.director.resume();
            this.pauseLabel.node.active=false;
        }
        else{
            cc.director.pause(); 
            this.pauseLabel.node.active=true;
        }
        
    },

回到编辑器选中暂停按钮,在属性检查器面板,component选择Game,handler选择onPause

下面贴出Game.js全部代码



cc.Class({
    extends: cc.Component,
    
    //游戏分数
    _score: 0,
    
    //生命
    _life: 0,
    
    //
    _candiesCount: 0,
    
    _isLaunch: false,
    
    _delta: 0,

    properties: {
        pauseLabel: {
            default: null,
            type: cc.Label
        },
        scoreLabel: {
            default: null,
            type: cc.Label
        },
        candyLayer: {
            default: null,
            type: cc.Layout
        },
        gameOverLayer: {
            default: null,
            type: cc.Layout
        },
        candies: {
          default: ],
          type: cc.Prefab
        },
        maxLife: 5
    },
    
    onPause: function(){
        if(cc.director.isPaused()){
            cc.director.resume();
            this.pauseLabel.node.active=false;
        }
        else{
            cc.director.pause(); 
            this.pauseLabel.node.active=true;
        }
        
    },

    // use this for initialization
    onLoad: function () {
        //延迟2秒开始游戏
        var self= this;
        self.scheduleOnce(function(){
            self.initGame();
        },2);
        
        self.node.on('candy_die',function(event){
            self.addScore(1);
            self._candiesCount--;
        },self);
        
        
    },
    
    //初始化游戏
    initGame: function () {
        
        //初始化玩家生命
        this._life= this.maxLife;
        
        //初始化得分
        this._score= 0;
        
        this._isLaunch= true;
        
    },
    
    //游戏加分
    addScore: function(score){
        
        this._score+=score;
        this.scoreLabel.string= this._score+ '';
        
    },
    
    //生产糖果
    spawCandy: function(dt){
        
        if(this._isLaunch == false) return;
        
        if(this._candiesCount > 5) return;
        
        this._delta+=dt;
        if(this._delta<2) return;
        
        this._delta= 0;
        
        var candyLayer= this.candyLayer.node;
        
        var winSize= candyLayer.getContentSize();
        
        var candiesAmount= this.candies.length - 1;
        
        
        var candy= cc.instantiate(this.candies);
        
        candy.setPosition(this.newCandyPosition());
        
        var self= this;
        
        candy.runAction(cc.sequence(cc.moveBy(4,cc.p(0,-winSize.height-100)),
            cc.removeSelf(true),
            cc.callFunc(function(){self._candiesCount--;},self),
            cc.callFunc(function(){self.gameOver();},self)
            ));
        
        candyLayer.addChild(candy);
        
        this._candiesCount++;
        
    },
    
    //
    newCandyPosition: function(){
        
      var winSize= this.candyLayer.node.getContentSize();
      var x= winSize.width*cc.random0To1();
      var y= winSize.height+ 100;
      
      return cc.p(x,y);
      
    },
    
    gameOver: function(){
        this._isLaunch= false;
        this.gameOverLayer.node.active= true;
        
        this.candyLayer.node.removeAllChildren();
    },

    // called every frame, uncomment this function to activate update callback
    update: function (dt) {
        
        this.spawCandy(dt);
        
    },
});


整个工程见附件

-----------------------------------------------------------QQ交流群166029496--------------------------------------------------------------------

很棒的教程!顶一个!

已经顺利的写好,但是遇到点问题,就是层的设置问题应该是。请问你是怎么设置的layer?

真心感谢楼主的教程,让我对cocosCreater简单入门,谢谢,以后还多多向你学习!

顶! :2::2::2::2::2::2::2:

Layer就是Layout,在Layout的属性检查器面板里面,把Sprite组件去掉

第一次点中时 会出现 NAN,怎么破

var candyLayer= this.candyLayer.node;
请问这句是什么意思,layer下面并没有任何node节点啊
Uncaught TypeError: Cannot read property ‘node’ of null
报了这个错误

哦,是我自己写错了

:2:不错的教程 !!

运行的时候控制台报如下错误是什么原因导致的?
教程里好像没有提到要创建layout节点啊?
望大神赐教,不胜感激!

没看到Candies啊

先感谢大神无私分享:14:

楼主你好 请问如果想让Layer里的糖果和Player发生碰撞 如何去处理 我尝试了下失败了

图片都看不了了…什么情况额…

请问附件在哪里?改版后看不到附件了?
因为图都没了,对Layout的设置不清楚。

为什么在浏览器上,糖果精灵可接收touch,但打包成APK放到手机里,点糖果就没有反应了呢?
Candy.js
this.node.on(cc.Node.EventType.TOUCH_START,function(event){
this.node.dispatchEvent( new cc.Event(‘candy_die’, true) );
this.node.removeFromParent();
},this);
这里是不是要修改下啊?如何修改?

很棒的教程!顶一个!

附件在哪里下载

1赞

是要进行回复才能看到附件?

附件呢…