Web Page Animation16. Specific Path |
|
Join the DiscussionRather than having the object move to random destinations we could instead set up an array containing a number of destinations on the screen and have the object follow a path from one position to the next. For the purpose of this example let's have our object srart in the top right corner of the screen and move to the centre, to the top left corner, back to the centre, then to the bottom left corner, back to the cenre again, to the bottom right corner, back to the centre yet again and then back to where it started in the top left corner to start over again. To handle this we add two arrays to handle the X and Y coordinates to the top of the script along with a variable to hold the array counter.
var eX = new Array();
var eY = new Array(); var nE = 0; Next we amend our nextEnd function to use the next entry from the array as the next destination:
function nextEnd() {
if (nE > eX.length) nE = 0; dist = distX = distance(xxx,eX[nE]); distY = distance(yyy,eY[nE]); stepx = 2 * direction(xxx,eX[nE]) * rate(distX,distY); stepy = 2 * direction(yyy,eY[nE]) * rate(distY,distX); nE++; } The final change is to amend the top of the start function to load the required entries into the array. We can't load the array before the page finishes loading because we need to check the screen size and object size to determine what the destinations should be.
function start() {
var w = objWidth('myobj'); var h = objHeight('myobj'); eX[0] = eX[2] = eX[4] = eX[6] = (pageWidth()-w)/2; eY[0] = eY[2] = eY[4] = eY[6] = (pageHeight()-h)/2; eX[1] = eX[3] = 20; yyy = eY[1] = eY[7] = 10; eY[3] = eY[5] = (pageHeight()-h-10); xxx = eX[5] = eX[7] = (pageWidth()-w-20); nextEnd(); moveit(); setObjVis('myobj','visible'); mover(); } You can of course have as many entries in the array as you want and set each to whatever coordinates that you want. Since the path I have set for this example uses the same values for several different positions in the array I have taken the shortcut of assigning all of the identical fields at the same time. |

