javascript - Uncaught TypeError: cannot set property 'x' of undefined -
as part of first code trying make array randomly generated circles (stars).
this line typeerror occurs.
stars[i].x = math.floor(math.random() * w)
i looked through code , stars defined.
$(document).ready(function() { //canvas var canvas = $("#canvas")[0]; var ctx = canvas.getcontext("2d"); var w = $("#canvas").width(); var h = $("#canvas").height(); var stars = [];
the rest of code fine might have here see else may have gone wrong.
$(document).ready(function() { //canvas var canvas = $("#canvas")[0]; var ctx = canvas.getcontext("2d"); var w = $("#canvas").width(); var h = $("#canvas").height(); var stars = []; function init() { createstars(); drawstars(); } init(); function createstars() { (var i=0; i<=4; i++) { stars[i].x = math.floor(math.random() * w); stars[i].y = math.floor(math.random() * h); } } function drawstars() { (var i=0; <= 4; i++) { ctx.beginpath(); ctx.arc(stars[i].x, stars[i].y, 10, 0, 2 * math.pi); ctx.stroke(); } } });
this first program ever i'm not entirely sure debugging process. thank in advance.
start
array defined, stars[i]
object there? need explicetly create objects:
function createstars() { (var i=0; i<=4; i++) { stars[i] = {}; stars[i].x = math.floor(math.random() * w); stars[i].y = math.floor(math.random() * h); } }
or more concise syntax:
function createstars() { (var i=0; i<=4; i++) { stars[i] = { x: math.floor(math.random() * w), y: math.floor(math.random() * h) }; } }
Comments
Post a Comment