Culture and education you achieve a "battle aircraft" in the control logic fighter

Vertical version of shooting game is a more classic type of game, from the early NES platform to today's mobile platforms, have always been very classic game works. Vertical version of shooting game only need to control the aircraft and avoid enemy bullets and attack the enemy, and play operations are very simple, so it is suitable for operation on mobile platforms. Once micro-channel platform smash hit "World War II aircraft." I believe that everyone played, so to teach you today how to implement control logic game fighter.

 

1. First, create a game scene GameScene, add background of the game and today the protagonist in the scene - Fighter:

 

2. Next, create a control script GamePlane.js fighter:

 

3. after successfully created can be edited, and control the fighter is divided into two types: one is to hold down anywhere on the screen to achieve fighter drag; the other is only holding down the fighter aircraft drag nodes can be achieved. If you want to hold down and drag anywhere on the screen to achieve fighter, the Registrar touch event fighter parent monitor; if you want to hold down the fighter node can be achieved only fighter drag touch event fighter node is registered listeners. This embodiment is a first implementation, the second to remove .parent:

/** * GamePlane.js */cc.Class({    extends: cc.Component,    properties: {    },    // LIFE-CYCLE CALLBACKS:    onLoad() {        /**         * 如果想按住屏幕任意位置实现战斗机拖动,则注册战斗机父节点的 touch 事件监听;         * 如果想只有按住战斗机节点才能实现战斗机拖动,则注册战斗机节点的 touch 事件监听。         * 本例为第一种实现方式,第二种去掉 .parent 即可。         */        this.node.parent.on(cc.Node.EventType.TOUCH_MOVE, this.touchMoveCallBack, this);    },    start() {    },    // update (dt) {},    // TouchMove 回调函数    touchMoveCallBack: function (event) {        console.log("touchMoveCallBack.");        let rangePos = event.getDelta();    // 获取位置变化        this.node.x += rangePos.x;        this.node.y += rangePos.y;        // 控制飞机节点不能移动出屏幕        let minX = -this.node.parent.width/2 + this.node.width/2; // 最小X坐标        let maxX = Math.abs(minX);    // 最大X坐标        let minY = -this.node.parent.height/2 + this.node.height/2; // 最小Y坐标        let maxY = Math.abs(minY);    // 最大y坐标        let currentPos = this.node.getPosition();        if (currentPos.x < minX) {            currentPos.x = minX;        }else if (currentPos.x > maxX) {            currentPos.x = maxX;        }else if (currentPos.y < minY) {            currentPos.y = minY;        }else if (currentPos.y > maxY) {            currentPos.y = maxY;        }        this.node.setPosition(currentPos);      },});

 

4. Next, the edited script to mount the fighter can preview the node:

 

You can see even without holding down the drag fighter fighter node can also be achieved, without exceeding the scope of the screen, of interest to a small partner hurry to try it!

 


 

I am a "Super in" determined to make people have a positive feedback every day!

 

 

Guess you like

Origin www.cnblogs.com/yu97271486/p/11302461.html