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
1. Do a good job of cleaning before installation ...
Docker Compose is a Docker tool for defining and ...
Table of contents Preface Active withdrawal Excep...
The first article on data backup and restoration ...
Basic Environment Pagoda installation service [Py...
Table of contents 1. let keyword 1.1 Basic Usage ...
Table of contents background Effect Ideas backgro...
This article explains the difference between arro...
illustrate This article was written on 2017-05-20...
Table of contents 1. Shallow cloning 2. Deep clon...
Use canvas to create graphics and text with shado...
Table of contents Business scenario: Effect demon...
I have previously written an article about file t...
Glass Windows What we are going to achieve today ...
Table of contents Preface What are constructible ...