Skip to content

Commit

Permalink
fix: add protect (#166)
Browse files Browse the repository at this point in the history
* fix: protect
* fix: merge condition
  • Loading branch information
fanmingfei authored Dec 8, 2021
1 parent e7336b7 commit 2ff8f94
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 7 deletions.
4 changes: 4 additions & 0 deletions packages/eva.js/lib/core/GameObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,10 @@ class GameObject {

/** Destory this gameObject */
destroy() {
if (!this.transform) {
console.error('Cannot destroy gameObject that have already been destroyed.')
return
}
Array.from(this.transform.children).forEach(({ gameObject }) => {
gameObject.destroy();
});
Expand Down
6 changes: 3 additions & 3 deletions packages/eva.js/lib/core/observer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ function removeObjectCache(component: Component) {
* @param {ObserverType} param0.type - observer type
*/
function addObserver({ systemName, componentName, component, prop, type }: ObserverEvent) {
systemInstance[systemName].componentObserver.add({
systemInstance[systemName]?.componentObserver?.add({
component,
prop,
type,
Expand Down Expand Up @@ -304,7 +304,7 @@ export function observerAdded(component: Component, componentName: string = comp
const observerInfo = observerInfos[systemName] || {};
const info = observerInfo[componentName];
if (info) {
systemInstance[systemName].componentObserver.add({
systemInstance[systemName]?.componentObserver?.add({
component,
type: ObserverType.ADD,
componentName,
Expand All @@ -323,7 +323,7 @@ export function observerRemoved(component: Component, componentName: string = co
const observerInfo = observerInfos[systemName] || {};
const info = observerInfo[componentName];
if (info) {
systemInstance[systemName].componentObserver.add({
systemInstance[systemName]?.componentObserver?.add({
component,
type: ObserverType.REMOVE,
componentName,
Expand Down
3 changes: 2 additions & 1 deletion packages/plugin-renderer-img/lib/system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ export default class Img extends Renderer {
this.imgs[changed.gameObject.id].image = instance;
} else if (changed.type === OBSERVER_TYPE.REMOVE) {
const sprite = this.imgs[changed.gameObject.id];
this.containerManager.getContainer(changed.gameObject.id).removeChild(sprite.sprite);
if (!sprite) return
this.containerManager?.getContainer(changed.gameObject.id)?.removeChild(sprite.sprite);
sprite.sprite.destroy({ children: true });
delete this.imgs[changed.gameObject.id];
}
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-renderer/lib/manager/ContainerManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default class ContainerManager {
}
updateTransform({ name, transform }: { name: number; transform: Transform }) {
const container = this.containerMap[name];
if (!container) return;
if (!container || !transform) return;
const { anchor, origin, position, rotation, scale, size, skew } = transform;
container.rotation = rotation;
container.scale = scale as Point;
Expand Down
4 changes: 2 additions & 2 deletions packages/spine-base/lib/SpineSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export default class SpineSystem extends Renderer {
return;
}
this.remove(changed);
const container = this.renderSystem.containerManager.getContainer(changed.gameObject.id);
const container = this.renderSystem?.containerManager?.getContainer(changed.gameObject.id);
if (!container) {
// console.warn('添加spine的container不存在');
return;
Expand Down Expand Up @@ -164,7 +164,7 @@ export default class SpineSystem extends Renderer {
clearTimeout(component.addHandler);
const armature = this.armatures[changed.gameObject.id];

const container = this.renderSystem.containerManager.getContainer(changed.gameObject.id);
const container = this.renderSystem?.containerManager?.getContainer(changed.gameObject.id);
if (container && armature) {
container.removeChild(armature);
} else {
Expand Down

0 comments on commit 2ff8f94

Please sign in to comment.