The Essential Guide to HTML5

(Greg DeLong) #1

CANNONBALL AND SL INGSHOT


Code Explanation


my = ev.layerY; ev.layerY for my


} else if (ev.offsetX || ev.offsetX == 0) { Does offsetX work?


mx = ev.offsetX; Use it for mx


my = ev.offsetY; Use offsetY for my


} Close if true


cball.sx = mx; Position ball x


cball.sy = my; ...and y


mysling.bx = mx; Position sling bx


mysling.by = my; ... and by


drawall(); Draw everything


} Close if in motion


} Close function


function finish(ev) { Function for mousedown


if (inmotion) { In motion?


inmotion = false; Reset inmotion


var outofcannon =
distsq(mysling.bx,mysling.by,
mysling.s1x,mysling.s1y)/700;


Base outofcannon proportional to square of
bx,by to s1x,s1y

var angleradians = -Math.atan2
(mysling.s1y-mysling.by,
mysling.s1x-mysling.bx);


Compute angle

horvelocity = outofcannon*Math.cos
(angleradians);


verticalvel1 = - outofcannon*Math.sin
(angleradians);

Free download pdf