cocos creator3.0 见缝插针
效果展示
## gif 帧率较低 实际体验效果更好
实现思路
1、中间一个节点 里面有两个node 分别放背景橘子node,存放刀的node
然后只旋转这个中间节点让刀和背景一块旋转就行了
2、碰撞检测给每一把刀放上碰撞检测
3、下边放一把假的刀 用来做动画执行,刀子到达位置 将假的刀放回原来的位置,将复制新的刀到中间存刀的节点
4、判断下一关 将中间节点存刀的node判断子节点数量
5、管卡难度
·匀速
·变速
·变速+变向
·时间限制
关键代码
fire(event: any, data: any) {
// 游戏结束 或者当前没有刀不能点击
if (!this.gameStatus || this.knifeCount <= 0) {
return
}
// 当前刀的数量减少
this.knifeCount--;
if (this.knife) {
let p = this.knife.getPosition()
p.x = 0;
p.y = 0;
if (this.knifeSize) {
p.y += this.knifeSize.height / 2;
}
let that = this
/**
* 执行线性动画
* 动画结束复制当前动画的刀到当前位置
*/
this.currentKnifeAnim = tween(this.knife)
.to(0.1, { position: p }, {
easing: 'linear',
onComplete: function (target?: object) {
let node = instantiate(that.knife)
if (node) {
// 设置刀的位置 到初始位置
that.knife?.setPosition(that.knifeX, that.knifeY, 0)
// 执行橘子动画 向上弹
that.orangeNode?.getComponent(AnimationComponent)?.play()
let px = 0, py = 0, ox = 0, oy = 0;
if (that.knifeSize) {
// 设置刀插进去的位置
py = that.knifeSize.height / 2 - that.orangeY;
}
/**
* 2D上的点围绕某另一个点旋转: If you rotate point (px, py) around point (ox, oy) by angle theta you’ll get:
* p'x = cos(theta) * (px-ox) - sin(theta) * (py-oy) + ox
* p'y = sin(theta) * (px-ox) + cos(theta) * (py-oy) + oy
*/
// 计算角度 要一直为下方 注意补角
let temp = 360 - that.angle;
let theta = Math.PI / 180 * temp
let x = Math.cos(theta) * (px - ox) - Math.sin(theta) * (py - oy) + ox
let y = Math.sin(theta) * (px - ox) + Math.cos(theta) * (py - oy) + oy
// 设置新刀位置
node.setPosition(x, y, 0)
// 设置新刀角度
node.setWorldRotationFromEuler(0, 0, temp)
// 附加到虚拟转盘上
node.parent = that.discNode
}
}
})
.start()
}
}
试玩地址(加载慢,自己服务器带宽低)
https://www.intkilow.top/jfcz/
下载地址
关注微信公众号:
随疾风前行
回复:
cocos见缝插针游戏源码