سؤال

For the variable snake1, is there a set method so that I can later get the x and y coordinates.

Something like snake1.getX() or something? Can't seem to find anything

var canvas = document.getElementById("canvas");
var context = canvas.getContext("2d");

var snake1 = context.fillRect(x,y, snakeHeight, snakeWidth);

Thanks in advance

هل كانت مفيدة؟

المحلول

No, there is no such methods, see the reference: http://www.w3schools.com/tags/ref_canvas.asp

You can fill the variable snake1 yourself by doing this for example: var snake1 = {'x': x, 'y': y}; then access it like any other object: snake1.x

نصائح أخرى

According to MDN, fillRect doesn't return any value at all, so snake1 won't actually be set to anything.

However, x and y are values you have already calculated to pass in, so you can save them wherever you want.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top