Analyze the production steps:1. Prepare resources and build the sceneYou can find resources online or use mine. Create a folder and put the resources in the res folder. Build the scene: Step 2: Create Step 3: Add the player, bullets, and enemy planes to the scene in the same way, create a Label node to display the score, and adjust the position; 2. Code controls the game Step 1: Create a Step 2: Edit the code and add properties in Step 3: Code logic control, including initializing players, bullets, enemies; registering monitoring events; writing action functions; scoring judgment, etc.; Full code: cc.Class({ extends: cc.Component, properties: playerNode: cc.Node, enemyNode: cc.Node, fireNode: cc.Node, scoreNode: cc.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("Defeat the enemy aircraft"); this.scoreNode.string= ++this.score; //hit score this.fireNode.stopAction(this.fireAction); this.enemyNode.stopAction(this.enemyAction); this.enemyNode.active=false; this.fireNode.active=false; this.fireLoad();//Initialize bullets this.enemyLoad();//Initialize enemy aircraft} }, // Close event listening onDestroy(){ this.node.off("touchstart",this.fire,this); }, // Initial player playLoad(){ this.score=0; this.playerNode.y=-cc.winSize.height/4; }, //Initialize bullet fireLoad(){ this.fireNode.active=true; this.isFire=false; this.fireNode.x=this.playerNode.x; this.fireNode.y=this.playerNode.y+this.playerNode.height; }, // Initialize the enemy enemyLoad(){ this.enemyNode.active=true; this.enemyNode.x=Math.random()* cc.winSize.width; this.enemyNode.y=cc.winSize.height/3; let x = cc.winSize.width/2-this.enemyNode.width/2; let y=Math.random()* cc.winSize.height/4; let seq=cc.repeatForever(cc.sequence(cc.moveTo(1.5,cc.v2(-x,y)),cc.moveTo(1.5,cc.v2(x,y)))); this.enemyAction = this.enemyNode.runAction (seq); }, // Reload the game on death dear(){ console.log("death"); cc.director.loadScene("game_scenes"); }, // Fire bullet fire(){ if(this.isFire) return; this.isFire=true; console.log("Start launching"); var fireaction = cc.sequence( cc.moveTo(1,cc.v2(this.playerNode.x,cc.winSize.height/2)), cc.callFunc(()=>{ this.dear(); })); this.fireAction = this.fireNode.runAction(fireaction); console.log("End of launch"); } }); Final resultThe above is the details of how to use CocosCreator to realize shooting games. For more information about CocosCreator to realize shooting games, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: Nginx try_files directive usage examples
>>: How to avoid the trap of URL time zone in MySQL
You can view the container logs through the docke...
1. Check whether event is enabled show variables ...
A recent business involves such a requirement tha...
Today I suddenly thought of reviewing the producti...
need When querying a field, you need to give the ...
Table of contents 1. Requirements description 2. ...
Today, I am sharing the valuable experience of a ...
background Since I converted all my tasks to Dock...
In MySQL, you can specify multiple indexes for a ...
Solution to mysql not closing: Right-click on the...
Table of contents 1. Environment variable $PATH: ...
This article shares the specific code of js to im...
MySQL is the most popular relational database man...
Recently, the project uses kubernetes (hereinafte...
#include <linux/moduleparam.h> 1. Module pa...