|
|
楼主 |
发表于 2016-11-26 19:54:29
|
显示全部楼层
2016年11月26日19:44:22
摸了几天鱼,今天着手写了自机弹幕的发射。
大概思路如下:
创建一个精灵节点,调整好材质之后,挂上去一个让它自动向前移动和在几秒后销毁的脚本。然后把这个精灵节点设置成Prefab
然后在触发按键事件之后按照一定速率重复生成该Prefab。【注意:这种方式有重大缺陷,重复的创建销毁对象,对性能的影响非常大。这里只做演示,完成后会改成用数组存储弹幕缓存的方式来进行优化】要注意的是,生成新的对象Prefab之后,将其加入到角色的子节点。这样设置坐标的时候就可以以角色本身的坐标系来进行。
这样就完成了子弹的发射。
如果要完成子弹和敌机的碰撞功能,需要在子弹节点和敌机上添加碰撞体。然后将碰撞进行分组。我设置的是playerBullet碰撞enemy。
这样当带有这两个标签的,带有碰撞体的节点相互碰撞,就会触发 onCollisionEnter()onCollisionStay()onCollisionExit()三个方法。
需要注意的是,碰撞管理默认情况下是关闭的,所以需要用代码手动启用。
子弹脚本如下:
- cc.Class({
- extends: cc.Component,
- properties: {
- // foo: {
- // default: null, // The default value will be used only when the component attaching
- // to a node for the first time
- // url: cc.Texture2D, // optional, default is typeof default
- // serializable: true, // optional, default is true
- // visible: true, // optional, default is true
- // displayName: 'Foo', // optional
- // readonly: false, // optional, default is false
- // },
- // ...
- shootSpeed:0,
-
- },
- onCollisionEnter: function (other, self) {
- console.log('on collision enter');
- self.node.destroy();
- },
- onCollisionStay: function (other, self) {
- console.log('on collision stay');
- },
- onCollisionExit: function (other, self) {
- console.log('on collision exit');
-
- other.node.destroy();
-
- },
- // use this for initialization
- onLoad: function () {
- this.counter=0;
- var manager = cc.director.getCollisionManager();
- manager.enabled = true;
- },
- move:function(dt){
- this.node.y+=this.shootSpeed*dt;
- },
-
- // called every frame, uncomment this function to activate update callback
- update: function (dt) {
- this.counter++;
- this.move(dt);
-
- //if(this.node.y>this.game.height*0.5||this.node.y<0-game.height*0.5||this.node.x<0-this.game.width*0.5||this.node.x>this.game.width*0.174)
- if(this.counter>=100)
- this.node.destroy();
- },
- });
复制代码
添加了发射功能的角色脚本如下:
- cc.Class({
- extends: cc.Component,
- properties: {
- // foo: {
- // default: null, // The default value will be used only when the component attaching
- // to a node for the first time
- // url: cc.Texture2D, // optional, default is typeof default
- // serializable: true, // optional, default is true
- // visible: true, // optional, default is true
- // displayName: 'Foo', // optional
- // readonly: false, // optional, default is false
- // },
- // ...
- idle:{
- default:null,
- type:cc.SpriteFrame
- },
- left:{
- default:null,
- type:cc.SpriteFrame
- },
- right:{
- default:null,
- type:cc.SpriteFrame
- },
- normalBullet:{
- default:null,
- type:cc.Prefab
- },
- game:{
- default:null,
- type:cc.Node
- },
-
- moveSpeed:0,
- fireSpeed:0,//普通攻击速率
- specialBomb:0,//特殊攻击可用次数
- weaponLevel:0,//武器等级
- weaponPosX:0,
- weaponPosY:0,
- },
- // use this for initialization
- onLoad: function () {
- this.counter=0;
- this.sprite=this.getComponent(cc.Sprite);
- this.setInputControl();
- var manager = cc.director.getCollisionManager();
- manager.enabled = true;
- },
-
- cannonControl:function(){//直接在自机前方生成子弹
- },
- setInputControl:function(){
- cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN,
- this.onKeyDown, this);
- cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP,
- this.onKeyUp, this);
- },
- onKeyDown:function(event){
- if(event.keyCode==cc.KEY.z){
- this.cannonFire=true;
- }
-
- if(event.keyCode==cc.KEY.left){
- this.sprite.spriteFrame=this.left;
- this.enabled=false;
- this.enabled=true;
- this.runToLeft=true;
- }
- if(event.keyCode==cc.KEY.right){
- this.sprite.spriteFrame=this.right;
- this.enabled=false;
- this.enabled=true;
- this.runToRight=true;
- }
- if(event.keyCode==cc.KEY.up){
- this.sprite.spriteFrame=this.idle;
- this.enabled=false;
- this.enabled=true;
- this.runToUp=true;
- }
- if(event.keyCode==cc.KEY.down){
- this.sprite.spriteFrame=this.idle;
- this.enabled=false;
- this.enabled=true;
- this.runToDown=true;
- }
- if(event.keyCode==cc.KEY.shift){
- this.moveSpeed=this.moveSpeed*0.5;
- }
-
-
- },
- onKeyUp:function(event){
-
- if(event.keyCode==cc.KEY.left){
- this.sprite.spriteFrame=this.idle;
- this.enabled=false;
- this.enabled=true;
- this.runToLeft=false;
- }
- if(event.keyCode==cc.KEY.right){
- this.sprite.spriteFrame=this.idle;
- this.enabled=false;
- this.enabled=true;
- this.runToRight=false;
- }
- if(event.keyCode==cc.KEY.up){
- this.runToUp=false;
- }
- if(event.keyCode==cc.KEY.down){
- this.runToDown=false;
- }
- if(event.keyCode==cc.KEY.shift){
- this.moveSpeed=this.moveSpeed*2;
- }
- if(event.keyCode==cc.KEY.z){
- this.cannonFire=false;
- }
- },
-
- normalAttackLv1:function(dt){
-
- if(this.counter>=this.fireSpeed){
- var bullet=cc.instantiate(this.normalBullet);
- this.node.addChild(bullet);
- bullet.setPosition(0,0);
- bullet.getComponent("NormalBulletScript").game=this.game;
-
-
- this.counter=0;
- }
- else{
- this.counter++;
- }
- //cc.log(this.node.x+"x"+this.node.y);
- },
- normalAttackLv2:function(dt){
- },
- normalAttackLv3:function(dt){
- },
- boardCheck:function(){
-
- var screenWidth=this.game.width;
- var screenHeight=this.game.height;
- if(this.node.x<0-0.5*screenWidth)
- this.node.x=0-0.5*screenWidth;
- if(this.node.x>0.174*screenWidth)
- this.node.x=0.174*screenWidth;
- if(this.node.y<0-0.5*screenHeight)
- this.node.y=0-0.5*screenHeight;
- if(this.node.y>0.5*screenHeight)
- this.node.y=0.5*screenHeight;
-
- },
- moveControl:function(dt){
- if(this.cannonFire){
- this.normalAttackLv1(dt);
- }
- if(this.runToLeft){
- this.node.x -=this.moveSpeed*dt;
- }
- if(this.runToRight){
- this.node.x+=this.moveSpeed*dt;
- }
- if(this.runToUp){
- this.node.y+=this.moveSpeed*dt;
- }
- if(this.runToDown){
- this.node.y-=this.moveSpeed*dt;
- }
-
- },
- // called every frame, uncomment this function to activate update callback
- update: function (dt) {
- if(this.cannonFire){
- this.normalAttackLv1(dt);
- }
- this.moveControl(dt);
- this.boardCheck();
-
-
- },
- });
复制代码 我的命名不规范,还请不要学习我。
如果仔细看代码的话会发现我创建了三个类似的攻击函数,并只实现了一个。
剩下的两个是为了给拾取道具增强火力做出的预留。【并非最终版本,设计随时可能会变更】
接下来要实现至少一种的敌人和敌人的弹幕设计。
敌人和敌人的弹幕设计都做好之后,会进行第一阶段的优化。
将弹幕以及敌人进行缓存处理。
这次更新就到这里。
|
|