Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

bouncing ball

Posted on 2003-11-23
5
Medium Priority
?
1,617 Views
Last Modified: 2006-11-17
i made a program for bouncing ball but i couldn't make it better. it is run correct but i need clew to how control the numbers of movement of the ball

this is my code;

<HTML>
<HEAD>
<TITLE>Bouncing Ball</TITLE>

<STYLE>

#table
 {
  position:absolute;
  top:100px;
  left:100px;
  width:300px;
  height:300px;
  border-style:ridge;
  background-color:white;
 }

#ball
 {
  position:absolute;
  top:100px;
  left:300px;
 }

</STYLE>

<!---- START java script----->

<script language="javascript">
<!--

  var xPos=100 + (Math.random()*100);  //Ball in x position
  var xvel=1 + (Math.random()*3);   //Ball in x velosity
  var yPos=100 +(Math.random()*100);
  var yvel=1 + (Math.random()*3);

var isNetscape = navigator.appName=="Netscape";

function move(elementId)

{

    xPos = xPos + xvel ;
    yPos = yPos + yvel;

    ball.style.left = xPos;
    ball.style.top = yPos;
    if (xPos>370) {xvel= -4}
    if (xPos<110) {xvel= 40}
    if (yPos>370) {yvel=-4}
    if (yPos<110) {yvel=40}

    if (isNetscape)
    {

        document.ball.left = xPos;
        document.ball.top = yPos;

    }
    else
    {

        ball.style.left=xPos;
        ball.style.top=yPos;
    }
}


//-->
</SCRIPT>

<!----- END java script------>
</HEAD>

<BODY onclick=clearInterval(intervalId)
onload="intervalId = setInterval('move(ball)',30)" class="all">

<link  rel="stylesheet" type="text/css" href="mystyle2.css">

<H1><p class="heading">Bouncing Ball</p></H1>


<p id="table"></p>

<div id="ball">
<img border="0" src="ball3.gif" width="40" height="40">
</div>
</BODY>
</HTML>

thanky very much
0
Comment
Question by:alqaoud
  • 2
4 Comments
 
LVL 4

Expert Comment

by:apprenti
ID: 9806750
>> i made a program for bouncing ball

Just wondering...did you write it?
0
 
LVL 10

Expert Comment

by:NeoTeq
ID: 9809454
I doubt that too, but anyway try this:
(I will add some random numbers if you like)

<HTML>
<HEAD>
<TITLE>Bouncing Ball</TITLE>

<STYLE>

#table
 {
  position:absolute;
  top:100px;
  left:100px;
  width:300px;
  height:300px;
  border-style:ridge;
  background-color:white;
 }

#ball
 {
  position:absolute;
  top:100px;
  left:300px;
 }

</STYLE>

<!---- START java script----->

<script language="javascript">
<!--

  var xPos=100 + (Math.random()*100);  //Ball in x position
  var xvel=1 + (Math.random()*3);   //Ball in x velosity
  var yPos=100 +(Math.random()*100);
  var yvel=1 + (Math.random()*3);

var isNetscape = navigator.appName=="Netscape";

function move(elementId)

{

    xPos = xPos + xvel ;
    yPos = yPos + yvel;

    ball.style.left = xPos;
    ball.style.top = yPos;
    if (xPos>370) {xvel= -xvel}
    if (xPos<110) {xvel= -xvel}
    if (yPos>370) {yvel= -yvel}
    if (yPos<110) {yvel= -yvel}

    if (isNetscape)
    {

        document.ball.left = xPos;
        document.ball.top = yPos;

    }
    else
    {

        ball.style.left=xPos;
        ball.style.top=yPos;
    }
}


//-->
</SCRIPT>

<!----- END java script------>
</HEAD>

<BODY onclick=clearInterval(intervalId)
onload="intervalId = setInterval('move(ball)',30)" class="all">

<link  rel="stylesheet" type="text/css" href="mystyle2.css">

<H1><p class="heading">Bouncing Ball</p></H1>


<p id="table"></p>

<div id="ball">
<img border="0" src="ball3.gif" width="40" height="40">
</div>
</BODY>
</HTML>
0
 
LVL 10

Accepted Solution

by:
NeoTeq earned 200 total points
ID: 9809474
Better yet (with random, no crossing boundaries):

<HTML>
<HEAD>
<TITLE>Bouncing Ball</TITLE>

<STYLE>

#table
 {
  position:absolute;
  top:100px;
  left:100px;
  width:300px;
  height:300px;
  border-style:ridge;
  background-color:white;
 }

#ball
 {
  position:absolute;
  top:100px;
  left:300px;
 }

</STYLE>

<!---- START java script----->

<script language="javascript">
<!--

  var xPos=100 + (Math.random()*100);  //Ball in x position
  var xvel=1 + (Math.random()*3);   //Ball in x velosity
  var yPos=100 +(Math.random()*100);
  var yvel=1 + (Math.random()*3);

var isNetscape = navigator.appName=="Netscape";

function move(elementId)

{

    xPos = xPos + xvel ;
    yPos = yPos + yvel;

    ball.style.left = xPos;
    ball.style.top = yPos;
    if (xPos>370) {xvel= -xvel + (Math.random()*3) - 1.5; xPos = 370}
    if (xPos<110) {xvel= -xvel + (Math.random()*3) - 1.5; xPos = 110}
    if (yPos>370) {yvel=-yvel + (Math.random()*3) - 1.5; yPos = 370}
    if (yPos<110) {yvel=-yvel + (Math.random()*3) - 1.5; yPos = 110}

    if (isNetscape)
    {

        document.ball.left = xPos;
        document.ball.top = yPos;

    }
    else
    {

        ball.style.left=xPos;
        ball.style.top=yPos;
    }
}


//-->
</SCRIPT>

<!----- END java script------>
</HEAD>

<BODY onclick=clearInterval(intervalId)
onload="intervalId = setInterval('move(ball)',30)" class="all">

<link  rel="stylesheet" type="text/css" href="mystyle2.css">

<H1><p class="heading">Bouncing Ball</p></H1>


<p id="table"></p>

<div id="ball">
<img border="0" src="ball3.gif" width="40" height="40">
</div>
</BODY>
</HTML>
0
 
LVL 16

Expert Comment

by:jaysolomon
ID: 10383624
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Accept: NeoTeq {http:#9809474}

Please leave any comments here within the next four days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jAy
EE Cleanup Volunteer
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question