2d横板游戏,背景轮换,相机跟随人物

// Learn cc.Class:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] https://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

var curMod = cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
        player: cc.Node,
        mapCamera: cc.Node,
        PicWidth: 720,//图片宽度
        _cameraSpeed: 5,
    },

    statics: {
        _instance: null,
        Instance() {
            return curMod._instance;
        }
    },
    // onLoad () {},
    start() {
        curMod._instance = this;
        this.setStart();
    },

    setStart() {
        //背景1
        this.LoadBgload = false;
        this.picNode = [];
        var self = this;
        var url = "BgPic";//resources下目录
        cc.loader.loadResDir(url, cc.Prefab, function (err, objects, urls) {
            if (err) {
                cc.error('loadMapData', err);
                return;
            }
            self.node.removeAllChildren();
            cc.log(objects);

            for (var j = 0; j < objects.length; j++) {
                var name = objects[j]._name;
                cc.log(name)
                if (name === "bg1") {
                    self.picNode[0] = cc.instantiate(objects[j]);
                    self.picNode[0].parent = self.node;
                } else if (name === "bg2") {
                    self.picNode[1] = cc.instantiate(objects[j]);
                    self.picNode[1].parent = self.node;

                } else if (name === "bg3") {
                    self.picNode[2] = cc.instantiate(objects[j]);
                    self.picNode[2].parent = self.node;
                    self.picNode[3] = cc.instantiate(objects[j]);
                    self.picNode[3].parent = self.node;
                }
            }
            //this.PicWidth:图片宽度,123循环
            self.picNode[3].setPosition(-self.PicWidth, 0);//末尾一张放到最开头
            self.picNode[0].setPosition(0, 0);
            self.picNode[1].setPosition(self.PicWidth, 0);
            self.picNode[2].setPosition(self.PicWidth * 2, 0);
            self.criticalOne = self.picNode[0];
            self.criticalTwo = self.picNode[1];
            self.criticalThree = self.picNode[2];
            self.LoadBgload = true;
        });
    },

    //执行背景移动入口 CCC update函数
    update(dt) {
        //背景随着相机移动切换
        //cc.log(this.camera.x);
        if (this.LoadBgload) {
            if (this.mapCamera.x >= (this.criticalTwo.x + this.PicWidth / 2)) {//图片Anchor为(0,0.5)
                this.criticalOne.setPosition(this.criticalThree.x + this.PicWidth, 0);
                var temp = this.criticalOne;
                this.criticalOne = this.criticalTwo;
                this.criticalTwo = this.criticalThree;
                this.criticalThree = temp;
            }
        }
    },
    lateUpdate() {
        //相机跟随人物移动
        //let mid_x = (this.player1.node.x + this.player2.node.x) / 2;
        let mid_x = this.player.x;
        if (!this._targetPosX) {
            this._targetPosX = mid_x;
        }
        if (this._targetPosX) {
            if ((mid_x - this.mapCamera.node.x) > 0) {
                this.mapCamera.node.x += (mid_x - this.mapCamera.node.x) / this._cameraSpeed;
            }
            else if ((this.mapCamera.node.x - mid_x) > 300) {
                this.mapCamera.node.x -= (this.mapCamera.node.x - mid_x) / this._cameraSpeed;
            }
        }
    },
});



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

烧仙草奶茶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值