Here’s the problem, document.onkeydown = checkkeycode
I’m passing two variable to checkeycode e which is storing the event and hence keycode and the array circle which I want to update with variable c’s latest position. when I pass circle to the function, it becomes undefined hence
{circle[4] = c._.ty};
returns an undefined error
I tried putting document.write(circle);
into the document and it just wrote undefined and when I switched it with E it became a keyboard object. What I need solved is:
-
how do I pass the array circle to the function so that when raphael translates the circle, it updates the values in circle?
-
in the if (keycode == blah ) do I just do {c.translate(value)} {circle[4] = c._.ty} ?
do I append two different function to a single if event?
<html>
<head>
<script language="javascript" type="text/javascript" src="/home/andrew/Documents/rahpael/raphael-min.js"></script>
</head>
<body>
<script language="javascript">
var paper = Raphael(50, 50, 420, 300);
var d = paper.rect(150, 150, 30, 30);
var c = paper.circle(50, 50, 40);
var circle = [c.attrs.cy, c.attrs.cx, c.attrs.r, c._.tx, c._.ty];
var rectie = [ d.attrs.x, d.attrs.y, d.attrs.height, d.attrs.width];
document.onkeydown = checkKeycode
function checkKeycode(e, circle) {
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
if (keycode == 40) { c.translate(0, 10)}; //{circle[4] = c._.ty};// down
if (keycode == 39) { c.translate(10, 0)}; // right
if (keycode == 38) { c.translate(0, -10)}; // up
if (keycode == 37) { c.translate(-10, 0)}; // left
}
// deleted some comments I've made so far.
</script>
</body>
</html>
A couple of issues there:
1) Passing arguments
No, you aren’t. The browser will pass one argument to it, the
event(unless it’s IE, but you’ve handled that).If you change:
to
…you’ll be passing in the
circleyou defined at global scope.2) Names
Your
checkkeycodefunction accepts the argumentseandcircle, but appears to useeandcinstead.(Your
checkkeycodecode already closes over thecircleat global scope anyway, so you could just drop thecircleargument from the definition ofcheckkeycodeand usecirclerather thanc. But best to keep things modular if you can, by passing the argument into the function.)So if you’re going modular and just closing over
circlein the event handler: