Jump to content
Search Community

For loop returning incorrect counter number

pdiddles03 test
Moderator Tag

Warning: Please note

This thread was started before GSAP 3 was released. Some information, especially the syntax, may be out of date for GSAP 3. Please see the GSAP 3 migration guide and release notes for more information about how to update the code to GSAP 3's syntax. 

Recommended Posts

I'm working on a game. Inside of it, i have an array which gets loop through using a for loop and if each object is not being tweened, it tweens them to a specific position like this:

if(ship.active_bullets.length > 0){
        for(var i = 0; i < ship.active_bullets.length; i++){
          if(ship.active_bullets[i]){
            if(TweenMax.isTweening(ship.active_bullets[i])===false){
              TweenMax.to(ship.active_bullets[i], 2*scale, {y:0,ease:Power0.easeNone,onComplete:function(){
                console.log(i)
                
              }});
            }
          }
        }
      }

The output in the console on complete is then number 1 instead of 0 which is what it is before the tween. why is this?

Link to comment
Share on other sites

I did seem to solve the issue by doing this but I don't feel this is a very convenient way of doing it. Seeing I am writing a game, it adds unnecessary bloat to it.

if(ship.active_bullets.length > 0){
        for(var i = 0; i < ship.active_bullets.length; i++){
          if(ship.active_bullets[i]){

            if(TweenMax.isTweening(ship.active_bullets[i])===false){
              (function(i){
                TweenMax.to(ship.active_bullets[i], 2*scale, {y:0,ease:Power0.easeNone,onComplete:function(){
                  
                }});
              })(i)

            }
          }
        }
      }

Link to comment
Share on other sites

Hi and welcome to the GreenSock forums,

 

Hmm, it seems like the loop is working properly just not as you would wish.

By the time your onComplete callbacks run i has already been incremented to its highest value.

 

It really helps if you could provide a reduced sample, but I think using onCompleteParams will help:

 

for (var i = 0; i < 3; i++)
  TweenLite.to("#box" + i, 0.2, {
    x: 100,
    onComplete: function(n) {
      console.log(n) //0, 1, 2
    },
    onCompleteParams:[i]
  })

 

http://codepen.io/GreenSock/pen/mEWqWR?editors=0011

  • Like 2
Link to comment
Share on other sites

As Carl just demonstrated, you have to capture that value before the next loop. Notice how the buttons are labeled correctly but logging the wrong value. 

See the Pen XmBPdO?editors=0011 by osublake (@osublake) on CodePen


 
If you're worried about bloat, use your array. Your code can be reduced to 2 lines of code.

activeBullets
  .filter(function(bullet) { return !TweenMax.isTweening(bullet); })
  .forEach(function(bullet) { TweenLite.to(bullet, 1, { y: 0 }); }); 

Time to do some bullet/rocket spawning...

See the Pen 0d7684be69769aa4edc1d69d01a9a86f?editors=0010 by osublake (@osublake) on CodePen

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...