第一次实现一个面向对象的游戏

 properties: {
                startprefab:{default:null,type:cc.Prefab},
               bulletprefab:{default:null,type:cc.Prefab},
               scorelable:{default:null,type:cc.Label},
               player:{default:null,type:cc.Node},
               background:{default:null,type:cc.Node},
               bullett_num:0,
               bullett_speed:0,
    },
mousegetposition()
{
      this.node.on(cc.Node.EventType.TOUCH_START,function(event){
            var touch =event.getTouches()[0];
            var position = touch.getLocation();
            var localPosition = this.node.convertToNodeSpaceAR(position);
            this.shoot(localPosition);


    // this.node.on(cc.node.EventType.TOUCH_START,function(event)
    // {
    //     var touch=event.getTouches()[0];
    //     var position=touch.getLocation();
    //     var localposition=this.Node.place(position);//修改过
    //     this.shoot(localposition);


    },this);
},
newstar()
{
    if(this.happen!=null)
    {
        
        this.happen.destroy();
    }
    var happen=cc.instantiate(this.startprefab);
    this.node.addChild(happen);
    happen.position=this.randomPosition();
    this.happen=happen;
},
randomPosition()
{
    var x=this.node.width/2*cc.randomMinus1To1();
    var y=320*cc.random0To1();
    return cc.p(x,y);
},
shoot(normalposition)
{
     if(0<this.bullett_num)
   {
        var fire=cc.instantiate(this.bulletprefab);
        this.node.addChild(fire);
        fire.position=this.player.position;
        let bulletthaverb=fire.getComponent("rigidBody");
        normalposition=cc.pNormalize(cc.pSub(normalposition,fire.position));
        
     fire.getComponent(cc.RigidBody).linearVelocity=cc.pMult(normalposition,this.bullett_speed);
        this. bullett_num--;
    }
},
scoreAdd()
{
    this.count++;
    this.scorelable.string="Scroce:"+this.count;
},
onLoad()
{
    cc.director.getPhysicsManager().enabled=true;
        cc.director.getPhysicsManager().debugDrawFlags = cc.PhysicsManager.DrawBits.e_aabbBit;
    this.mousegetposition();
    this.count=0;
    this.happen=null;
    this.newstar();


},



star.........


cc.Class({
    extends: cc.Component,


    properties: {
        
    },
  onBeginContact(contact,thisColider,otherColider)
  {
    
      if(otherColider.node.name=="star")
      {
         console.log("star");
          otherColider.node.parent.getComponent("game").newstar();
          otherColider.node.parent.getComponent("game").scoreAdd();
          
      }
  
     
     this.node.parent.getComponent("game").bullett_num++;
     this.node.destroy();
     console.log("1000000000");
  },
   onLoad () {},


    start () {


    },


    // update (dt) {},
});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值