Jump to content
Search Community

Random tween positioning (fish movement)

swiftmend test
Moderator Tag

Recommended Posts

Hi,

 

I'm building a "pond" like top-down experience and have a fish animated fish I'd like to random move around in the pool (and then run away if you click near them)

 

was wondering how I could leverage tweenlite to have the fish randomly move around in the pond (without going outside of the pond itself).

 

Is there some type of random position tweens with random speed? Or a way of looping the controls infinitly?

 

Or if i could make a random tween and stick it in an enter_frame type control so it keeps running it over and over?

 

Thanks for the help!

Link to comment
Share on other sites

Sure, you can just use an onComplete to call the same function over and over. Kinda like:

 

var xCenter:Number = 275;
var yCenter:Number = 200; 
var poolRadius:Number = 200;
function tweenFish():void {
   var angle:Number = Math.random() * Math.PI * 2; //random angle in radians
   var radius:Number = Math.random() * poolRadius;
   TweenLite.to(fish, 2, {x:Math.cos(angle) * radius + xCenter, y:Math.sin(angle) * radius + yCenter, ease:Quad.easeInOut, onComplete:tweenFish});
}
tweenFish();

 

You could get more fancy with using a bezier to add curved motion, etc. but hopefully this demonstrates the general concept.

 

Happy tweening.

Link to comment
Share on other sites

Wow, this is working pretty good, I tried to swap it over to a bezier to make smoothly rotate towards the new location, but that doesn't seem to work, is there a quick way to mke him rotate the point he moves to before he moves to it? currently he just moves around without any rotation.

Link to comment
Share on other sites

What would be the easiest way of doing this?

 

I was thinking that from within the function I could set a currentx and y position, then reset it after the tween was done but before the function is called again, and use that as the point of reference, but that would make it tween to its current spot (eg do nothing)

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...