Jono,
So i have been working and I thought that this had made it in but that must have been in my newest iteration.
my requestAnimationFrame now looks like this:
var requestAnimationFrame =
window.mozRequestAnimationFrame ||
window.webkitRequestAnimationFrame ||
function(/* function */ callback, /* DOMElement */ element){
window.setTimeout(callback, 1000 / 60, new Date());
};
Do you think this would be better? or should I stick with setInterval ?
Ya in another iteration I had the ability to double jump by setting a counter and only allowing them to jump when the counter was less than 2. I do like your boolean though for other reasons, like knowing when to add gravity to yVelocity without having to always do it and set the postion to the platform if i was on a platform.
How would you go about determining that? Check for y values, and would you consider adding in a threshold of a couple pixels?
ImageLoader
So I was having trouble with images not loading before I drew them to an animation canvas that I use so I can prerender evertyhing and just draw the canvas, so nothing was showing up. After doing some research I found out that it was because the images were not loaded. After thinking about it for a while I came up with this image loader class.
it has the following attributes:
images //An array of the images
sources //An array of the sources for the images
totalImages //Keeps track of total number of images to load
loadedImages //Keeps track of currently loaded images
and the following methods:
/* Adds src to sources with the index of name */
addImage(name,src)
/* Called when we want to loaded all images */
loadImages()
/* Called within a loop(requestAnimationFrame) to show the progress it just returns (loadedImages / totalImages) * 100.0 */
getProgress()
The main function and most interesting one is loadImages
-
I loop through all of sources creating images and adding them to the images array with the index of name
-
I then set the onload to increment loadedImages and then check if loadedImages = totalImages
-
If they do eqaul I then call startGame
-
I then set the source of the image to start loading the image
-
Order matters, the onload must be set before the image.src is set
And thats it. It allows us to preload the images with a progress bar and removes the problem of drawing without an image. Sorry for the lengthy post and if you would like the code for it and an example of use just let me know =)
-Jason