Log in

View Full Version : Need help in my game code



NitroDev
10-26-2013, 04:23 PM
So the problem is i want it to show the string whats inside the variable.

Here is the code:


function explore()
{
document.body.innerHTML="<pre><h1>Where do you want to explore?<\/h1><button onClick='newWorld()'>Back<\/button><br><button onClick='exploreNorth()'>North<\/button><\/pre>"
}

<!--Below are the explore() functions
function exploreNorth()
{
var found=Branch
document.body.innerHTML="<pre><h4>You walk towards north and find (found)<\/h4><br><button onClick='explore()'>Back<\/button><\/pre>"
}

I really don't know whats the problem. :P

molendijk
10-28-2013, 08:52 PM
Function exploreNorth refers to Branch, which is not specified / does nothing.

NitroDev
10-29-2013, 01:47 PM
umm What?

molendijk
10-29-2013, 03:18 PM
function exploreNorth()
{
var found=Branch
document.body.innerHTML="<pre><h4>You walk towards north and find (found)<\/h4><br><button onClick='explore()'>Back<\/button><\/pre>"
}
What does Branch do?

NitroDev
10-29-2013, 06:49 PM
Nothing i guess btu dont you see i have set the word Branch to found

molendijk
10-29-2013, 10:11 PM
As I said, function exploreNorth refers to Branch, which is not specified / does nothing. So the variable found doesn't do anything either.
You have to give us the code for the WHOLE document to allow us to see what's happening.

NitroDev
10-30-2013, 05:50 AM
OKay here you have it then:


<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>&nbsp;</title>

<script>
function credit()
{
document.body.innerHTML+="<pre><h3>These people helped me alot:<\/h3><br><br><a href='http://www.dynamicdrive.com/forums/'>Dynamic Drive<\/a><\/pre>"
}
function craft()
{
document.body.innerHTML="<pre><p>What do you want to craft?<\/p><button onClick='newWorld()'>Back<\/button><\/pre>"
}
function explore()
{
document.body.innerHTML="<pre><h1>Where do you want to explore?<\/h1><button onClick='newWorld()'>Back<\/button><br><button onClick='exploreNorth()'>North<\/button><\/pre>"
}

<!--Below are the explore() functions
function exploreNorth()
{
var found=Branch
document.body.innerHTML="<pre><h4>You walk towards north and find (found)<\/h4><br><button onClick='explore()'>Back<\/button><\/pre>"
}
function myInventory1()
{
document.body.innerHTML="<pre>You came from first inventory item <button onClick='newWorld()'>Back<\/button><\/pre>"
}

function myInventory2()
{
document.body.innerHTML="<pre>You came from second inventory item <button onClick='newWorld()'>Back<\/button><\/pre>"
}

function myInventory3()
{
document.body.innerHTML="<pre>You came from third inventory item <button onClick='newWorld()'>Back<\/button><\/pre>"
}

function showInventory()
{
document.getElementById('inventory').style.display='block';
document.getElementById('hide_list').style.display='inline'
}

function hideInventory()
{
document.getElementById('inventory').style.display='none';
document.getElementById('hide_list').style.display='none'
}


function newWorld()
{
document.body.innerHTML="<pre><h1>Hello and welcome to World of My Core!<\/h1>What would you like to do?<br><button style='width:145px' onClick='window.location.reload()'>Exit<\/button><br><button onClick='showInventory()' style='display: inline; width: 145px'>Show Inventory Items<\/button>&nbsp;<button id='hide_list' style='display: none' onclick='hideInventory()'>Hide Inventory<\/button><ul id='inventory' style='margin-top: 10px; margin-bottom: 0px; display: none'><li onclick='myInventory1()'>first item<\/li><li onclick='myInventory2()'>second item<\/li><li onclick='myInventory3()'>third item<\/li><\/ul><br><button onClick='craft()' style='width:145px'>Craft<\/button><br><button onClick='explore()' style='width:145px'>Explore<\/button><\/pre>"
}
function charCreate()
{
document.body.innerHTML="<pre><h3>Start by choosing your name!<\/h3><br><br><input type='text' name='name'><br><br><input type='radio' name='gender' value='Male'>Male<br><input type='radio' name='gender' value='Female'>Female<br><br><button onClick='newWorld()'>New World<\/button><pre>"
}
</script>

<style>
pre{font-family: verdana; font-size: 12px}
button {font-family: arial; font-size: 13px}
button,li{cursor: pointer}
</style>
</head>

<body>

<h1 style="color:brown">Welcome to My Core</h1>

<button onClick="charCreate()">Play</button><br>
<button onClick="credit()">Credits</button>

</body>
</html>
</body>
</html>

molendijk
10-30-2013, 06:19 PM
var found=Branch is preventing the script from working, because Branch is not specified.
If you replace

function exploreNorth()
{
var found=Branch
document.body.innerHTML="<pre><h4>You walk towards north and find (found)<\/h4><br><button onClick='explore()'>Back<\/button><\/pre>"
}
with:

function exploreNorth()
{
document.body.innerHTML="<pre><h4>You walk towards north and find (found)<\/h4><br><button onClick='explore()'>Back<\/button><\/pre>"
}
it will work.

NitroDev
10-31-2013, 09:22 AM
I know but i want to have a variable on that function

molendijk
10-31-2013, 10:06 AM
So the uestion is what you want the variable to 'do'.

NitroDev
10-31-2013, 02:33 PM
well to do like this:

When i set the variable like x = thing i want to print the thing like:


var x = thing
document.body.innerHTML="<pre><p>(thing)<\/p><\/pre>

molendijk
10-31-2013, 02:50 PM
I see what you mean now. The problem was that you didn't put the value for the variable between quotes.

<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>&nbsp;</title>

<script>
function credit()
{
document.body.innerHTML+="<pre><h3>These people helped me alot:<\/h3><br><br><a href='http://www.dynamicdrive.com/forums/'>Dynamic Drive<\/a><\/pre>"
}
function craft()
{
document.body.innerHTML="<pre><p>What do you want to craft?<\/p><button onClick='newWorld()'>Back<\/button><\/pre>"
}
function explore()
{
document.body.innerHTML="<pre><h1>Where do you want to explore?<\/h1><button onClick='newWorld()'>Back<\/button><br><button onClick='exploreNorth()'>North<\/button><\/pre>"
}

<!--Below are the explore() functions
function exploreNorth()
{
var found='a branch.'
document.body.innerHTML="<pre><h4>You walk towards the north and find "+found+"<\/h4><br><button onClick='explore()'>Back<\/button><\/pre>"
}
function myInventory1()
{
document.body.innerHTML="<pre>You came from first inventory item <button onClick='newWorld()'>Back<\/button><\/pre>"
}

function myInventory2()
{
document.body.innerHTML="<pre>You came from second inventory item <button onClick='newWorld()'>Back<\/button><\/pre>"
}

function myInventory3()
{
document.body.innerHTML="<pre>You came from third inventory item <button onClick='newWorld()'>Back<\/button><\/pre>"
}

function showInventory()
{
document.getElementById('inventory').style.display='block';
document.getElementById('hide_list').style.display='inline'
}

function hideInventory()
{
document.getElementById('inventory').style.display='none';
document.getElementById('hide_list').style.display='none'
}


function newWorld()
{
document.body.innerHTML="<pre><h1>Hello and welcome to World of My Core!<\/h1>What would you like to do?<br><button style='width:145px' onClick='window.location.reload()'>Exit<\/button><br><button onClick='showInventory()' style='display: inline; width: 145px'>Show Inventory Items<\/button>&nbsp;<button id='hide_list' style='display: none' onclick='hideInventory()'>Hide Inventory<\/button><ul id='inventory' style='margin-top: 10px; margin-bottom: 0px; display: none'><li onclick='myInventory1()'>first item<\/li><li onclick='myInventory2()'>second item<\/li><li onclick='myInventory3()'>third item<\/li><\/ul><br><button onClick='craft()' style='width:145px'>Craft<\/button><br><button onClick='explore()' style='width:145px'>Explore<\/button><\/pre>"
}
function charCreate()
{
document.body.innerHTML="<pre><h3>Start by choosing your name!<\/h3><br><br><input type='text' name='name'><br><br><input type='radio' name='gender' value='Male'>Male<br><input type='radio' name='gender' value='Female'>Female<br><br><button onClick='newWorld()'>New World<\/button><pre>"
}
</script>

<style>
pre{font-family: verdana; font-size: 12px}
button {font-family: arial; font-size: 13px}
button,li{cursor: pointer}
</style>
</head>

<body>

<h1 style="color:brown">Welcome to My Core</h1>

<button onClick="charCreate()">Play</button><br>
<button onClick="credit()">Credits</button>

</body>
</html>
</body>
</html>

NitroDev
10-31-2013, 05:12 PM
Okay thanks i will put a question on this thread when i need any help

EDIT: So now i have a question: is there a way to add multiple values on 1 variable?

molendijk
10-31-2013, 05:46 PM
That depends on what you want. You can have dynamic variables, for instance. You can give different values to found depending on the function(s) in which it occurs.

NitroDev
10-31-2013, 05:51 PM
well what are the dynamic variables?

molendijk
10-31-2013, 06:48 PM
You have to define them yourself. Here's an example with '2 founds'.
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>&nbsp;</title>

<script>
function credit()
{
document.body.innerHTML+="<pre><h3>These people helped me alot:<\/h3><br><br><a href='http://www.dynamicdrive.com/forums/'>Dynamic Drive<\/a><\/pre>"
}
function craft()
{
document.body.innerHTML="<pre><p>What do you want to craft?<\/p><button onClick='newWorld()'>Back<\/button><\/pre>"
}
function explore()
{
document.body.innerHTML="<pre><h1>Where do you want to explore?<\/h1><button onClick='newWorld()'>Back<\/button><br><button onClick='exploreNorth()'>North<\/button><br><button onClick='exploreSouth()'>South<\/button><\/pre>"
}

<!--Below are the explore() functions
function exploreNorth()
{
var found='a branch.'
document.body.innerHTML="<pre><h4>You walk towards the north and find "+found+"<\/h4><br><button onClick='explore()'>Back<\/button><\/pre>"
}

function exploreSouth()
{
var found='an apple.'
document.body.innerHTML="<pre><h4>You walk towards the south and find "+found+"<\/h4><br><button onClick='explore()'>Back<\/button><\/pre>"
}


function myInventory1()
{
document.body.innerHTML="<pre>You came from first inventory item <button onClick='newWorld()'>Back<\/button><\/pre>"
}

function myInventory2()
{
document.body.innerHTML="<pre>You came from second inventory item <button onClick='newWorld()'>Back<\/button><\/pre>"
}

function myInventory3()
{
document.body.innerHTML="<pre>You came from third inventory item <button onClick='newWorld()'>Back<\/button><\/pre>"
}

function showInventory()
{
document.getElementById('inventory').style.display='block';
document.getElementById('hide_list').style.display='inline'
}

function hideInventory()
{
document.getElementById('inventory').style.display='none';
document.getElementById('hide_list').style.display='none'
}


function newWorld()
{
document.body.innerHTML="<pre><h1>Hello and welcome to World of My Core!<\/h1>What would you like to do?<br><button style='width:145px' onClick='window.location.reload()'>Exit<\/button><br><button onClick='showInventory()' style='display: inline; width: 145px'>Show Inventory Items<\/button>&nbsp;<button id='hide_list' style='display: none' onclick='hideInventory()'>Hide Inventory<\/button><ul id='inventory' style='margin-top: 10px; margin-bottom: 0px; display: none'><li onclick='myInventory1()'>first item<\/li><li onclick='myInventory2()'>second item<\/li><li onclick='myInventory3()'>third item<\/li><\/ul><br><button onClick='craft()' style='width:145px'>Craft<\/button><br><button onClick='explore()' style='width:145px'>Explore<\/button><\/pre>"
}
function charCreate()
{
document.body.innerHTML="<pre><h3>Start by choosing your name!<\/h3><br><br><input type='text' name='name'><br><br><input type='radio' name='gender' value='Male'>Male<br><input type='radio' name='gender' value='Female'>Female<br><br><button onClick='newWorld()'>New World<\/button><pre>"
}
</script>

<style>
pre{font-family: verdana; font-size: 12px}
button {font-family: arial; font-size: 13px}
button,li{cursor: pointer}
</style>
</head>

<body>

<h1 style="color:brown">Welcome to My Core</h1>

<button onClick="charCreate()">Play</button><br>
<button onClick="credit()">Credits</button>

</body>
</html>
</body>
</html>

NitroDev
10-31-2013, 08:50 PM
Okay i knew that but i want it inside the same function