Solved

bouncing ball

Posted on 2003-11-23
5
1,598 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
5 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 50 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

830 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