I'm using the array as a queue. Values are added to one end and the other end is what is used and where values are removed.
f either global variable is not equal to 0 --> add the angle expression to the arrayHere we know that some direction is being pressed, so the angle is added (pushed) to the front end of the queue. In simpler terms we are just saving the current angle pressed.
f the array has 5 or more entries on the x axis (width) --> move to the back of the arrayYou have the first part right, but all it does is remove (pop) a value from the back end of the queue when there is 5 five or more values. This is limiting the number of angles saved to 4 by removing the extra values. The "back" is where we remove from since it's the oldest value.
f sprite is moving in any direction ---> set animation to walk & int(add 360 to the back of the array, divide it by 360, and divide the remainder by 45) from current frameThis one is kind of two part. The bulk of the formula is converting an angle to the closest eight direction. Here is a topic that explains it pretty well:
http://www.scirra.com/forum/sprite-movement-8-dir-troubles_topic47201.html
The second part is what angle is used, which is the oldest value in the queue or the "back" value. The gist of this is we are effectively using the direction that was pressed four frames ago.
f sprite is not moving ---> set animation to first sprite in animation, and then reset/empty the arrayWhen the sprite is not moving we want the animation to reflect this. Without this event the sprite would just walk in place. By setting the animation frame to 0 we prevent animation and use the image for standing. The effect of resetting the queue to be empty is not really noticeable and that action could be removed. I just thought it logical to fill the queue new every time instead of reusing existing values from a previous motion. But considering the time it takes to fill the array: 4 frames *dt ~= 0.07 seconds it's pointless as that's faster than human reaction time.
And yeah that's the main character from Earthbound.