Sign in to follow this  

I need help in improving my code!

Recommended Posts

So I have 3 different characters (using the same sprite at the moment). They are supposed to roam given a predefined path for each.


I am using 3 different loops so that they start at different times. I don't want them to start and stop at the same time. Trying to make it as natural as possible and so far I got this


Can someone suggest improvements? I need to make it look as smooth and natural as possible. 

Share this post

Link to post
Share on other sites

That's pretty much what I do in my games. Check out the march behavior from my game Blaster.


The enemies sleep for a short, random time then move for a short, random time. That gives them an insectoid, lurching movement that I really liked. You could consider tweening the start and finish velocities, but I know it was hard getting that moveToXY code right so maybe something else?


Also, in your "callMe" method you have a big if statement where, based on the num value, you change a bunch of array values. Don't use an if statement, use the num value for those arrays:

            isMoving[num]=false;            if(count[num]>=path[num].length) count[num] = 0;            var x = path[num][count[0]][0];         var y = path[num][count[0]][1];                        game.physics.arcade.moveToXY(players[num],x,y,60,3000);                        setTimeout((function(i){              return function() {                players[i].body.velocity.x = 0;                players[i].body.velocity.y = 0;              }        })(num),3000);            count[num]++;            isMoving[num]=true;

Or something. ( =

Share this post

Link to post
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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.