国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

node.js|vue.js|jquery|angularjs|React|json|js教程|

服務器之家 - 編程語言 - JavaScript - js教程 - 詳解CocosCreator制作射擊游戲

詳解CocosCreator制作射擊游戲

2022-03-03 16:56缺氧何甘酸 js教程

這篇文章主要介紹了CocosCreator制作射擊游戲,各個方面都講的比較詳細,希望同學們自己動手試一下

場景布置

游戲資源

詳解CocosCreator制作射擊游戲

詳解CocosCreator制作射擊游戲

詳解CocosCreator制作射擊游戲

炮塔旋轉

機制與之前手柄實例的小車相同,使用touchmove監聽觸摸事件,

  1. 獲取觸摸位置
  2. 通過位置用signAngle方法將該位置與cc.v2(1,0)位置的角度差求出(記得要加負號,比較所得逆時針為負,賦值angle逆指針為正)。
  3. 所求的的角度即為最終角度。

詳解CocosCreator制作射擊游戲

 onLoad(){
        //初始化為90度
        this.node.angle=90;
        this.node.on("touchstart",this.onTouchStart,this);
        this.node.on("touchmove",this.onTouchMove,this);
        this.node.on("touchend",this.onTouchEnd,this);
        this.node.on("touchconcel",this.onTouchConcel,this);
    }
   
    onTouchStart(e:cc.Event.EventTouch){
        //獲取開始的位置
        this.starPos=this.node.parent.convertToNodeSpace(e.getLocation());
        //獲取炮口的初始角度
        this.starAngle=this.node.angle;

    }
    onTouchEnd(e:cc.Event.EventTouch){

    }
    onTouchMove(e:cc.Event.EventTouch){
        //獲取觸點當前的位置
        let pos:cc.Vec2=this.node.parent.convertToNodeSpace(e.getLocation());

        //獲取角度
        //angle順時針為負逆時針為正
        let sweep_radian=pos.signAngle(this.starPos);//pos相對于starPose的角度p相對s順時針為正
        let sweep_angle=sweep_radian*180/Math.PI;//弧度制換算角度
        
        //讓炮塔的角度指向最終的角度
        let angle=this.starAngle-sweep_angle;
      
        //將角度限制在45~135之間
        if(angle<45)angle=45;
        if(angle>135)angle=135;

        cc.log("炮口擺動:"+sweep_angle+"最終角度位置:"+angle);
        this.node.angle=angle;
    }

動態生成子彈

詳解CocosCreator制作射擊游戲

  1. 生成節點cc.Node,并增加組件addComponent(cc.Sprite)
  2. 為組件的屬性賦值,將圖片的spriteFrame賦值
  3. 將組件掛載在一個父節點下
  4. 設置位置、角度等
  5. 控制其運動可以導入新建的腳本,并將該腳本增加到動態生成節點的組件中
 onTouchEnd(e:cc.Event.EventTouch){
        this.fire();
    }
    onTouchConcel(e:cc.Event.EventTouch){

    }

    fire(){

        if(this.bulleteicon==null)return;
        let bullet:cc.Node=new cc.Node();
        let sprite:cc.Sprite=bullet.addComponent(cc.Sprite);
        sprite.spriteFrame=this.bulleteicon;
        

        //掛載到射擊系統節點下
        bullet.parent=this.node.parent;

        //設置相對父節點位置
        let ration=this.node.angle*Math.PI/180;
        let direction=cc.v2(Math.cos(ration),Math.sin(ration));
        bullet.angle=this.node.angle;
        let r=100;
        bullet.setPosition(cc.v3(r*direction.x,r*direction.y,0));
       
        //附加腳本組件
         let script=bullet.addComponent(Buletet);
         script.explodeImg=this.explodeImg;
         script.direction=direction;

    }
 start () {
         this.schedule(this.onTimer,0.01);
    }

    onTimer(){
        if(this.node.y>300){
            this.unschedule(this.onTimer);
            this.explode();
           
            return;
        }
        let dx=this.direction.x*5;
        let dy=this.direction.y*5;
        this.node.y+=dy;
        this.node.x+=dx;
    }

    explode(){

        let sp:cc.Sprite=this.getComponent(cc.Sprite);
        sp.spriteFrame=this.explodeImg;
      
        //將子彈縮小
        this.node.scale=0.1;
        //爆炸動畫效果緩動系統
        let self=this;
        cc.tween(this.node)
            .to(0.5,{scale:1,opacity:0})
            .call(function(){
                self.afterExplode();
            })
            .start();

            
    }

    afterExplode(){
        this.node.destroy();
    }

本次bug:

  1. 導入的類名與文件名不同, 注意重命名文件不會自動修改代碼中的類名,需要修改兩次
  2. setposition()方法使用時參數寫在了cc.v3的構造函數內,一定注意參數的位置

碰撞計算

詳解CocosCreator制作射擊游戲

計算子彈和靶標的相對位置,若小于范圍,則判斷為命中靶,執行命中的操作,否則判斷為沒有命中,執行沒有命中的操作。

腳本需傳入靶子節點,增加target屬性

   @property(cc.SpriteFrame)
    explodeImg: cc.SpriteFrame = null;
    
    direction: cc.Vec2 = null;

    target: cc.Node = null;
    onLoad() {

    }

    start() {
        this.schedule(this.onTimer, 0.01);
    }

    onTimer() {
        if (this.node.y > 350) {
            if (this.isHit()) {
                //播放爆炸效果
                this.explode();
                console.log("命中靶");
            }
            else {
                console.log("脫靶");
                this.disMiss();
            }
            this.unschedule(this.onTimer);
            return;
        }
        let dx = this.direction.x * 5;
        let dy = this.direction.y * 5;
        this.node.y += dy;
        this.node.x += dx;
    }

    //判斷是否命中
    isHit(): boolean {
        let targetPos: cc.Vec2 = this.geWorldLocation(this.target);
        let selfPos: cc.Vec2 = this.geWorldLocation(this.node);
        let distance = Math.abs(targetPos.x - selfPos.x);
        console.log("靶標x=" + targetPos.x + " , 子彈x=" + selfPos.x);

        if (distance < 50) {
            return true;

        }
        else {
            return false;
        }

    }
    explode() {

        let sp: cc.Sprite = this.getComponent(cc.Sprite);
        sp.spriteFrame = this.explodeImg;

        //將子彈縮小
        this.node.scale = 0.1;
        //爆炸動畫效果緩動系統
        let self = this;
        cc.tween(this.node)
            .to(0.5, { scale: 1, opacity: 0 })
            .call(function () {
                self.disMiss();
            })
            .start();


    }

    geWorldLocation(node: cc.Node): cc.Vec2 {
        let pos = node.getPosition();
        //注意這里是node.parent。方法的調用者要是當前節點的坐標系
        return node.parent.convertToWorldSpaceAR(pos);

    }

    disMiss() {
        this.node.destroy();
    }

本次bug:

獲取世界坐標時,沒有調用其父節點的坐標系,用了當前節點的坐標系,所以返回的依然是自身當前坐標系的值。記得轉換世界坐標的方法調用者是當前節點的坐標系,一般為其父節點 return node.parent.convertToWorldSpaceAR(pos);(以錨點為原點)

增加效果

詳解CocosCreator制作射擊游戲

在靶子的節點下增加腳本,控制移動,左右來回移動
同時,當子彈命中后增加文字提示效果。

文字提示:

 cheer() {
        //創建節點并掛載
        let node: cc.Node = new cc.Node();
        node.parent = this.node.parent;//兩者同一級,同一個父對象
        let label: cc.Label = node.addComponent(cc.Label);
        label.string = "+10分";

        //設置位置、透明度等
        node.setPosition(cc.v3(0, 250, 0));
        node.opacity = 200;
        node.color = new cc.Color(255, 0, 0);

        //動效
        cc.tween(node)
            .to(0.5, { scale: 1.5 })
            .to(0.2, { opacity: 0 })
            .call(function () {
                node.destroy();
            })
            .start();

    }

靶子移動

 update (dt) {
         let speed=3;
         if(this.isLeft){
             speed=-speed;
         }
         this.node.x+=speed;
         if(this.isLeft&&this.node.x<-350){
             this.isLeft=false;
         }
         if(!this.isLeft&&this.node.x>350){
            this.isLeft=true;
        }
    }

增加彈藥庫的顯示

詳解CocosCreator制作射擊游戲

  1. 增加彈藥庫節點,批量生成子彈圖片(可用widget組件設置位置)
  2. 增加減少子彈方法,并通過設置子彈圖片的active屬性來減少子彈。
  3. 在炮塔的fire方法中調用減少子彈的方法

調用方法有兩種,一種為在炮塔腳本中獲取彈藥庫節點在調用,另一種為設置公共類,(靜態變量),在onLoad()方法中就初始化該節點,然后直接調用。用后者。

 @property(cc.SpriteFrame)
    bulleteIcon: cc.SpriteFrame = null;
    capacity: number = 10;
    stockNumber: number = 10;


    onLoad() {

        let space: number = this.node.width / this.capacity;
        for (let i = 0; i < this.capacity; i++) {
            //生成圖片
            let bulleteNode: cc.Node = new cc.Node();
            let bulleteSprite: cc.Sprite = bulleteNode.addComponent(cc.Sprite);
            bulleteSprite.spriteFrame = this.bulleteIcon;
            this.node.addChild(bulleteNode);

            //設置位置
            bulleteNode.x += space * i + 10;
            bulleteNode.y = 0;

        }

    }

    start() {

    }

    consum(num: number) {
        this.stockNumber -= num;
        if (this.stockNumber < 0) {
            this.stockNumber = 0;
        }
        this.display();
    }

    display() {
        let nodes: cc.Node[] = this.node.children;
        console.log(nodes.length);
        for(let i=0;i<nodes.length;i++){
            if(i>=this.stockNumber){
                nodes[i].active=false;
            }
           
        }
    }

Common公共類

  //靜態類,全局變量,將所有會公用的變量、類定義在Common類中
    static ammo:Ammo=null;

    onLoad() {
        Common.ammo=cc.find("Canvas/彈藥").getComponent("Ammo");
        console.log(Common.ammo);
    }

此處bug:

cc.find()方法中記得用除法的斜杠。

子彈耗盡提示分數

詳解CocosCreator制作射擊游戲

  1. 創建遮罩層,將腳本類導入到Common類中,設置active屬性為false
  2. 在ResultDialog腳本 增加show方法,讓其active屬性變為true同時將分數顯示在屏幕上。
  3. 在Bullete(控制子彈運動腳本)中判斷子彈數量是否<=0,并調用Common中show方法顯示分數提示框。

ResultDialog腳本(控制分數提示框)

 onLoad () {
         let replay:cc.Node=cc.find("Canvas/結束提示框/再玩一局");
         console.log(replay);
         replay.on("touchstart",this.dismiss,this);

         this.node.on("touchstart",this.onTouchdisable,this);
         this.node.on("touchmove",this.onTouchdisable,this);
         this.node.on("touchend",this.onTouchdisable,this);
   
     }

     //顯示提示框
     show(){
        this.node.active=true;  
        let scoreNode : cc.Node = cc.find("分數框/分數", this.node);
        let scoreLabel : cc.Label = scoreNode.getComponent(cc.Label);   
        scoreLabel.string = Common.score + "分";   
       
        
     }

     //隱藏提示框
     dismiss(){
         this.node.active=false;
     }

     //遮罩顯示時屏蔽
     onTouchdisable(e:cc.Event.EventTouch){
         e.stopPropagation();
     }
    start () {

    }

Common腳本

 //靜態類,全局變量,將所有會公用的變量、類定義在Common類中
    static ammo:Ammo=null;
    static score : number = 0;
    static resultdialog : ResultDialog = null;
  

    onLoad() {
        Common.resultdialog=cc.find("Canvas/結束提示框").getComponent("ResultDialog");
        Common.ammo=cc.find("Canvas/彈藥").getComponent("Ammo");
    }

在Bullete方法中增加分數增加

  if (this.isHit()) {
                //播放爆炸效果
                this.explode();
                //顯示+10分
                this.cheer();
                //總分數+10
                Common.score += 10;
                console.log("命中靶");
            }

游戲重開

詳解CocosCreator制作射擊游戲

該小游戲比較簡單,重開只需要重置彈藥庫節點即可,因此reset方法放在Ammo腳本中
在公共類中創建Ammo對象,設置靜態方法,重置得分、以及調用Ammo的reset方法。

Ammo(彈藥庫類)腳本添加

 reset(){
        this.stockNumber=this.capacity;
        this.display();
    }

更改Common腳本

 //靜態類,全局變量,將所有會公用的變量、類定義在Common類中
    static ammo:Ammo=null;
    static score : number = 0;
    static resultdialog : ResultDialog = null;
  

    onLoad() {
        Common.resultdialog=cc.find("Canvas/結束提示框").getComponent("ResultDialog");
        Common.ammo=cc.find("Canvas/彈藥").getComponent("Ammo");
        console.log(Common.ammo);
    }
    static resetGame() {
        Common.score=0;
        Common.ammo.reset();
    }

增加一些細節

詳解CocosCreator制作射擊游戲

增加游戲聲音以及炮塔激活的變化

1.炮塔腳本增加屬性

 //音效
    @property(cc.AudioClip)
    audioFire: cc.AudioClip = null;
    @property(cc.AudioClip)
    audioExplode: cc.AudioClip = null;

    //炮塔圖片
    @property(cc.SpriteFrame)
    iconNormal: cc.SpriteFrame = null;
    @property(cc.SpriteFrame)
    iconActive: cc.SpriteFrame = null;

圖片切換

 onTouchStart(e: cc.Event.EventTouch) {方法最后添加
	   //炮塔圖片切換至激活
        this.node.getComponent(cc.Sprite).spriteFrame = this.iconActive;	
 onTouchEnd(e: cc.Event.EventTouch) {方法最后添加
          //圖片恢復
        this.node.getComponent(cc.Sprite).spriteFrame = this.iconNormal;
      }

音效播放

fire(){   方法后添加

       //將子彈爆炸音頻傳送至子彈腳本
        script.audioExplode = this.audioExplode;
 if (this.audioFire != null) {
            cc.audioEngine.play(this.audioFire, false, 1);
        }
    }

播放音頻的方法:==cc.audioEngine.play(this.audioFire, false, 1);==第二個參數為是否循環播放,第三個參數為音量大小

子彈腳本

//添加屬性
@property(cc.SpriteFrame)
explodeImg: cc.SpriteFrame = null;
在判斷子彈命中靶子的操作后添加
if(this.audioExplode!=null){
	cc.audioEngine.play(this.audioExplode,false,1);
}

以上就是詳解CocosCreator制作射擊游戲的詳細內容,更多關于CocosCreator射擊游戲的資料請關注服務器之家其它相關文章!

原文鏈接:https://blog.csdn.net/m0_46113894/article/details/111301163

延伸 · 閱讀

精彩推薦
  • js教程如何在CocosCreator中利用常駐節點做圖層管理

    如何在CocosCreator中利用常駐節點做圖層管理

    這篇文章主要介紹了如何在CocosCreator中利用常駐節點做圖層管理,這些技巧非常實用,希望同學們看完,回去可以試一下...

    「已注銷」10662022-03-02
  • js教程JavaScript實現前端網頁版倒計時

    JavaScript實現前端網頁版倒計時

    這篇文章主要為大家詳細介紹了JavaScript實現前端網頁版倒計時,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    小 菜5892022-02-20
  • js教程使用Webpack構建多頁面程序的實現步驟

    使用Webpack構建多頁面程序的實現步驟

    這篇文章主要介紹了使用Webpack構建多頁面程序的實現步驟,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋...

    Tuzilow9532022-02-17
  • js教程Javascript實現漢字和拼音互轉的終極方案

    Javascript實現漢字和拼音互轉的終極方案

    網上關于JS實現漢字和拼音互轉的文章很多,但是比較雜亂,有的不支持多音字、不支持聲調或者字典文件太大,無法根據實際需要滿足需求。這篇文章給...

    我是小茗同學10462021-12-15
  • js教程JS實現百度搜索框

    JS實現百度搜索框

    這篇文章主要為大家詳細介紹了JS實現百度搜索框,實時返回搜索建議項,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參...

    張先生的blog9312022-01-24
  • js教程JavaScript Dom實現輪播圖原理和實例

    JavaScript Dom實現輪播圖原理和實例

    這篇文章主要為大家詳細介紹了JavaScript Dom實現輪播圖原理和實例,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    ALonelyLemon3762022-01-21
  • js教程五種使 JavaScript 代碼庫更干凈的方法

    五種使 JavaScript 代碼庫更干凈的方法

    今天向大家介紹5種使JavaScript代碼庫更干凈的方法,一起來看一下都有哪些吧!...

    Mason程10682021-12-29
  • js教程JavaScript實現簡單的計算器功能

    JavaScript實現簡單的計算器功能

    這篇文章主要為大家詳細介紹了JavaScript實現簡單的計算器功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    小小小青臺7662022-02-22
主站蜘蛛池模板: 成人超碰在线 | 在线中文一区 | 久久99久久99 | 中文字幕亚洲一区 | 成人av免费 | 国产精品一区久久久 | 亚洲国产精品成人 | 五月激情综合网 | 久久精品国产99精品国产亚洲性色 | 久久成人人人人精品欧 | 夜夜久久 | 久久99视频精品 | 夜夜骑日日操 | 最新天堂中文在线 | 亚洲久久 | 毛片免费在线 | 99久久婷婷国产综合精品电影 | 99久久精品免费看国产一区二区三区 | 观看av| 午夜精品视频 | 91嫩草视频在线 | 欧美a在线 | 亚洲第一黄色网 | www.国产.com | 日日嗨av一区二区三区四区 | 欧美日韩在线看 | 午夜tv| 亚洲视频区 | 亚洲一区二区久久 | 一级做a爰片久久毛片免费陪 | 99精品国产热久久91蜜凸 | 日韩欧美h | 国产日韩欧美不卡 | 91色在线| 免费观看www7722午夜电影 | 亚洲精品一区二区三区不 | 亚洲一区 日韩精品 中文字幕 | 久久久国色| 亚洲一区二区三区四区的 | 日韩在线小视频 | 99精品在线 |