Hallo allemaal!
Ik heb een erg raar probleem, ik heb het volgende document:
Het probleem is dat wanneer ik de DrawFallingSquare-functie aanroep Firefox klaagt dat de variabele y niet gedefinieerd is, terwijl ik nergens een variabele y gebruik!
Weet iemand waardoor dit komt?
Alvast bedankt,
Joshua
Ik heb een erg raar probleem, ik heb het volgende document:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Bass Boss</title>
<script type="text/javascript">
/* These global variables contain a reference to the canvas and its 2D-context.
We need these in all functions and we only have one canvas area, so grabbing these variables
in every function is a waste of resources. */
var canvas;
var canvas_context;
/* This function makes all other functions ready to use.
ALWAYS CALL THIS FUNCTION FIRST, OR OTHER FUNCTIONS WILL
WORK WRONGLY OR NOT AT ALL. */
function Initialize ( )
{
/* Get a reference to the raw canvas object */
canvas = document.getElementById ( "drawing_area" );
/* Get the context for this canvas object */
canvas_context = canvas.getContext ( "2d" );
/* Set the stroke color and width */
canvas_context.fillStyle = "brown";
canvas_context.lineWidth = 10;
/* Set the fill color */
canvas_context.fillStyle = "red";
}
/* This function erases everything on the canvas. */
function EraseCanvas ( )
{
canvas_context.clearRect ( 0, 0, canvas.width, canvas.height );
}
/* This function draws a falling square. Accepts one argument, namely the
distance the block should fall per second */
function DrawFallingSquare ( fall_per_second )
{
/* This variable holds the constantely increasing y-value for the position
to draw the square, so when we draw the square in certain intervals it will
appear to be falling. */
var y_offset;
/* In this function the main drawing happens. We cannot put this
unencapsulated in the code because drawing needs to be done in
certain lapses of times and Javascript only has setInterval()
for such a task, which calls a function after a certain interval. */
var MainDrawingFunction = function ( ) {
EraseCanvas ( );
canvas_context.strokeRect ( 100, y_offset, 100, 100 );
canvas_context.fillRect ( 100, y_offset, 100, 100 );
}
for ( y_offset = 0; y_offset <= 300; y_offset += fall_per_second )
{
setInterval ( MainDrawingFunction, 1000 );
}
}
</script>
</head>
<body>
<canvas id="drawing_area" width="300" height="300">
<p>Deze browser ondersteund deze webapplicatie niet.</p>
</canvas>
<script type="text/javascript">
Initialize ( );
DrawFallingSquare ( 5 );
</script>
</body>
</html>
Weet iemand waardoor dit komt?
Alvast bedankt,
Joshua