// 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;
}
}
},
});
2d横板游戏,背景轮换,相机跟随人物
最新推荐文章于 2024-08-28 15:56:12 发布