CocosLinux学习之路程序员

使用cocos creator制作小游戏总结

2018-04-22  本文已影响253人  xurna

要点

浅拷贝:
var obj1 = {name:'lily',age:12}
var obj2 = obj1
obj2.age = 13 //obj1,obj2的age都变成13

深拷贝:
function copy(obj){
    var newObj = {}
    for(var attr in obj){
        newObj[attr] = obj[attr]
    }
    return newObj
}
var obj1 = {name:'lily',age:12}
var obj2 = copy(obj1)
obj2.age = 13
1.将`cc.Sprite`组件的`type`属性改成`FILLED`
横向loading:
fillType: HORIZONTAL
fillStart:0  //从左边开始
fillRange:0  //填充的百分比,0~1
扇形loading:
fillType: RIDICAL //扇形
fillCenter:(0.5,0.5)//中心点开始
fillStart:0.25  //最上边
fillRange:0   //填充的百分比,负为顺时针,正为逆时针
扇形讲解图:
            ^y
        0.25|              
  0.375 ----|---0.125
        |   |   |
  0.5---|---|---|0----->x
        |   |   |       
   0.625----|---0.875
           0.75
2.通过脚本修改`fillRange`属性即可
cc.loader.onProgress = (completedCount, totalCount, item) => {
   const progress = (completedCount / totalCount).toFixed(2)
   // 顺时针
   this.loading.getComponent('cc.Sprite').fillRange = -progress
   }
 }
1.右键点击cocos creator软件选择“打开包内容”
2.去到文件路径路径:Contents/Resources/static/build-templates/shares/下修改style-mobile.css对应样式
onCollisionEnter (other, self) {
   console.log('on collision enter')
   },
onCollisionStay (other, self) {
  console.log('on collision stay')
},
onCollisionExit (other, self) {
   console.log('on collision exit')
  }

d.并开启碰撞检测:

this.manager = cc.director.getCollisionManager()
// 开启碰撞检测
this.manager.enabled = true
// 开启碰撞边缘绘制调试
manager.enabledDebugDraw = true
// 开启包围盒绘制调试
manager.enabledDrawBoundingBox = true

e.如果需要去掉相同预制节点中不需要进行碰撞检测的节点,可以指定节点关闭

node.getComponent(cc.CircleCollider).enabled = false

难点

// ws模块:
const WS = {
  ws: null,
  wsUrl: null
  },
  _setWsCloseFunc: function () {},
  _setOpenFunc: function () {},
  _setMessageFunc: function () {}
}
// 连接websocket
WS._createWebSocket = function (url) {
  try {
    this.ws = new WebSocket(url)
    this._initEventHandle()
  }
  catch (e) {
    console.log('catch', e)
  }
}

// websocket事件
WS._initEventHandle = function () {
  this.ws.onclose = () => {
    this._setWsCloseFunc()
  }
  this.ws.onerror = (err) => {
  }
  this.ws.onopen = () => {
    this._setOpenFunc()
  }
  this.ws.onmessage = (event) => {
    this._setMessageFunc()
  }
}
module.exports = WS

// 游戏逻辑模块
import WS from './ws'
cc.Class({
  extends: cc.Component,
  properties: {
  },
  onLoad () {
  },
  start () {
    WS._setOpenFunc = this._setOpenFunc.bind(this)
    WS._setMessageFunc = this._setMessageFunc.bind(this)
    WS._setWsCloseFunc = this._setWsCloseFunc.bind(this)
  },
  // onclose
  _setWsCloseFunc () {
  },
  // onopen
  _setOpenFunc () {
  },
  // onmessage
  _setMessageFunc () {
  },

})

node.on(cc.Node.EventType.TOUCH_START, () => { })
this.windowRatio = this.node.width / 720
if (this.node.width < 720) {
  this.node.scale = this.windowRatio
}
...
this._controlTime(turn, timer)
...
_controlTime (turn, timer) {
    timer--
    const timeStamp1 = Date.parse(new Date()) / 1000
    setTimeout(() => {
        const timeStamp2 = Date.parse(new Date()) / 1000
        const timeStamp = parseInt(timeStamp2 - timeStamp1)
        let realTime = 0
        if (timeStamp === 1) {
          realTime = timer
        }
        else {
          realTime = timer - timeStamp
        }
        console.log(timeStamp, realTime)
        this._controlTime(turn, realTime)
    }, 1000)
}

b.在对方回合压后台,一开始记录连接ws的时候用后台的时间戳与本地时间戳做一个校准,表示服务器与客户端相差多少时间deltaTimer,在每个回合变换时间的时候,用后台的时间戳与本地时间戳和矫正时间戳做比较,设置正确的timer。

timer = serviceTimer - localTimer - deltaTimer
上一篇 下一篇

猜你喜欢

热点阅读