橡皮怪Dici,over,welcome,player

本文介绍了一个2D平台跳跃游戏的开发过程,包括使用Cocos2d-x进行场景搭建、角色控制及碰撞检测等内容。文章还详细解释了如何通过脚本语言控制游戏角色的动作、触发事件等关键要素。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Dici

cc.Class({
    extends: cc.Component,
    properties:
    {
        //主角遇刺音效
        dieAudio:
        {
           default:null,
           url:cc.AudioClip
        },
    },

    onLoad: function () 
    {
        //计时器
        this.schedule(this.setDiciMove,1);
        //为屏幕设置触摸侦听事件,控制地刺移动
        this.node.parent.on(cc.Node.EventType.MOUSE_ENTER,this.setDiciMove.bind(this),this);
    },

    //设置地刺移动
    setDiciMove:function()
    {     
        var goAction= cc.moveBy(1,cc.p(-960,0));
        this.node.runAction(goAction);
    },

    //返回地刺在世界坐标中的包围盒
    noteBox:function()
    {
        return this.node.getBoundingBoxToWorld();
    },

    update: function (dt) 
    {
        var player = cc.find("Canvas/player").getComponent("Player");
        if(cc.rectIntersectsRect(player.node.getBoundingBoxToWorld(),this.noteBox()))
        {
            cc.audioEngine.playEffect(this.dieAudio,false);
           //cc.director.loadScene('over');
        }
        if(this.node.y > cc.winSize.height/2)
        {
            this.node.destroy();
        }
    },
});```


**over**

cc.Class({
extends: cc.Component,

properties: 
{
   scoreLabel:cc.Label,     //本轮得分文本
   button:cc.Node,          //重新开始按钮
   highScoreLab:cc.Label,   //最高分文本
   breakRecordParticle:cc.ParticleSystem,
},


breakRecordAction:function()
{
  var action1 = cc.scaleTo(0.2,1.2);
  var action2 = cc.scaleTo(0.2,1);
  var seq = cc.sequence(action1, action2);
  var repeat = cc.repeatForever(seq);
  return repeat;
},

onLoad: function () 
{
    this.breakRecordParticle.enabled = false;
    var highScore = cc.sys.localStorage.getItem("highScore") || 0;
    var score = cc.sys.localStorage.getItem("score");
    cc.director.preloadScene("game");
    if(Number(highScore) < Number(score))
    {
        this.breakRecordParticle.enabled = true;
        this.highScoreLab.node.runAction(this.breakRecordAction());
        cc.sys.localStorage.setItem("highScore",Number(score));
    }
    highScore = cc.sys.localStorage.getItem("highScore");
    this.highScoreLab.string = "HighScore: " + highScore;
    this.scoreLabel.string = "本轮得分:"+score;
    this.button.on("touchstart",function(){
        cc.director.loadScene("game");
    });
},

});


**welcome**

cc.Class({
extends: cc.Component,
properties:
{
//背景音乐
bgAudio:
{
default:null,
url:cc.AudioClip
},
//按钮图标
startBtn: cc.Node,
},

onLoad: function () 
{
    cc.audioEngine.playMusic(this.bgAudio,true);
    cc.director.preloadScene("game");
    var scaleTo = cc.scaleTo(0.8,0.9);
    var reverse = cc.scaleTo(0.8,1);
    var seq = cc.sequence(scaleTo,reverse);
    var repeat = cc.repeatForever(seq);
    this.startBtn.runAction(repeat);
    this.startBtn.on("touchstart",function(){
         cc.audioEngine.pauseMusic();
         cc.director.loadScene("game");
    });
},

});



**player**

cc.Class({
extends: cc.Component,

properties: {},

onLoad: function (){},

noteBox:function()
{
    return this.node.getBoundingBox();
}

});
“`

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值