cocos creator笔记(14)

tiledmap


通过图层来放不同的叠加场地

对象层可以保存信息并且可以通过代码获取
对象层在可覆盖层之上,在遮盖层之下
在这里插入图片描述
在这里插入图片描述
MapControl

const {ccclass, property} = cc._decorator;



@ccclass
export default class MapContrrol extends cc.Component {
    map:cc.TiledMap;
    player:cc.Node = null;
    
    start () {
        //获取地图信息
        this.map = this.getComponent(cc.TiledMap);
        //普通层
        //this.map.getLayer();
        //对象层
        let playerLayer = this.map.getObjectGroup("player");
        //提取某个对象
        let playerObj = playerLayer.getObject("startpos");
        //判断是否为玩家对象
        if(playerObj.isPlayer == true){
            //加载玩家预设体
            cc.loader.loadRes('player',cc.Prefab,(res,playerPre)=>{
                //创建玩家
                this.player = cc.instantiate(playerPre);
                this.player.setParent(this.node.children[2].children[0]);
                this.player.x = playerObj.x;
                this.player.y = playerObj.y;

            });
        }
        
    }
     
    update(dt){
        //摄像头跟随玩家
        if(this.player !== null){
            cc.Camera.main.node.x = this.player.x - 240;//减去半个屏幕的位置使其置于左下角
            cc.Camera.main.node.y = this.player.y - 160;

        }
    }
}

Input


const {ccclass, property} = cc._decorator;

@ccclass
export default class Input  {
    private static instance : Input = null;

    //水平轴
    horizontal:number = 0;
    //垂直轴
    vertical:number = 0;
    
    static get Instance(){
        if(this.instance == null){
            this.instance = new Input();
        }
        return this.instance;
    }
    constructor(){
        //键盘按下
        cc.systemEvent.on(cc.systemEvent.EventType.KEY_DOWN,(event)=>{
            if(event.keyCode == cc.macro.KEY.w){
                this.vertical = 1;
            }else if(event.keyCode == cc.macro.KEY.s){
                this.vertical = -1;
            }
            if(event.keyCode == cc.macro.KEY.a){
                this.horizontal = -1;
            }else if(event.keyCode == cc.macro.KEY.d){
                this.horizontal = 1;
            }
        });
        //键盘抬起
        cc.systemEvent.on(cc.systemEvent.EventType.KEY_UP,(event)=>{
            if(event.keyCode == cc.macro.KEY.w&&this.vertical == 1 ){
                this.vertical = 0;
            }else if(event.keyCode == cc.macro.KEY.s&&this.vertical == -1){
                this.vertical = 0;
            }
            if(event.keyCode == cc.macro.KEY.a&&this.horizontal == -1){
                this.horizontal = 0;
            }else if(event.keyCode == cc.macro.KEY.d&&this.horizontal == 1){
                this.horizontal = 0;
            }
        });
    }
   
}


PlayerControl

import Input from "./Input";

const {ccclass, property} = cc._decorator;

@ccclass
export default class PlayerControl extends cc.Component {

    //速度
    speed: number =20;

    start () {

    }

    update (dt) {
        //移动dt帧间隔
        this.node.x += this.speed *dt*Input.Instance.horizontal;
        this.node.y += this.speed*dt*Input.Instance.vertical;
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值