Jump to content

something weird going on..


php_novice2007

Recommended Posts

Hi,

 

I've got the following function:

 

function animatePath(ppoly) {
  if (d>=eol) {
    alert("6");
    killAni();
    alert("7");
    return;
  }
  alert("5");
  var p = ppoly.GetPointAtDistance(d);
  var indexNum = ppoly.GetIndexAtDistance(d);
  aniIndexCount++;
  if( paused == 0 ) {
    d = d + step;
    alert("here1");
    setTimeout("animatePath(ppoly)", tick); 
  }
  alert("8");
  carMarker.setPoint(p);
  alert("9");
  map.addOverlay(carMarker);
  alert("10");
}

 

The variables d, eol, step, tick, carMarker, and map are all global. When I run this function, I get the alert boxes 5, here1, 8,9,10, and then an error saying ppoly is undefined. I'm guessing its within the setTimeout function, when the timer expires, the function calls itself, but somehow it doesn't recognise ppoly...

 

How can I fix this?

 

Please help.. thanks!

Link to comment
Share on other sites

And what is ppoly? Object? Than you can't use setTimeout syntax as you did.

Because in fact it than call somethink like animatePath([object]).

 

If ppoly is stored in some global array or it is an element of the document use

setTimeout('animatePath(document.getElementById("'+ppoly.id+'")', tick);

or

setTimeout('animatePath(getPoly(ppoly.id))', tick);

where getPoly is function that retrieve ppoly object.

Link to comment
Share on other sites

You have to call setTimeout in the same way you've called animatePath for the first time.

Considering that setTimeout will call it's function from the global scope.

 

Maybe if you post bigger part of your script...

Link to comment
Share on other sites

Sure.. The code that calls animatePath for the first time is this:

 

function startAnimation(){
   aniIndexCount = 0;
   var radiocounter;
   for (radiocounter = 0; radiocounter < animation_form.AnimationChoice.length; radiocounter++) {
     if (animation_form.AnimationChoice[radiocounter].checked) {
       break;
     }
   }
                    
   var tmpPoly;
   for (var i=0; i<polyArray.length; i++) {
     tmpPoly = polyArray[i];
     if (tmpPoly.myname == animation_form.AnimationChoice[radiocounter].value) {
       alert("1");
       eol = tmpPoly.Distance();  //total distance of the polyline.
       alert("2");
       paused = 0;
       d = 0;
       step = eol/300;  //so the animation will run for 30 seconds since time step is 100 ms. 
       carMarker = new GMarker(tmpPoly.getVertex(0),gicon["car"]);
       break;
     }
   }
   animatePath(tmpPoly);
 }

 

Basically when the user clicks on a button to start the animation, it finds out which path the user has chosen by the radio buttons. And then picks out the path from an array, and set it to the path in which animatePath will follow through...

 

Maybe it would work if I make ppoly a global variable and just have startAnimation() set the path chosen to be ppoly and then aimatePath won't need an input parameter.. But the thing is, I want to eventually make the program so that I could be animating multiple paths at the same time, so instead of having radio buttons have check boxes and when the user clicks start animation goes through all the check boxes figure out which paths needs to be animated and save them all in an array and pass it to animatePath which can go through each path and step through them..

 

For now I'm just trying to get one working :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.