Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

use functions from javascript story

Hello everybody!
I am new to twine and I am having some problems. I am using twine2 and Harlowe, however I also tried SugarCube but none of them work.
I have been trying to use a function defined on my javascript in a passage, but when I call it an error appears saying that the function is undefined.
The javascript code is taken from ResponsiceVoice JS,as I want to transform text to speech. I also tried to use that external js file but it didnt wok so I ended up copying and pasting that code on my javascript story.

Here is what I have:

Here is my code:

Harlowe:

1.In my passage code

………………..
<script>

var responsiveVoice = new ResponsiveVoice();
//i thought that with this the function would be defined but it is not

</script>

<p id="voiceselection">Trying to change from text to speech</p>

<input
onclick="responsiveVoice.speak($('#voiceselection').val(),'US English Female');"
type="button"
value="Play"
/>
…………………….


2. In my javascript code: Sorry if that part is too long. I have attached a word file with all the code but here I have tried just to put what I use in my passage



if (typeof responsiveVoice != 'undefined') {
console.log('ResponsiveVoice already loaded');
console.log(responsiveVoice);
} else {

var ResponsiveVoice = function () {

var self = this;

self.version = 3.2;
console.log("ResponsiveVoice r" + self.version);

// Our own collection of voices
self.responsivevoices = [
{name: 'UK English Female', voiceIDs: [3, 5, 1, 6, 7, 8]},
{name: 'UK English Male', voiceIDs: [0, 4, 2, 6, 7, 8]},
//etc............

];

//All voices available on every system and device
self.voicecollection = [
{name: 'Google UK English Male'}, //0 male uk android/chrome


//etc....

];

//etc.....


//etc..............


// etc...

self.getVoices = function () {

//Create voices array

var v = [];

for (var i = 0; i < self.responsivevoices.length; i++) {
v.push({name: self.responsivevoices.name});
}

return v;

}


self.speak = function (text, voicename, parameters) {

self.msgparameters = parameters || {};
self.msgtext = text;
self.msgvoicename = voicename;


//Etc.......

}

if (self.fallbackMode) {


self.fallback_part_index = 0;
self.fallback_startPart();

}

}

//etc...

}
var responsiveVoice = new ResponsiveVoice();
}

………………………


Could you please help me to figure out what is my mistake? How can I call the responsive speak function without the undefined error?
[img][/img]

If in harlowe is not possible I can try it in Sugarcube.

Thank you very much, I would really appreciate your help


Comments

  • I got the sound but not in the way I wanted, could you help me with this?:

    This works but I don’t want a text area, I want just text like a paragraph


    <textarea id="text" cols="45" rows="3" readonly="readonly">hello my friend</textarea>
    <input
    onclick="responsiveVoice.speak($('#text').val(),'US English Female');"
    type="button"
    value="Play"
    />

    But when I try this the sound doesn’t work.

    <p id="text" >Hello my friend</p>
    <input
    onclick="responsiveVoice.speak($('#text').val(),'US English Female');"
    type="button"
    value="Play"
    />

    So I was trying to do it with variable instead of using the id of the html tag, but it doesn’t work. If I just type the text it works but as I want to change from text to speech all my passages that would be really bad, and I could change its style.

    <input
    onclick="responsiveVoice.speak(“This works”,'US English Female');"
    type="button"
    value="Play"
    />

    So this is what I have tried:

    Set the variable

    ………..
    Option1
    {
    (set: $varble to " This is the text one ")
    }

    Option2
    <script>
    var $varble = "This is the text two"
    </script>
    ………..


    Option1

    <input
    onclick="responsiveVoice.speak(varble,'US English Female');"
    type="button"
    value="Play"
    />

    Option2

    <input
    onclick="responsiveVoice.speak($varble,'US English Female');"
    type="button"
    value="Play"
    />


    …………………
    But all combinations fail with the same error: $varble or varble is not defined.

    Do you know how can I fix it?

    Thank you for your help!



  • never mind! i got ir with this:

    <p id="text" >Hello my friend</p>
    <input
    onclick="responsiveVoice.speak($('#text').text(),'US English Female');"
    type="button"
    value="Play"
    />
  • Hello beafus, next time you have issues you should attach a published story file so we can look at the code.

    Or you can use the code BB markup to demark your code as such, so that it isn't so poorly formatted as to be unreadable. Click on the "C" button to do this.
  • Thank you AvaJarvis for the advice!
    I didn´t know I could do that, next time I'll use it!
Sign In or Register to comment.