Cocos Creator2D游戏开发(5)-飞机大战(3)-手指操作玩家飞机移动
创始人
2024-11-27 16:04:25
0
  1. 添加玩家飞机
    1.创建player_node节点
    2.将资源中Player图片拖入player_node节点
    3.将Player的Position属性值调整为(0,-300) Content Size属性调整为(100 100)
    4.点击运行,飞机在屏幕上了
    在这里插入图片描述
    2 用手指拖着飞机跑流程
    ① 要监听手指按下
    ②判断手指是否在飞机上
    ③移动飞机
    ④监听手指离开
    在这里插入图片描述
    创建一个脚本Player.ts , 场景节点选player_node节点, 将Player.ts拖入右边属性检查器,保存;
    Player.ts脚本
import { _decorator, Component, EventTouch, Input, input, UITransform} from 'cc'; const { ccclass, property } = _decorator;  @ccclass('Player') export class Player extends Component {      private isPress; //是否按下     start() {         this.isPress = false;     }      onLoad() {         input.on(Input.EventType.TOUCH_START, this.onTouchStart, this);  //注册按下事件         input.on(Input.EventType.TOUCH_MOVE, this.onTouchMove, this);  // 注册移动事件         input.on(Input.EventType.TOUCH_END, this.onToucheEnd, this); // 注册离开事件     }      onToucheEnd(event: EventTouch) {         this.isPress = false;     }       onTouchMove(event: EventTouch) {         if (!this.isPress) {             return;         }         const player = this.node.getChildByName("Player"); // 获取指定的组件         if (player === null) {             return;         }         const uiPos = event.getUILocation(); //获取世界坐标         player.setWorldPosition( uiPos.x,uiPos.y,0); // 设置指定坐标     }      onTouchStart(event: EventTouch) {         const player = this.node.getChildByName("Player");         if (player === null) {             return;         }          const transform = player.getComponent(UITransform);         const newRect = transform.getBoundingBoxToWorld(); //获取player组件框框         if(newRect.contains(event.getUILocation())){             this.isPress= true;         }      }      update(deltaTime: number) {      } }    

相关内容

热门资讯

第三方技巧!hhpoker软件... 您好,hhpoker可以控制吗这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302...
最终!德普之星透视软件免费入口... 最终!德普之星透视软件免费入口官网,aapoker透视脚本,切实存在有挂(今日头条)1、下载好德普之...
日前!模拟器打开hhpoker... 日前!模拟器打开hhpoker,德普辅助软件,总是确实有挂(有挂解惑)1、进入到模拟器打开hhpok...
2026版总结!werplan... 2026版总结!werplan免费挂下载,德州圈脚本,好像真的有挂(有挂技巧)一、werplan免费...
反观!wpk真吗,hhpoke... 反观!wpk真吗,hhpoker破解工具,都是真的有挂(证实有挂)运hhpoker破解工具辅助工具,...
在玩家背景下!hhpoker透... 在玩家背景下!hhpoker透视脚本下载,hhpoker辅助挂,一直确实有挂(有挂详情)1、让任何用...
黑科技辅助挂!fishpoke... 黑科技辅助挂!fishpoker透视底牌,hhpoker有没有作弊挂,一直真的是有挂(真的有挂)1、...
今天上午!aapoker怎么选... 今天上午!aapoker怎么选牌,wepoker私人辅助器,都是是真的有挂(有挂实锤)1、不需要AI...
据目击者称!wpk德州局透视,... 据目击者称!wpk德州局透视,wpk是真的还是假的,真是存在有挂(有挂规律)1、玩家可以在wpk是真...
第三方技巧!xpoker辅助工... 第三方技巧!xpoker辅助工具,wpk俱乐部怎么作弊,一直是有挂(有挂透明挂)1、上手简单,内置详...