Skip to content

Commit a197806

Browse files
fix: assign transform to a destroyed container problem
1 parent fd35764 commit a197806

7 files changed

Lines changed: 45 additions & 25 deletions

File tree

packages/webgal/src/Core/controller/stage/pixi/PixiController.ts

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export interface IStageObject {
3434
uuid: string;
3535
// 一般与作用目标有关
3636
key: string;
37-
pixiContainer: WebGALPixiContainer;
37+
pixiContainer: WebGALPixiContainer | null;
3838
// 相关的源 url
3939
sourceUrl: string;
4040
sourceExt: string;
@@ -235,7 +235,7 @@ export default class PixiStage {
235235
const targetPixiContainer = this.getStageObjByKey(target);
236236
if (targetPixiContainer) {
237237
const container = targetPixiContainer.pixiContainer;
238-
PixiStage.assignTransform(container, effect.transform);
238+
if (container) PixiStage.assignTransform(container, effect.transform);
239239
}
240240
return;
241241
}
@@ -776,6 +776,7 @@ export default class PixiStage {
776776
const figureRecordTarget = this.live2dFigureRecorder.find((e) => e.target === key);
777777
if (target && figureRecordTarget?.motion !== motion) {
778778
const container = target.pixiContainer;
779+
if (!container) return;
779780
const children = container.children;
780781
for (const model of children) {
781782
let category_name = motion;
@@ -799,6 +800,7 @@ export default class PixiStage {
799800
if (target?.sourceType !== 'spine') return;
800801

801802
const container = target.pixiContainer;
803+
if (!container) return;
802804
// Spine figure 结构: Container -> Sprite -> Spine
803805
const sprite = container.children[0] as PIXI.Container;
804806
if (sprite?.children?.[0]) {
@@ -825,6 +827,7 @@ export default class PixiStage {
825827
const figureRecordTarget = this.live2dFigureRecorder.find((e) => e.target === key);
826828
if (target && figureRecordTarget?.expression !== expression) {
827829
const container = target.pixiContainer;
830+
if (!container) return;
828831
const children = container.children;
829832
for (const model of children) {
830833
// @ts-ignore
@@ -840,6 +843,7 @@ export default class PixiStage {
840843
const figureRecordTarget = this.live2dFigureRecorder.find((e) => e.target === key);
841844
if (target && !isEqual(figureRecordTarget?.blink, blinkParam)) {
842845
const container = target.pixiContainer;
846+
if (!container) return;
843847
const children = container.children;
844848
let newBlinkParam: BlinkParam = { ...baseBlinkParam, ...blinkParam };
845849
// 继承现有 BlinkParam
@@ -860,6 +864,7 @@ export default class PixiStage {
860864
const figureRecordTarget = this.live2dFigureRecorder.find((e) => e.target === key);
861865
if (target && !isEqual(figureRecordTarget?.focus, focusParam)) {
862866
const container = target.pixiContainer;
867+
if (!container) return;
863868
const children = container.children;
864869
let newFocusParam: FocusParam = { ...baseFocusParam, ...focusParam };
865870
// 继承现有 FocusParam
@@ -883,6 +888,7 @@ export default class PixiStage {
883888
const target = this.figureObjects.find((e) => e.key === key);
884889
if (target && target.sourceType === 'live2d') {
885890
const container = target.pixiContainer;
891+
if (!container) return;
886892
const children = container.children;
887893
for (const model of children) {
888894
// @ts-ignore
@@ -925,20 +931,28 @@ export default class PixiStage {
925931
const indexBg = this.backgroundObjects.findIndex((e) => e.key === key);
926932
if (indexFig >= 0) {
927933
const bgSprite = this.figureObjects[indexFig];
928-
for (const element of bgSprite.pixiContainer.children) {
929-
element.destroy();
934+
if (bgSprite.pixiContainer)
935+
for (const element of bgSprite.pixiContainer.children) {
936+
element.destroy();
937+
}
938+
if (bgSprite.pixiContainer) {
939+
bgSprite.pixiContainer.destroy();
940+
this.figureContainer.removeChild(bgSprite.pixiContainer);
930941
}
931-
bgSprite.pixiContainer.destroy();
932-
this.figureContainer.removeChild(bgSprite.pixiContainer);
942+
bgSprite.pixiContainer = null;
933943
this.figureObjects.splice(indexFig, 1);
934944
}
935945
if (indexBg >= 0) {
936946
const bgSprite = this.backgroundObjects[indexBg];
937-
for (const element of bgSprite.pixiContainer.children) {
938-
element.destroy();
947+
if (bgSprite.pixiContainer)
948+
for (const element of bgSprite.pixiContainer.children) {
949+
element.destroy();
950+
}
951+
if (bgSprite.pixiContainer) {
952+
bgSprite.pixiContainer.destroy();
953+
this.backgroundContainer.removeChild(bgSprite.pixiContainer);
939954
}
940-
bgSprite.pixiContainer.destroy();
941-
this.backgroundContainer.removeChild(bgSprite.pixiContainer);
955+
bgSprite.pixiContainer = null;
942956
this.backgroundObjects.splice(indexBg, 1);
943957
}
944958
// /**

packages/webgal/src/Core/controller/stage/pixi/animations/testblur.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export function generateTestblurAnimationObj(targetKey: string, duration: number
1010
* 在此书写为动画设置初态的操作
1111
*/
1212
function setStartState() {
13-
if (target) {
13+
if (target?.pixiContainer) {
1414
target.pixiContainer.alpha = 0;
1515
// @ts-ignore
1616
target.pixiContainer.blur = 0;
@@ -22,7 +22,7 @@ export function generateTestblurAnimationObj(targetKey: string, duration: number
2222
* 在此书写为动画设置终态的操作
2323
*/
2424
function setEndState() {
25-
if (target) {
25+
if (target?.pixiContainer) {
2626
target.pixiContainer.alpha = 1;
2727
// @ts-ignore
2828
target.pixiContainer.blur = 5;
@@ -40,9 +40,10 @@ export function generateTestblurAnimationObj(targetKey: string, duration: number
4040
const currentAddOplityDelta = (duration / baseDuration) * delta;
4141
const increasement = 1 / currentAddOplityDelta;
4242
const decreasement = 5 / currentAddOplityDelta;
43-
if (container.alpha < 1) {
44-
container.alpha += increasement;
45-
}
43+
if (container)
44+
if (container.alpha < 1) {
45+
container.alpha += increasement;
46+
}
4647
// @ts-ignore
4748
if (container.blur < 5) {
4849
// @ts-ignore

packages/webgal/src/Core/controller/stage/pixi/animations/timeline.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ export function generateTimelineObj(
9292
* 在此书写为动画设置终态的操作
9393
*/
9494
function setEndState() {
95+
if (!container) {
96+
return;
97+
}
9598
if (animateInstance) animateInstance.stop();
9699
animateInstance = null;
97100
if (target?.pixiContainer) {

packages/webgal/src/Core/controller/stage/pixi/animations/universalSoftIn.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export function generateUniversalSoftInAnimationObj(targetKey: string, duration:
1212
*/
1313
function setStartState() {
1414
elapsedTime = 0; // Reset timer when animation starts
15-
if (target) {
15+
if (target?.pixiContainer) {
1616
// 修正:不再强制设为 0,而是记录当前的透明度
1717
startAlpha = target.pixiContainer.alpha;
1818
}
@@ -22,7 +22,7 @@ export function generateUniversalSoftInAnimationObj(targetKey: string, duration:
2222
* 在此书写为动画设置终态的操作
2323
*/
2424
function setEndState() {
25-
if (target) {
25+
if (target?.pixiContainer) {
2626
// 终态是完全不透明,这保持不变
2727
target.pixiContainer.alpha = 1;
2828
}
@@ -49,7 +49,7 @@ export function generateUniversalSoftInAnimationObj(targetKey: string, duration:
4949
// 公式:最终值 = 初始值 + (目标值 - 初始值) * 进度
5050
// 在这里,目标值是 1,所以公式为:
5151
// alpha = startAlpha + (1 - startAlpha) * easedProgress
52-
sprite.alpha = startAlpha + (1 - startAlpha) * easedProgress;
52+
if (sprite) sprite.alpha = startAlpha + (1 - startAlpha) * easedProgress;
5353
}
5454
}
5555

packages/webgal/src/Core/controller/stage/pixi/animations/universalSoftOff.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export function generateUniversalSoftOffAnimationObj(targetKey: string, duration
1212
*/
1313
function setStartState() {
1414
elapsedTime = 0; // 重置计时器
15-
if (target) {
15+
if (target?.pixiContainer) {
1616
// 修正:不再强制设为1,而是记录当前的透明度
1717
startAlpha = target.pixiContainer.alpha;
1818
}
@@ -22,7 +22,7 @@ export function generateUniversalSoftOffAnimationObj(targetKey: string, duration
2222
* 在此书写为动画设置终态的操作
2323
*/
2424
function setEndState() {
25-
if (target) {
25+
if (target?.pixiContainer) {
2626
// 终态是完全透明,这保持不变
2727
target.pixiContainer.alpha = 0;
2828
}
@@ -50,7 +50,7 @@ export function generateUniversalSoftOffAnimationObj(targetKey: string, duration
5050
// 在这里,目标值是 0,所以公式简化为:
5151
// alpha = startAlpha + (0 - startAlpha) * easedProgress
5252
// alpha = startAlpha * (1 - easedProgress)
53-
targetContainer.alpha = startAlpha * (1 - easedProgress);
53+
if (targetContainer) targetContainer.alpha = startAlpha * (1 - easedProgress);
5454
}
5555
}
5656

packages/webgal/src/Core/gameScripts/changeFigure.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,12 @@ export function changeFigure(sentence: ISentence): IPerform {
196196
}
197197
};
198198

199-
function setFigureData() {
199+
function postFigureStateSet() {
200200
if (isUrlChanged) {
201201
// 当 url 发生变化时,即发生新立绘替换
202202
// 应当赋予一些参数以默认值,防止从旧立绘的状态获取数据
203+
// 并且关闭一些 hold 动画
204+
WebGAL.gameplay.performController.unmountPerform(`animation-${key}`, true);
203205
bounds = bounds ?? [0, 0, 0, 0];
204206
blink = blink ?? cloneDeep(baseBlinkParam);
205207
focus = focus ?? cloneDeep(baseFocusParam);
@@ -236,7 +238,7 @@ export function changeFigure(sentence: ISentence): IPerform {
236238
*/
237239
const freeFigureItem: IFreeFigure = { key, name: content, basePosition: pos };
238240
setAnimationNames(key, sentence);
239-
setFigureData();
241+
postFigureStateSet();
240242
dispatch(stageActions.setFreeFigureByKey(freeFigureItem));
241243
} else {
242244
/**
@@ -255,7 +257,7 @@ export function changeFigure(sentence: ISentence): IPerform {
255257

256258
key = positionMap[pos];
257259
setAnimationNames(key, sentence);
258-
setFigureData();
260+
postFigureStateSet();
259261
dispatch(setStage({ key: dispatchMap[pos], value: content }));
260262
}
261263

packages/webgal/src/Stage/MainStage/useSetFigure.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export function useSetFigure(stageState: IStageState) {
6363
useEffect(() => {
6464
Object.entries(figureMetaData).forEach(([key, value]) => {
6565
const figureObject = WebGAL.gameplay.pixiStage?.getStageObjByKey(key);
66-
if (figureObject && !figureObject.isExiting && value?.zIndex !== undefined) {
66+
if (figureObject && !figureObject.isExiting && value?.zIndex !== undefined && figureObject.pixiContainer) {
6767
figureObject.pixiContainer.zIndex = value.zIndex;
6868
}
6969
});

0 commit comments

Comments
 (0)