水排序游戏道具,-使用CocosCreator + spine制作水排序游戏 - setItemColor(slot, c)

📅 2026-11-30 👤 拼多多助力营销技巧 📂 手赚APP 👁️ 113 次阅读

1. 完成效果

说明

使用spine做倒水逻辑时。水块本质上讲就是获取spine动画的插槽,设置插槽颜色。

2. spine相关

1.倒水倒出的动画(spine_out)

说明:这是一个倒出水的spine动画,其中这部分的动画插槽会在spine部分的结尾一起说明。

注意的是这个动画中需要包含3个骨骼挂点。其中一个是瓶子的挂点(将瓶子的sprite节点挂到这个指定的骨骼上),剩余2个是瓶口左右的挂点(将倒水时瓶口的流水sprite节点挂到这个指定的骨骼上)。这个动画包含n个关键帧事件,分别为每部分的水结束/开始倒出的事件 + 初始帧事件该spine包含左右两个倒出水的动画

2.倒水倒入的动画(spine_in)

说明:这是一个倒入水的spine动画,其中这部分的动画插槽会在spine部分的结尾一起说明。

这个动画包含n个关键事件帧,分别为每部分的水完成倒入的事件这个动画可不需要做骨骼挂点该spine包含一个水面增长动画

3.水面摇晃的动画(spine_wave)

说明:这是一个水面摇晃的动画。

该spine包含一个水面摇晃动画

4.水柱动画(spine_line)

说明:这是一个倒水的水柱动画。

该spine包含2个插槽,瓶口的贴图插槽+水柱的贴图插槽该spine包含3个动画。分别为开始倒水、持续倒水,结束倒水动画

5.倒水水花动画(spine_hua)

说明:无特殊需求,按项目制作即可

6.动画插槽描述(相关动画内容补充,非新spine动画)

说明:

动画共n部分插槽,每部分包含3个插槽,分别为‘顶部切面’、‘顶部与正向的连接线’、‘正向面’。其中顶部切面和正向面的颜色为不同的值可做出更好的水柱效果,连接线颜色为统一的白色高光。

3. 倒水流程描述(瓶A往瓶B倒水)

1.倒出过程(瓶A播放)

倒出过程是播放指定spine(spine_out)的左右倒水动画在播放动画的时候监听spine的帧事件,每次触发事件后将该帧之后的插槽的颜色清除掉(即倒完了一块水)。倒水开始时将动画速率设置为+timeScale,触发了指定事件后(即倒完了该倒出的水,可能是n个水块)将动画速率设置为-timeScale即开始回正,当速率<0且触发初始事件帧时即表示瓶子‘已回正完成’,此时将动画速率设置为0。触发开始倒水的事件帧(即水面抵达瓶口)时,调用瓶B的倒入函数。

2.倒入过程(瓶B播放)

倒入过程是播放B瓶的水柱spine(spine_line)的开始倒水动画,该动画播放到指定帧事件/指定事件后播放倒入spine(spine_in)的倒入动画,播放完成n个阶段后停止动画即为倒入完成。以及整个过程在水面播放水花动画(spine_hua)。这个过程中需要监听事件帧,需要不断更新每个阶段的颜色。

3.水面摇晃动画(瓶A抬起/放下播放)

在瓶子抬起放下时将水面spine(spine_wave)挂载到水面顶部的插槽上。并将颜色设置为顶部水的颜色,播放指定动画。

4.水柱动画(瓶B播放)

在瓶A的水面接触到瓶口时播放(会触发开始倒出水的事件帧)开始倒水的动画并开始播放spine_in的水面增长动画以及spine_hua的水花动画,播放到指定的帧/时间后播放持续的倒水动画。当瓶A的指定水倒出后(完成该次倒水)清除该动画的瓶口插槽颜色,只保留柱体的插槽颜色。播放结束倒水的动画,播放到指定帧/事件后停止水花动画。

5.水花动画(瓶B播放)

在水柱动画播放到指定帧事件/时间后开始播放动画,完成后停止动画需要在增长动画的监听的帧事件中更新节点的骨骼挂点,让水花保持在顶部切面挂点上。

4.主要代码

1.倒入倒出节点基类(skeItem.ts)

import * as cc from 'cc';

import { GameConst } from '../GameConst';

const { ccclass, property } = cc._decorator;

@ccclass('SkeItem')

export class SkeItem extends cc.Component {

@property(cc.sp.Skeleton)

protected ske: cc.sp.Skeleton = null;

protected skeLen: number = 4;

protected waterLen: number = 0;

// 清除所有插槽的颜色

public clearColor() {

this.ske.clearAnimations();

for(const name of GameConst.WATER_SLOTS) {

const slot = this.ske.findSlot(name);

if (slot) { slot.color.a = 0; }

}

this.waterLen = 0;

}

/**

* 获取插槽列表

* @param stage 阶段

* @param idx 第几个

* @returns 插槽列表[]

*/

public getWaterSlots(stage: number, idx: number = null): cc.sp.spine.Slot[] {

let result: cc.sp.spine.Slot[] = [];

if(idx) {

result.push(this.ske.findSlot('s2_lv' + stage + '_' + idx));

} else {

for(let i = 1; i <= 3; i++) {

result.push(this.ske.findSlot('s2_lv' + stage + '_' + i));

}

}

return result;

}

/**

* 设置插槽的颜色

* @param slot 插槽

* @param color 颜色rgb

*/

public setItemColor(slot: cc.sp.spine.Slot, color: any) {

if(!color) return;

if (slot) {

slot.color.r = color.r / 255;

slot.color.g = color.g / 255;

slot.color.b = color.b / 255;

slot.color.a = 1;

}

}

/**

* 清除插槽的颜色

* @param slot 插槽,将颜色aplha设置为0

*/

public setItemDisable(slot: cc.sp.spine.Slot) {

if(slot) {

slot.color.a = 0;

}

}

/**

* 清除阶段的插槽颜色

* @param stage spine阶段

*/

public clearWaterStageColor(stage: number) {

const slots = this.getWaterSlots(stage);

slots.forEach(slot => {

if(slot) {slot.color.a = 0 }

})

}

}

2.倒出spine节点主要逻辑

private _key: string = 'ds';

protected onLoad(): void {

this.ske.timeScale = GameConst.WATER_SPINE_SCALE_1;

}

protected onDisable(): void {

this.ske.clearAnimations();

this.clearSkeEvent();

}

private _start: number = 0;

private _target: number = 0;

private _color: number = 0;

private startCall: Function = null;

private endCall: Function = null;

private moveBack: Function = null;

private _len: number = 0;

// 开始倒水时调用

public playWaterAnim(begin: number, target: number, color: number, {

start = () => {},

end = () => {},

back = () => {},

}) {

this._start = begin;

this._target = target;

this._color = color;

this.startCall = start;

this.endCall = end;

this.moveBack = back;

this.bindSkeEvent();

this._len = target - begin;

this.ske.timeScale = this._timeScale;

}

// 事件帧监听

private onSkeEvent(t: cc.sp.spine.TrackEntry, e: any) {

const _tEvent = this._key + this._target;

const eName = e.data.name;

const num = Number(eName[eName.length - 1]);

if(num < this._target) { this.clearWaterStageColor(num - 1) }; // 把上一个的颜色清除掉

if(num === this._start && this.ske.timeScale > 0) {

this.ske.timeScale = GameConst.WATER_SPINE_SCALE_3 * this.getTimeScale();

this.startCall && this.startCall(); // 开始往外倒水,执行回调

}

if(eName === _tEvent) {

if(this.ske.timeScale > 0) {

this.ske.timeScale = 0;

this.clearWaterStageColor(this._target - 1); // 把上一个阶段的颜色清除掉

this.endCall && this.endCall();

this.moveBack && this.moveBack();

this.clearSkeEvent();

}

}else {

if(num > this._start) { this.setTopColor(num) }; // 把当前阶段的颜色设置上

}

}

// 水不断倒出,更新阶段的水面颜色

private setTopColor(stage: number) {

const slots = this.getWaterSlots(stage); // 获取这个阶段的所有插槽,包含一个正面一个顶部一个连接

slots.forEach(slot => {

let _c = GameMgr.getBodyColor(this._color);// 正面颜色

if(slot && slot.data.name.endsWith('1')) {

_c = {r: 255, g: 255, b: 255}// 连接面固定颜色

} else if(slot && slot.data.name.endsWith('2')) {

_c = GameMgr.getTopColor(this._color);// 顶部颜色

}

this.setItemColor(slot, _c);

});

}

private bindSkeEvent() {

this.ske.setEventListener(this.onSkeEvent.bind(this));

}

private clearSkeEvent() {

this.ske.setEventListener(null);

}

2.倒入spine节点主要逻辑

private _spray: cc.sp.Skeleton = null;// 顶部的水纹spine

protected onEnable(): void {

this._time = 0;

this._putLength = 0;

}

// 更新spine,这个是顶部的水纹spine

public updateSpray(spray: cc.sp.Skeleton) {

this._spray = spray;

this._spray.node.parent.active = false;

}

protected onDisable(): void {

this.ske.clearAnimations();

}

private _oriLen: number = 0;

private _start: number = 0;

private _target: number = 0;

private _color: number = null;

private startCall: Function = null;

private endCall: Function = null;

private _len: number = 0;

private _endEvent: number = 0;

private _putLength: number = 0;

// 倒入的动画执行

public playWaterAnim(name: string, ori: number, begin: number, target: number, color: any, time: number, {

start=()=>{},

end=()=>{}

}) {

this._endEvent = target - begin + ori;

if (this._putLength === 0) {

this.ske.clearAnimations();

this._target = target;

}

this._oriLen = ori;

this._start = begin;

this._color = color;

this.startCall = start;

this.endCall = end;

this._len = this._target - this._start;

if(this._putLength === 0) {

this.clearColor();

this.bindSkeEvent();

this.ske.timeScale = 5; // 时间速率设置为10倍

this.ske.setAnimation(0, name, false);// 二次进入不再需要播放动画

}

this._time = this._time === 1 ? this._time : time;

this._putLength += 1;

}

private onSkeEvent(t: cc.sp.spine.TrackEntry, e: any) {

// const _tEvent = this._key + this._target;

const eName = e.data.name;

const num = Number(eName[eName.length - 1]);

if(num === this._endEvent) {

this.ske.timeScale = 0;

this.endCall && this.endCall();

this.clearSkeEvent();

this.hideSprayAnim();

this._putLength = 0;

return;

}

const stage = this.skeLen - num;

if(this.skeLen - (this.skeLen - this._oriLen) === num) {

this.ske.timeScale = GameConst.WATER_SPINE_SCALE_3 * this.getTimeScale(); // 在增长区域了,将时间速率设置回来

this.startCall && this.startCall();

}

if (stage > this._start && stage <= this._target) {

this.setColor(stage);

this.playSprayAnim(stage);

}

}

// 更新阶段的插槽颜色

private setColor(s: number) {

const slots = this.getWaterSlots(s);

slots.forEach(slot => {

if(slot.data.name.endsWith('1')) {

this.setItemColor(slot, {r: 255, g: 255, b: 255});

} else if(slot.data.name.endsWith('2')) {

this.setItemColor(slot, GameMgr.getTopColor(this._color));

} else {

this.setItemColor(slot, GameMgr.getBodyColor(this._color));

}

})

}

// 播放顶部水花动画,帧事件触发时即执行,内部处理更新挂点

private playSprayAnim(s: number) {

const list2 = ['', 'root/all/bone/bone2/shui4/shuimian4', 'root/all/bone/bone2/shui3/shuimian3', 'root/all/bone/bone2/shui2/shuimian2', 'root/all/bone/bone2/shui1/shuimian'];

const socket = new cc.sp.SpineSocket(list2[s], this._spray.node.parent);

this.ske!.sockets.length = 0; // 清空之前的挂点

this.ske!.sockets.push(socket);

this.ske!.sockets = this.ske!.sockets;

if(this._spray) this._spray.node.parent.active = true;

this.setSprayColor();

this._spray?.setAnimation(0, 'shuihua', true);

}

private _time: number = 0;

public hideSprayAnim() {

this._time -= 1;

if(this._time > 0) return;

this._time = 0;

this._spray?.clearAnimations();

if(this._spray) this._spray.node.parent.active = false;

}

// 更新顶部水花的颜色

private setSprayColor() { 拼多多帮助力入口

const solts = this.getSpraySlots();

solts.forEach(slot => {

this.setItemColor(slot, GameMgr.getTopColor(this._color));

})

}

// 获取水花的插槽

private getSpraySlots() {

const names = ['s2shuihua', 's2_bw_16', 's2_bw_6', 's2_bw_1'];

return names.map(name => this._spray?.findSlot(name));

}

private bindSkeEvent() {

this.ske.setEventListener(this.onSkeEvent.bind(this));

}

private clearSkeEvent() {

this.ske.setEventListener(null);

}

3.瓶子脚本主要逻辑

@property(WaterSke)

private waterSke: WaterSke = null; // 常驻/倒出水的spine

@property(TopSke)

private waterTopSke: TopSke = null; // 倒入时的水

@property(cc.sp.Skeleton)

private waveSke: cc.sp.Skeleton = null; // 顶部的水面

@property(cc.sp.Skeleton)

private waterLine: cc.sp.Skeleton = null; // 水柱

@property(cc.sp.Skeleton)

private spray: cc.sp.Skeleton = null; // 水面的倒水水花

// 更新水UI

private updateWater(stageColor: any[], push: boolean = true) {

// 同步spine的动画

for(let iStr in stageColor) {

const i = parseInt(iStr);

const colorId = stageColor[i].color;

const stage = this.waterMax - i;

const waters: cc.sp.spine.Slot[] = this.waterSke.getWaterSlots(stage);

const color = GameMgr.getBodyColor(colorId);

const isSame = stageColor[i + 1] && stageColor[i + 1].color == colorId;

const isEnd = i === stageColor.length - 1;

waters.forEach(water => {

const end1 = water.data.name.endsWith('1');

const end2 = water.data.name.endsWith('2');

if (isSame && (end1 || end2)) {

this.waterSke.setItemDisable(water);

} else if (isEnd && end1) { // 顶部的颜色,且是顶部水面边缘

this.waterSke.setItemColor(water, { r: 255, g: 255, b: 255 });

} else if(isEnd && end2) { // 顶部的颜色,且是顶部水面的颜色

this.waterSke.setItemColor(water, GameMgr.getTopColor(colorId));

} else {

this.waterSke.setItemColor(water, color);

}

})

if(!push) continue;

const _status: IWaterStatus = { color: colorId }

this.waters.push(_status);

}

this.resetWaveSke(true);

}

// top和水柱的alpha都设置为0

private clearWaters() {

this.waterSke.clearColor();

this.waterTopSke.clearColor();

this.resetWaveSke(true);

}

// 清除空掉的水柱(即将空水的颜色alpha = 0)

public cleanEmptyWater(len: number) {

const stage = this.waterMax - this.waters.length;

for(let i = 0; i< len; i++) {

let s = stage - (i);

this.waterSke.clearWaterStageColor(s);

}

if(this.waters.length > 0) {

this.updateWaterTop();

}

}

// 更新颜色

public updateColors(colors: Array) {

let cfg = [];

for(let i = 0; i < colors.length; i++) {

const color = colors[i];

const idx = this.waters.findIndex((w) => w.color === color);

cfg.push({color});

this.waters.splice(idx, 1);

}

cfg[cfg.length - 1].isHide = false;

this.initCup(cfg);

}

private _moveQueue = new AsyncQueue();

// 水往外倒

public putOutWater(cup: CupItem, waters: any[], call: Function, backCall: Function) {

let isRight = false;

const pr = this._pos.x - cup._pos.x >= 0;

isRight = pr;

// 根据缩放动态调整位移

const x = 130 * GameConst.CUP_SCLAE;

const y = 70 * GameConst.CUP_SCLAE;

const target1 = cc.v3(cup._pos.x + (isRight ? x : -x), cup._pos.y + y, 0);

const color = waters[0];

this.node.setSiblingIndex(-1);

this._moveQueue.clear();

cc.Tween.stopAllByTarget(this.node);

this.cleanWaveAnim();

this.itemStatus = ECupStatus.PutOut;

cup.itemStatus = ECupStatus.PutOn;

const t = 0.3;

this.waterSke.playMoveAnim(t + 0.1, isRight, (this.waterMax + 1 - this.waters.length));

this._moveQueue.push((next: NextFunction) => {

cc.tween(this.node)

.to(t, {position: target1})

.call(next)

.start();

})

this._moveQueue.complete = () => {

const t = (this.waterMax + 1) - (this.waters.length - waters.length);

this.waterSke.playWaterAnim(isRight ? 'left' : 'right', (this.waterMax - this.waters.length + 1), t, color, {

start: () => {

const c = GameMgr.getTopColor(waters[0]); // GameConst.COLOR_LIST[waters[0]];

const tNode = isRight ? this.left : this.right;

tNode.color = cc.Color.fromVec4(cc.v4(c.r / 255, c.g / 255, c.b / 255, 1));

tNode.node.active = true;

cup.playWaterAnim(c, isRight, () => {

cup.putOnWater(waters, 2);

});

},

end: () => {

cup.stopWaterLineAnim(() => {

call && call();

this.updateWaterTop();

cup.waterTopSke.hideSprayAnim();

});

const tNode = isRight ? this.left : this.right;

tNode.node.active = false;

},

back: () => { this.moveBack(waters.length, backCall) }

});

}

this._moveQueue.play();

}

// 节点移动回去

private moveBack(len: number, endCall: Function) {

// const fakeT = this.node.getPosition().subtract(this._pos).length() / GameConst.CUP_MOVE_SPEED;

// const t = fakeT < GameConst.MOVE_TIME_MIN ? GameConst.MOVE_TIME_MIN : fakeT;

const t = 0.3;

const waterLen = this.waters.length - len;

this.waterSke.playMoveBackAnim(t, (this.waterMax + 1 - waterLen));

cc.Tween.stopAllByTarget(this.node);

cc.tween(this.node)

.delay(0.1)

.to(t, {position: this._pos})

.call(() => {

this.cancelSelect(true);

this.itemStatus = ECupStatus.Normal;

endCall && endCall(() => {

this.waterWaveAnim();// 水花动画

});

})

.start();

}

// 更新顶部颜色

private updateWaterTop() {

if(this.waters.length <= 0) return;

const stage = this.waterMax - (this.waters.length - 1);

const info = this.waters[this.waters.length - 1];

const color = info.color;

const waters: cc.sp.spine.Slot[] = this.waterSke.getWaterSlots(stage);

waters.forEach(slot => {

let c = GameMgr.getBodyColor(color); // GameConst.COLOR_LIST[color];

if(slot.data.name.endsWith('1')) {

c = {r: 255, g: 255, b: 255}

} else if(slot.data.name.endsWith('2')) {

c = GameMgr.getTopColor(color);

}

this.waterSke.setItemColor(slot, c);

})

}

// 清除顶部颜色

private clearWaterTop(stage: number) {

const waters = this.waterSke.getWaterSlots(stage);

waters.forEach(slot => {

if(slot && (slot.data.name.endsWith('1') || slot.data.name.endsWith('2'))) {

this.waterSke.setItemDisable(slot);

}

})

}

// 倒入

public putOnWater(water: any[], t: number = 2) {

this.putOn(water);

this._memWaterAdd = true;

const name = 'stay';

const colorId = water[0]; // GameConst.COLOR_LIST[water[0]];

const begin = this.waterMax - this.waters.length;

const target = begin + water.length;

const originLen = this.waters.length - water.length;

// console.log("_____: ", begin, target, originLen);

this.waterTopSke.playWaterAnim(name, originLen, begin, target, colorId, t, {

start: () => { this.clearWaterTop(this.waterMax - (originLen - 1)); },

end: () => {

this._memWaterAdd = false;

this._onLen = 0;

this.putOnEnd();

},

})

}

// 倒入完成

private putOnEnd() {

this.updateWater(this.waters, false);

this.waterTopSke.clearColor();

this.itemStatus = ECupStatus.Normal;

}

// 重置水花颜色

private resetWaveSke(isClean: boolean = true) {

const slots = this.getWaveSlots();// 插槽

const color = this.waters[this.waters.length - 1]?.color;// 获得到顶部的颜色id

if(!color && color != 0) isClean = true;

slots.forEach(s => {

if (s) {

let _c = GameMgr.getBodyColor(color); // GameConst.COLOR_LIST[color];

if (s.data.name.endsWith('1')) {

_c = { r: 255, g: 255, b: 255 }; //结束1的是边缘光效

} else if(s.data.name.endsWith('2')) {

_c = GameMgr.getTopColor(color);

}

if (isClean) {

s.color.a = 0;

} else {

s.color.r = _c.r / 255;

s.color.g = _c.g / 255;

s.color.b = _c.b / 255;

s.color.a = 1;

}

}

});

}

private getWaveSlots() {

const names = ['s2_lv4_1', 's2_lv4_2', 's2_lv4_3'];

return names.map(name => this.waveSke.findSlot(name));

}

private _waterLine: number = 0;

// 这是播放倒水水柱的动画

private playWaterAnim(color: any, right: boolean, call?: Function) {

this._waterLine += 1;

if (this._waterLine === 1) {

this.waterLine.clearAnimations();

}

this.waterLine.node.active = true;

this.waterLine.node.setScale(cc.v3(right ? -1 : 1, 1));

this.resetWaterLine(color);

let t = (GameConst.LINE_WAIT_FRAME[this.waters.length]) * 0.016;

if (t < 0.016) {

call && call();

} else {

this.scheduleOnce(call, t);

}

if (this._waterLine === 1) {

this.waterLine.setAnimation(0, 'star', false);

this.waterLine.addAnimation(0, 'stay', true);

}

}

// 结束倒水的水柱动画

private stopWaterLineAnim(call?: Function) {

const t = (GameConst.LINE_WAIT_FRAME[this.waters.length]) * 0.016;

this.scheduleOnce(() => {

call && call();

this._waterLine--;

if(this._waterLine != 0) return;

this.resetWaterLine(null, true);

}, t);

if(this._waterLine != 0) return;

this.waterLine.setCompleteListener(() => {

this.waterLine.setCompleteListener(null);

this.waterLine.node.active = false;

})

const top = this.waterLine.findSlot('s2_shuitiao2');

if(top) { top.color.a = 0 };

this.waterLine.setAnimation(0, 'over', false);

}

private resetWaterLine(color: any, isHide: boolean = false) {

const slots = this.getWaterLineSlots();

slots.forEach(s => {

if (s) {

s.color.r = isHide ? 0 : color.r / 255;

s.color.g = isHide ? 0 : color.g / 255;

s.color.b = isHide ? 0 : color.b / 255;

s.color.a = isHide ? 0 : 1;

}

})

}

private getWaterLineSlots() {

const names = ['s2_shuitiao2', 's2_shuitiao'];

return names.map(name => this.waterLine.findSlot(name));

}

// 瓶子移动

public playMoveAnim(call?: Function, easing: any = cc.easing.circOut) {

let time = this.node.getPosition().subtract(this._pos).length() / (GameConst.CUP_MOVE_SPEED * 1.5);

cc.Tween.stopAllByTarget(this.node);

cc.tween(this.node)

.to(time, {position: this._pos, scale: cc.v3(GameConst.CUP_SCLAE, GameConst.CUP_SCLAE, GameConst.CUP_SCLAE)}, {easing: easing})

.call(() => { call && call(); })

.start();

}

杂谈

距离上次发文章已经过去整整一年了。这一年经历了几个项目却只整理出这一篇文章,究其原因还是因为自己的懈怠。未来我将更加专注于记录和总结每一个项目的进展与经验,将更多的过程和思考分享给大家。最后感谢大家的阅读,祝大家在技术的道路上越走越远!文内对与不对的地方,可以添加下面微信与我交流。

标签: 拼多多助力网站平台 赚钱项目 拼多多助力网 首码项目 手赚APP
拼多多助力营销技巧