Howdy, Stranger!

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

Probably already answered: got a die/dice, want to display button depending on result

This is what I got, a friend helped me out so button could only be pressed once, I actually have no clue on html
<!DOCTYPE html>
<html>
<body>
<h1 ALIGN=center>
<button id="boton" onclick="myFunction()">Try to hit</button>
<h2 ALIGN=center id="demo"></p ALIGN=center>
<script>
function myFunction() {
document.getElementById("boton").disabled = true;
var x = Math.floor((Math.random() * 20) + 1);
document.getElementById("demo").innerHTML =x;
if ( x <= 5 ) {
document.getElementById("demo").innerHTML += "<p>You fail your attack";
} else {
document.getElementById("demo").innerHTML += "<p>You hit";
}
}
</script>
</h1 ALIGN=center>
</body>
</html>
the text was something different but this is just fine for you guys to understand what I want to do :P
I tried replacing "You fail your attack" with "Fail but, as you can conclude, no result

It would be awesome too to keep the random number hidden, and yes, I know there are better ways to do this but, those were too hard for me, sorry.

So, if anyone is willing to kindly help me, thanks in advance :D

Comments

  • Can you maybe explain more what you are trying to do, because I cannot seem to get the crasp of your endgoal. Also please give us the full version number of both your version of Twine and the Story Format you are using.
  • Sorry for not asnwering before, didn't know my question would be approved so fast.

    Plus, I had introduced a spoiler that now is dissapeared for some reason, i'll just resend it and explain myself

    Version: Harlowe 2.0.1
    <!DOCTYPE html>
    <html>
    <body>
    <H1 ALIGN=center>
    <button id="boton" onclick="myFunction()">Attack the beast/throw the die/whatever message</button>
    <H2 ALIGN=center id="demo"></H2 ALIGN=center>
    <script>
    function myFunction() {
    document.getElementById("boton").disabled = true;
    var x = Math.floor((Math.random() * 20) + 1);
    document.getElementById("demo").innerHTML =x;
    if ( x <= 5 ) {
    document.getElementById("demo").innerHTML += "<p>11";
    } else {
    document.getElementById("demo").innerHTML += "<p>12";
    }
    }
    </script>
    </H1 ALIGN=center>
    </body>
    </html>

    In my story this is something that happens after you decide to either do a thing or another, so you can dodge this event or autowin it with other route, it won't be frustrating at all, but I want to:

    Make it random A or B outcome, maybe even without the reader/player knowing what's the numerical result, just the forced path (or forced different paths) that he can take from here.

    I tried to just copy and paste the Tw-link thingy I can read when inspecting an element, it didn't either work out, so, resuming:
    What is "Harlowe 2.0.1's way" to create a link from a scrpit
    How can I make in html the number to exist but not show?

  • I'm not sure why you are trying to using Javascript to do this when it can easily be done using Harlowe's version of TwineScript.

    You can use:
    A. a (link:) macro to show a single click link.
    B. the combination of a (random:) macro, a (set:) macro, and a temporary variable to obtain the random value.
    C. an (if: ) macro and an (else: ) macro to check the temporary variable to determine what to do next.
    D. a Named hook combined with a (replace:) macro to update a portion of the page.

    The code would look something like the following:
    (link: "Attack the beast/throw the die/whatever message")[
    	(set: _roll to (random: 0, 21))
    	(if: _roll <= 5)[
    		(replace: ?result)[_roll<p>[[You fail your blow|11]]]
    	](else:)[
    		(replace: ?result)[_roll<p>[[You hit with strength|12]]]
    	]
    ]
    |result>[]
    
  • This is so much easier, I just wasn't able to find that info on my own, thank you a lot and sorry for asking dumb questions :smile:

    Now I know where to find most of the stuff I'd work with, really, this is way more simple than understanding javascript, that is a pain in the ass for a non-native english speaker as me :heart:
Sign In or Register to comment.