It looks like you're new here. If you want to get involved, click one of these buttons!
$("a").mouseenter(function(){
$("<audio></audio>").attr({
'src':'sound/transition1.ogg',
'autoplay':'autoplay'
})
});
Taken from: https://css-tricks.com/play-sound-on-hover/
Comments
<a>
elements existing in the page when it's executed. If I understand what you want, you'll need to use a delegated handler.For example:
Is it possible to exclude particular links from that function?
Like I have different styled links in the form of as well as Where I might go <span class="newlink">[[my link]] </span>
I wouldn't want the sound on the newlink styled links in this example.
Although, in practice it doesn't sound quote as good as I'd hoped, so, might not use it anyway.
PassageReady
&PassageDone
special passages.Yes. You'd simply use a different selector, instead of
"a"
.I wouldn't want the sound on the newlink styled links in this example.
Your
<span>
example produces the following: Which, while it does allow you specifically target those<a>
elements for something (styling, modification, whatever), does not work so well when you're trying to exclude them. In particular, it would require you to filter the elements manually (which you do not want to do, if you can avoid it). For example: In this case, it would be better to use the<a>
markup, rather than wrapping the wiki link markup in a<span>
. For example: Which produces the following: After that, you could then modify the first line of my original example thus: Though, the following, which only handles #ui-bar menu links and story links (not bearing thenewlink
class), might be a bit better:Something like this:
Now that code doesn't work, but you can hopefully see what I'm trying to do. The problem lies with the reassigning the variable to 1 and then back to 0. The if statement works fine - if I initially define spam to 1, the sound will be blocked completely. The last two parts of the code, however, are not firing at all.
I'm pretty sure I'm not using functions and variables properly.
mouseenter
event and then ignores subsequent instances of the event until a pause of the specified delay has occurred (i.e. once activated, the debounced function deactivates until no new instances of the event have occurred for the delay):But I'm unsure of one little thing. The code I'm using is: But what I don't get is if I take out this semi colon at the end of the array, it works just as well:
The semi colon was in the example I pulled this code from, but took it out after seeing an array TheMadExile created for image preloading didn't have one. So... semi colon not needed?
I feel like you're confusing semi-colons (
;
) and commas (,
).Commas are allowed, but unnecessary, after the final element/property in either array or object literals. For example:
Semi-colons should always be used where appropriate. JavaScript has an automatic semi-colon insertion feature (henceforth: ASI), which allows you, in theory, to exclude semi-colons in various places. The problem is that the ASI rules are stupid and will happily insert semi-colons in places which will break your code. And, even more unfortunately, these breakages might not even be syntax errors, so the bug might go undetected for quite a while.
Also, you're reinventing the wheel again. SugarCube provides both the
<Array>.random()
method and theeither()
function. For example: You also don't need the extra variable for what you're doing, so my suggestion:I now want to create a duplicate version of the sound macros specifically for music play with a 4 minute 30 second debounce in place so music tracks don't start overlapping each other. So I renamed all instances of "sound" to "music" and shoved a huge debounce at the front (not knowing much about javascript).
The macros themselves work, but the debounce isn't debouncing. Can someone take a quick look and rearrange my debounce?
This code is super overkill too, since I don't need anything but the <<playmusic>> macro, but left the rest in as I didn't know what to delete. I doubt it causes any problems, though I could do with a <<fadeinmusic>> that doesn't loop, so might start looking at that.
debounce
here is not the solution. Do you simply want looping background music or something? Randomized or not?But I found those solutions problematic because of the save game feature. If I simply set a track to start at one particular point, the player will probably miss the music loop if they loaded in past that trigger point (music does not trigger at the very start of the game, but about 6 passages in, so cannot just trigger on game start). And if the player loads game while a music track is still playing, they'll probably queue up another on top.
So those required mucking around with the saved game config that I did not want to do.
Debounce seemed to be the simplest way to do it, by putting a "playmusic" macro at the start of every passage, and ensuring that it could not trigger if music was already playing. It could then be randomised by using the random function within Twine itself.
I could not use the existing unmodified sound macros to do this because they offer no protection against double playback of music tracks if I have 5-10 tracks to choose from (the user triggering two playback events within a short time, so having two tracks play on top of each other), and if I try to get round this by simply having all music in one huge file, it gets up to 30-50MB of MP3s. So, I:
1. Want to be able to load each track independently
2. Want to randomize playback to keep tracks fresh
3. Don't want double playback to happen
4. Want the tracks to load at appropriate times, which will probably mean not at game start, but on every load game use, as well as if the user turns the music off in the options, then on again
It seems like a somewhat complex thing, sorry, a bit beyond usual music implementation. So was just trying to come up with the simplest, most direct way of doing it. It seemed that a playsound macro on every passage that I could tailor with <<if>> statements, then a timer running in the background to prevent double playback that could either stop music playback on its own, or alter the <<if>> variable, was the most fitting solution.
I currently have in key passages: This way the music will trigger playing and assuming the player continues moving through the story, when it stops, it should trigger again fairly soon. I don't use <<loopsound>> due to wanting more control than a simple looping of tracks provides.
I then have in a passage called "music toggle" in my options area: This allows players to turn the music on/off at will.
Currently to prevent double play I simply rely on the fact that the browser will not play more than one instance of the same file at once, so "musicfull.ogg" contains all my music tracks in sequence. So if a player runs across <<playsound "sound/musicfull.ogg">> while the music is already playing, it will not trigger. This allows me to spread <<playsound>> commands throughout the story to ensure a player will always hit them on load game or at a point when the music has finished playing, without fear of doubling playback.
However, this is not ideal due to file size issues and the fact that they will always experience the same order of tracks. I would like to be able to separate this file into individual tracks to prevent the player from loading a huge 25MB+ file in at once, but cannot right now since my method relies on there only being one music file.
Which Story Format are you using?
The code in this thread is designed to work with SugarCube but it should be possible to convert it to work with a different story format.