如何用CocosCreator实现射击小游戏
网络编程 2021-07-04 14:07www.168986.cn编程入门
这篇文章主要介绍了如何用CocosCreator实现射击小游戏,此游戏难度不大,仅作为入门的练手小游戏,一小时就能完成,里面用到的知识很常用,喜欢游戏的同学,可以参考下
分析下制作步骤
1. 准备好资源,搭建场景
资源的话可以自己到网上找,也可以直接用我的也行;创建好相应文件夹,把资源放到res文件夹下;
搭建场景
第一步:创建一个单色精灵(Script) bg 背景, 设置好颜色,加一个Widget组件,使其充满屏幕;
第二步 在bg
节点下创建和
button
空节点作为顶与底部,然后在两个空节点加入带刺的节点(直接将图片拖到层级管理器就可以),现在我们需要给与button
节点添加一个Layout组件
,属性设置如图,这样可以看到屏幕上下都有刺了。
第三步 将玩家小人、子弹、敌机同样的方法加入到场景中,再创建一个Label节点用来显示分数,调节一下位置;
2. 代码控制游戏
第一步 创建一个game
脚本,挂载到dg
节点上;
第二步 编辑代码,在 properties
添加属性,用来关联玩家、子弹、敌人节点,再编辑器关联;
第三步 代码逻辑控制,包括初始化玩家、子弹、敌人;注册监听事件;写动作函数;计分判断等;
全部代码
.Class({ extends: .Component, properties: { playerNode: .Node, enemyNode: .Node, fireNode: .Node, scoreNode: .Label, }, onLoad () { this.playLoad(); this.fireLoad(); this.enemyLoad(); this.node.on("touchstart",this.fire,this); }, update (dt) { if(Math.abs(this.fireNode.y-this.enemyNode.y)<(this.fireNode.height/3+this.enemyNode.height/3) &&Math.abs(this.fireNode.x-this.enemyNode.x)<(this.fireNode.width/3+this.enemyNode.width/3)){ console.log("击败敌机"); this.scoreNode.string= ++this.score;//击中得分 this.fireNode.sAction(this.fireAction); this.enemyNode.sAction(this.enemyAction); this.enemyNode.active=false; this.fireNode.active=false; this.fireLoad();//初始化子弹 this.enemyLoad();// 初始化敌机 } }, // 关闭事件监听 onDestroy(){ this.node.off("touchstart",this.fire,this); }, // 初始玩家 playLoad(){ this.score=0; this.playerNode.y=-.winSize.height/4; }, //初始化子弹 fireLoad(){ this.fireNode.active=true; this.isFire=false; this.fireNode.x=this.playerNode.x; this.fireNode.y=this.playerNode.y+this.playerNode.height; }, // 初始化敌机 enemyLoad(){ this.enemyNode.active=true; this.enemyNode.x=Math.random() .winSize.width; this.enemyNode.y=.winSize.height/3; let x=.winSize.width/2-this.enemyNode.width/2; let y=Math.random() .winSize.height/4; let seq=.repeatForever(.sequence(.moveTo(1.5,.v2(-x,y)),.moveTo(1.5,.v2(x,y)))); this.enemyAction=this.enemyNode.runAction(seq); }, // 死亡 重新加载游戏 dear(){ console.log("死亡"); .director.loadScene("game_scenes"); }, // 发射子弹 fire(){ if(this.isFire) return; this.isFire=true; console.log("开始发射"); var fireaction=.sequence( .moveTo(1,.v2(this.playerNode.x,.winSize.height/2)), .callFunc(()=>{ this.dear(); })); this.fireAction=this.fireNode.runAction(fireaction); console.log("结束发射"); } });
最终效果
以上就是如何用CocosCreator实现射击小游戏的详细内容,更多关于CocosCreator实现射击小游戏的资料请关注狼蚁SEO其它相关文章!
编程语言
- 宿迁百度关键词排名指南:实现精准营销的关键
- 四川SEO优化怎么做网络推广
- 立昂技术备案老域名收购:如何为您的业务赋能
- 安徽百度关键词seo贵不贵,一般需要多少钱
- 吉林百度快照排名怎么做电话营销
- 多伦新手做SEO怎么做
- 甘肃优化关键词排名推广怎么做论坛营销
- 沙雅SEO网站推广:提升您的在线可见性
- 四川SEO优化如何提升销售额和销售量
- 聂荣网站排名优化:提升网站可见性的全方位指
- 涞水SEO:提升地方企业在线可见性的策略
- 辽宁百度seo排名怎样做网站排名
- 临湘哪有关键词排名优化:提升网站可见度的关
- 黑龙江百度网站优化有没有优惠
- 凉城优化关键词排名推广:提升您的网络可见性
- 萝北整站优化:提升您网站流量和排名的全面指