+1 vote
by (1.4k points)

Hello peoples!

I am using SugarCube 2.21.0, displaying images like so:

[img[image-url]]

Is there something that can make a sound play when the image is clicked?

Thank you in advance!

Patrick

2 Answers

0 votes
by (63.1k points)
selected by
 
Best answer

Assuming you have the sound all setup up properly using <<cacheaudio>> et al, just use a <<link>> macro.

<<link [img[image-url]]>>
    <<audio 'track-name' play>>
<</link>>

 

by (1.4k points)
Thank you!

I was concerned this would make the passage reload or have to go to another passage, but wonderfully not.

Is there a way to have the image be replaced by another image without reloading or changing the passage?
0 votes
by (23.6k points)

Use the <<link>> macro and just put the audio macro you need in there. Like this:

<<link [img[yourimage.jpg][PassageName]]>>
<<audio "your_audio" play>>
<</link>>

Don't forget to add the audio via  <<cacheaudio trackId sourceList>> in StoryInit.

by (1.4k points)
Is there a way to make audio play without going to another passage?

I have seen links that are replaced with text when clicked, and thought they did this without going to another passage, but perhaps I was mistaken.
by (2.7k points)
Your looking for the <<clickreplace>> macro
by (1.4k points)
How does one use the <<clickreplace>> macro? As in, how does one indicate what is to be replaced?
by (23.6k points)
edited by

You dont need that - if you don't want it be a link, just write:

<<link [img[yourimage.jpg]]>>
<<audio "your_audio" play>>
<</link>>

I know it says link - but it will just execute the content in between, without actually sending the player anywhere. - The example you are talking about - where a link is just replaced by clicking, also uses the <<link>> macro and combines it with the <<replace>> macro like this:

<<nobr>>
<span id = "stuff">
<<link "Initial Text">>
	<<replace "#stuff">>
		Text Afterwards
	<</replace>>
<</link>>
</span>
<</nobr>>

 

by (1.4k points)
Yep, that was what Chapel said, and I am very happy with that solution indeed.

I was still wondering about <<clickreplace>> though, in case it might be a way to have an image be replaced by another image without leaving the passage, as well as playing the sound. I tried looking it up, but did not find it in the SugarCube macro library.
by (23.6k points)
edited by

Then you just combine <<link>> and <<replace>> like I showed above.

If you want to be able to switch back and forth or similar with each click, you'd have to use a widget, like this:

<<widget imgChange>><<nobr>>
<span id = "test">
<<capture $args[0], $args[1]>>
<<link $args[1]>>
	<<replace "#test">>
		<<imgChange $args[1] $args[0]>>
	<</replace>>
<</link>>
<</capture>>
</span>
<</nobr>><</widget>>

 

by (1.4k points)
Wonderful! That should work I believe. Thank you for the help!
by (2.7k points)

Sorry I meant <<linkreplace>> - <<clickreplace>> is the sugarcube V1 version.
Here is an example:

<<linkreplace "Title">><<dosomething>>Text<</linkreplace>>

Where <<dosomething>> can be replaced by any macro and "Text" by any text. "Title" will be the title of the link.

by (1.4k points)
So you would put the first image in as the link title, and when that image is clicked it would be replaced by an image you would put in as the text, and the audio macro would play audio, correct?
by (2.7k points)

Correct. If you want the image to disappear or be replaced by another, then this is the correct solution. If you want it to remain, and be able to be clicked multiple times, use Chapel's answer.

by (1.4k points)
Thank you! It is good to have more than one thing to try.
...