I would do it like this:
import * as ecs from '@8thwall/ecs'
ecs.registerComponent({
name: 'Pause Audio on Lost',
schema: {
imageTargetName: ecs.string,
},
schemaDefaults: {
imageTargetName: 'My Image Target',
},
stateMachine: ({world, eid, schemaAttribute, dataAttribute}) => {
ecs.defineState('default')
.initial()
.listen(world.events.globalId, 'reality.imagefound', (e) => {
const {imageTargetName} = schemaAttribute.get(eid)
const {name} = e.data as any
if (name === imageTargetName) {
ecs.Audio.mutate(world, eid, (c) => {
c.paused = false
})
}
})
.listen(world.events.globalId, 'reality.imagelost', (e) => {
const {imageTargetName} = schemaAttribute.get(eid)
const {name} = e.data as any
if (name === imageTargetName) {
ecs.Audio.mutate(world, eid, (c) => {
c.paused = true
})
}
})
},
})
You’d attach this component to the same entity that has the Audio component and also make sure to give it the correct name of the Image Target in the Inspector.