JavaScript - Help Bouncing Ball User Input Javascript
Spent past few days helping my son for his project of manipulating bouncing ball. Please help me this script work. Once user inputs the new ani value, the ball goes out. I want user to set any value ani, int and size of the ball to achieve desired animation. Seriously urgent... Thanks!
<HTML> <HEAD> </HEAD> <BODY> <IMG ID="ball" STYLE="position: absolute; left: 325; top: 200; width: 30; height: 30;" SRC="images/ball1.gif"> <SCRIPT LANGUAGE="JavaScript"> function showAlert() { alert('ANI=' + ani + ', INT=' + int + ', SIZE=' + wxh + ''); } </SCRIPT> <SCRIPT LANGUAGE="JavaScript1.2"> <!-- var myBall = document.getElementById("ball"); var loc = 200; var ani = 10; var direction = 0; var int = 50; var wxh = 30; setInterval("myFunction()", int); function myFunction(){ if(0 == direction) { /* move down */ loc += ani; if(loc >= 500) { /* reached lower limit, change direction */ direction = 1; } } else { /* move up */ loc -= ani; if(loc < 10) { /* reached upper limit, change direction */ direction = 0; } } myBall.style.top = loc; } // --> </SCRIPT> <SCRIPT LANGUAGE="JavaScript1.2"> function readText (form) { if(form.f_ani.value != ""){ ani = form.f_ani.value ; myBall = document.getElementById("ball"); loc = 200; direction = 0; int = 50; setInterval("myFunction()", int); myFunction(); } showAlert(); } </SCRIPT> <FORM action="" method="Get"> Ball Movement (10 pixels):<input type='text' NAME="f_ani" value="10" size="20"><br> Interval (200 milliseconds):<INPUT type="text" id="f_int" value="200" size="20" name="1"><br> Ball Size (10 pixels W&H):<INPUT type="text" id="f_size" value="30" size="20" name="2"><br> <input type='button' value='Send' onclick="readText(this.form)"> <INPUT type="reset"> </FORM> </BODY> </HTML> Similar TutorialsHello, I am very new to javascript and most programming in general, but I am trying to make a simple game with a ball and paddle. The catch is the user inputs the height, width and speed of the ball, then when the start button is hit, the program runs... I was successfully able to make the ball bounce in a preset box (it was 500x500) but I am struggling to get the code to work when the user types in the values the ball either goes crazy or does not work. I have tried many things, but nothing has worked. the code I included is my most recent attempt. I am not asking for you guys to do the whole game for me, I would just like to know the solution to why my code is not working and any suggestions the ball is 15x15px btw Thanks, thejsnoob Code: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Untitled</title> <style type="text/css"> .button{height: 170px}; <!-- img { position: absolute; top: 300px; left: -50px; z-index::+1;} --> </style> </head> <body> <img src="ball.png" alt=""> <form name = "form1"> <table border = "1"> <tr> <td> WIDTH: </td> <td> <input type="text" id = "usrWidth"/> </td> <td rowspan="3" align="center" valign="middle"> <input type="button" class="button" value="START" onclick="moveImg()"/> </td> </tr> <tr> <td> HEIGHT: </td> <td> <input type="text" id = "usrHeight"/> </td> </tr> <tr> <td> SPEED: </td> <td> <input type="text"/> </td> </tr> </table> </form> <tr> <td></td> </tr> </table> <script language="JavaScript" type="text/javascript"> <!-- var xDir = 1; var yDir = -1; var left; var top; var check = 1; function moveImg() // clicks the button { var userWidth = document.form1.usrWidth.value; var userHeight = document.form1.usrHeight.value; if ( check == 1 ){ left=Math.floor(Math.random()* (userWidth + 1)); top=(Math.floor(Math.random()* (userHeight + 1)) + 220); document.images[0].style.left = left + "px"; document.images[0].style.top = top +"px"; check = 0; } document.body.appendChild(rectangle(userWidth,userHeight)) left += xDir; top += yDir; if ((top < 220) || (top > userHeight - 15)) yDir *= -1; if (left > (userWidth - 15) || (left < 0)) xDir *= -1; setTimeout(moveImg, 10); } function rectangle(w,h) { var rect = document.createElement("div"); var style = rect.style; style.width = w +"px"; style.height = h +"px"; style.left = "0px"; style.top = "220px"; // fixed style.position = "absolute"; style.borderStyle = "solid" style.borderWidth = 1; return rect } //document.body.appendChild( rectangle(500,500,0,220,"","red") ) //--> </script> </body> </html> I want to use javasript to create a new html page named from a text field in a form. Then I want to use javasript to copy all of the form fields to the new html page that was created using javascripting. I am creating a user are using a Javascript password login. With most of them the page is the user name or password. So this is why I am looking to do this. I use web 1000 a free host and they will only let me use javascripting. Any info or help would be great thank you
Hi, I'm very new to Javascript and I'm stuck. I am trying to create a js script that will prompt the user for a times table (example the 5 times table, the 7 times table, etc.) & prompt the user for 2 more values; the 'start' number and 'end' number. So there would be 3 numbers total the user must input. The script should present a 'warning' message if the user inputs letters or negative numbers. I have come very close to getting it too work but can't quite get past the last hurdle. Upon entering the 3 numbers, the output should look similar to this: (the 5 times table starting with 1 and ending with 12) The 5 times table 5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25 5 * 6 = 30 5 * 7 = 35 5 * 8 = 40 5 * 9 = 45 5 * 10 = 50 5 * 11 = 55 5 * 12 = 60 I have placed the js script code on pastebin.com and below. The pastebin link is: http://pastebin.com/2NZPiFH9 Code: function writeTimesTable(timesTable, timesByStart, timesByEnd) { for (;timesByStart <= timesByEnd; timesByStart++) { document.write(timesTable + " * " + timesByStart + " = " + timesByStart * timesTable + "<br>"); } } var timesTable; while ( (timesTable = prompt("Enter the first number in the times table. Enter -1 to exit.", -1)) != -1) { while (isNaN(timesTable) == true) { timesTable = prompt(timesTable + " is not a valid number, please retry.",-1); } if (timesTable == -1) { break; } document.write("<br>The " + timesTable + " times table<br>"); writeTimesTable(timesTable,1,12); } I'm trying to create an extended version of the famous bouncing ball script in HTML5/JS. This is where balls are drawn using the canvas which then move around the canvas randomly. What I'm trying to achieve is to replace that drawn ball with an actual jpg/png image. Also to have each image to redirect you to different pages when clicked.
Here's some code I wrote. It's just a red ball traveling around the screen. The point is to simulate some rudimentary physics. This means I have to make the ball bounce around the screen, but also get pulled down by gravity a bit. I did the bouncing part. However, I'm not sure how to simulate gravity. Any tips? Basically I just have to reduce the vertical velocity of the ball by a certain amount at every interval, but I'm not sure how to do that. I've tried different for loops in which vy will decrease by one, but to no avail! Code: <html> <head> <style> table {border-collapse:collapse} </style> <script> nx=128; ny=32; wx=600; wy=400; dx=Math.floor(wx/nx); ny=Math.round(nx*600/1000); x1=1;y1=1; vx=1;vy=1; function dart() {id1=x1+","+y1; b1=document.getElementById(id1); b1.style.backgroundColor="yellow"; if(x1==0 || x1==nx-1)vx= -vx; if(y1==0 || y1==ny-1)vy= -vy; x1 = x1+vx; y1 = y1+vy; id1=x1+","+y1; b1=document.getElementById(id1); b1.style.backgroundColor="red" } </script> </head> <body> <table> <script> dy=dx; for(i=0;i<ny;i++) {document.write("<tr>"); for(j=0;j<nx;j++) {color="yellow"; id1=j+","+i; document.write( "<td id="+id1+" style='background-color:"+color+ ";width:"+dx+";height:"+dy+"'></td>"); } document.write("</tr>"); } </script> </table> <form> Number of milliseconds <input type ="text" value="10" id="ms"/input> <br> <input type="button" value="dart" onclick="setInterval('dart()',document.getElementById('ms').value)"/input> </form> </body> </html> CLIFFS: - code shows red dot bouncing around - how do i decrease vertical velocity by N amount at every interval? I found a menu I really like at the top of the page at www-dot-sachscreative-dot-com but can't figure out how it works from the source code. Can someone tell me how to make something like it which is browser independent and uses javascript and/or CSS? It's been years since I did javascript work and I've forgotten must of what I used to know. Thanks.
I will be working on a game where there are balls that fall from the "sky" and the person moves around a square with the mouse trying not to get hit by the balls. I am going to use a random number between 1 and 900 to determine the position of the balls that are falling. Every second I will have 6 balls fall at random times (so they will be falling at random times in random places) What I need help with are the collisions. What I was thinking is use div's with borders and rounded edges so I didn't have to deal with images. I need help with making a function that stops the game when a <div> (with names?) hits the square (also a div, but with an id) And I would need to call the function every 10 milliseconds. How should I go about this? _________
Hello all, First off I would like to say a big hello and thank you to the forum for helping me with code over the years... I am trying to create an iframe with a user input, however, I dont simply want the iframe to display the URL the user has entered in the iframe. I want it to be used within a URL string. For example: The iframe should display http://www.google.co.uk/search?q=UserKeyword I havent got a clue where to start from.... Any help would be highly appreciated! Thanks! Is there any possible way for JS to save changes that a user has made? my situation is that I'm a photographer who offers photobooks. My customers often have hundreds or thousands of photos and want captions on lots of them. I want to post a photo gallery with a user input form. I've created a simple form, but I want the user to be able to save comments straight to the gallery so I can look at them... it's only a very simple form and isn't quite finished yet, oh by the way, I'm not sure how to use the check box... but, i'll work on that. Code: <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Untitled Document</title> <style type="text/css"> <!-- #divWrapper { text-align: center; width: 430px; margin-right: auto; margin-left: auto; } --> </style> </head> <body> <script type="text/javascript"> function picForm() { if(document.frmInput.addPic.value="yes") { document.frmOutput.outChoice.value="will" } else { document.frmOutput.outChoice.value="will NOT" } document.frmOutput.outCap.value=document.frmInput.addCap.value; document.frmOutput.outComm.value=document.frmInput.addComm.value; } </script> <div id="divWrapper"> <p>Uncheck the box if you do NOT want this photo to appear in your photobook. Please write your caption in the box provided. Any further comments you wish to make can be entered in the second box.</p> <div id="divInput"> <form name="frmInput"> <table id="tblForm" cellpadding="3px"> <tr> <td><input name="addPic" type="checkbox" value="yes"> Add to Photobook</td> <td> </td> </tr> <tr> <td>Caption<br> <textarea name="addCap" cols="24" rows="3" value="...Add Caption Here..."></textarea></td> <td>Comments<br> <textarea name="addComm" cols="24" rows="3" value="...Add Comments Here..."></textarea></td> </tr> <tr> <td></td> <td><div align="right"> <input name="btnSubmit" type="button" value="Submit" onClick="picForm()" /> </div></td> </tr> </table> </form> </div> <div id="divOutput"> <form name="frmOutput"> <p>This photo <input name="outChoice" type="text" value="" size="7" style="border:0px" /> appear in your photo book.</p> <p>You would like the caption to read: <input name="outCap" type="text" value="" size="50" style="border:0px" /> </p> <p>Your comments to us a <input name="outComm" type="text" value="" size="50" style="border:0px" /> </p> </form> </div> </div> </body> </html> Please help! In a normal chatroom, when the user "says" something, the text will be displayed normally on a line with the date/time attached like the example below. 2011/11/09 Capypara: Blah balh blah I would like to make it so that the text typed in by the user will appear inside a graphic (square, or those chat bubbles) like the example below. The graphic will only appear when a user says something, so essentially both the graphic and the text will appear together. ~~~~~~~~~~~~~~~~~~~~~~~~ | | | 2011/11/09 Capypara: Blah balh blah | | | ~~~~~~~~~~~~~~~~~~~~~~~~ Is it possible to go about doing this? Thanks for reading this. So I have been working on this exercise for about a month. In the book I am reading it say's this:"For each number that the user enters, the text boxes on the web page should display: the last score that was entered, the number of scores that have been entered, the total of these scores, the average score, the best score, and the worst score. When you write the code for this function, you should enclose the prompt method within a loop, but you decide which type of loop is best for this application. Now, write and test the code." The first peice of code is my HTML, and the second piece of code is the function that I have to start from in a .js file. I just seem to be confused at how my structuring needs to be. The 999 in the function is what the prompt says to enter to escape. Code: <body> <div id="content"> <h1>Using Loops</h1> <label for="lastScore">Last Sco </label> <input type="text" id="lastScore" /><br /> <p></p> <label for="scoreCount">Score Count:</label> <input type="text" id="scoreCount" /><br /> <label for="scoreTotal">Score Total:</label> <input type="text" id="scoreTotal" /><br /> <p></p> <label for="scoreAverage">Average Sco </label> <input type="text" id="scoreAverage" /><br /> <label for="bestScore">Best Sco </label> <input type="text" id="bestScore" /><br /> <label for="worstScore">Worst Sco </label> <input type="text" id="worstScore" /><br /> <p></p> </div> Code: var process_test_scores = function () { var testScore; testScore = parseInt(prompt( "Enter a valid test score between 0 and 100.\n" + "To end test score entry, enter 999.", 999)); } Hey, so i've been learning basics and I can get the following code to work as a prompt and I thought hey, I wounder if I can get it to work of a UI system, so I edited the code and got this Read Below Post Can you please help me to find where I am going wrong. Thanks MancunianMacca I need help using javascript cookies to save the input users will fill in to many inputs within one form. I've never worked with cookies and I was wondering if someone could help me out here - the answers I've found searching the web do not work for me. Any help is appreciated, Thanks. Okay, so I've created a JavaScript form which allows the user to enter their name into the form and display it within the sentence, "Welcome to the site ______". It works on one page, but, problem is, I'd like it to be displayed on every page. I'm aware you cannot do this with getElementbyId (can only be used once), which is what I've used. I've tried using getElementsbyClass, but from my understanding, it's not as simple as that. Here's my code so far anyway: Code: <script type="text/javascript"> function inputname(){ var userInput = document.getElementById('userInput').value; document.getElementById('welcome').innerHTML = userInput; } </script> Code: <p>Welcome to the site <b id='welcome'></b> </p> <input type='text' id='userInput' /> <input type='button' onclick='inputname()' value='Enter Name'/> Hey everyone, I've looked around quite a bit and can't seem to find anything but I was wondering if there is some open source software offering a text editor/input field similar to the one used on this forum when submitting a comment or thread? Where you can click a bold button or Italic button or URL button to structure it? Thanks Josh Hi there, I'll try to be brief: When a user submits a website in the input field on my site, I'd like there to be a background process that takes a screenshot of that website and then displays that image to the user. This website does it perfectly. The user writes the website to the right, and the screenshot is displayed to the left. I'm looking for a free alternative, and read that perhaps webkit(s) might be the solution? I'm a total newbie when it comes to this so any feedback is gold to me. Thank you so much guys/gals. Hey guys, I just joined the forums (literally) and just started coding (mainly with CSS and HTML) about 3-4 weeks ago. I'd really like to get into JavaScript to make cool things happen on my website. With that being said, I've come up with a pretty cool idea but am not sure how to go about my problem. Enough with the chit chat. What I'm trying to do is add text to a user's input. For instance, if they write "Hello", the function should automatically add "World" to the output. I'd ideally like for the output to be searched on Google (think search engine) with the results displayed on a new page. Thanks in advance for any help! Hi I have a form with some input tags, and when the user submit the form I which if a new hidden tag get submitted with the value the user did tip. I am thinking for doing that but not sure if it is the right thing : Code: <form method="POST" <input type="text" name="{actionForm.Name}" id="username"> <input type="password" name="{actionForm.Passwd}" id="password"> <input type="hidden" name="usernameh" value="document.getElementById('username')"> <input type="hidden" name="passwordh" value="document.getElementById('password')"> </form> Is it alright ? I mean when the user enter the username and password, the hidden tags should contain those two values that the user has made. Dear expert (Ok, i am new to this and should maybe not address this as a letter ) I hope someone can help me ... this is what i want to achieve (I am using frontpage) I want to create a table (this i can do!) that require a user input (number of guests). It then has SEVERAL options: - Select an option from a drop down list, and a price is then loaded from somewhere to create a total for that option and display it in the table (And keep that total for adding later) - Check a box, and if checked it creates a further calculation, displaying and storing the number for further calculation. a number of the above options, and a grand total on the bottom. I found more or less a page on a site that does what i am looking for, but after looking at the code, i have even LESS of an idea how it works Can someone PLEASE help me, or point me in the right direction? (I am not looking for someone to just write the code for me, as this will not teach me anything, and i want to learn) Below is a link to the example .. http://www.ouma-se-kombuis.co.za/test.html Thank you! |