The Essential Guide to HTML5

(Greg DeLong) #1

CANNONBALL AND SL INGSHOT


Code Explanation


var target = new Myrectangle(300,100,
80,200,"rgb(0,5,90)");


Build a Myrectangle object and assign to target

var ground = new Myrectangle(0,300,
600,30,"rgb(10,250,0)");


Build a Myrectangle object and assign to ground

everything.push(target); Add target to everything


everything.push(ground); Add ground


everything.push(cball); Add cball (which will be drawn last, so on top of other
stuff


function init(){ Header for init function


ctx = document.getElementById
('canvas').getContext('2d');


Set up ctx in order to draw on the canvas

drawall(); Draw everything


} Close init


function fire() { Head for fire function


cball.sx = iballx; Reposition cball in x


cball.sy = ibally; Reposition cball in y


horvelocity = Number(document.
f.hv.value);


Set horizontal velocity from form. Make a number

verticalvel1 = Number(document.
f.vv.value);


Set initial vertical velocity from form

drawall(); Draw everything


tid = setInterval
(change,100);


Start timing event

return false; Return false to prevent refresh of HTML page


} Close function

Free download pdf