Jump to content
Search Community

How to move my sprite along a sine wave?

test84 test
Moderator Tag

Recommended Posts

Hi,

 

I'm making a flying enemy, like how you see in most platformers, from NES's Ninja Gaiden to God of War, and I can make it go and stay on top of player's head but after that I need to make it move to left and right in shape of a sine wave while maintaining it's position on top of the player.

(I mean it should go up and down and left and right. to be more precise: it's x should be varying from like player.x - 40 to player.x + 40 and it's y should be from player'sTop.y - 40 to player'sTop.y + 40 but keep form of a sine wave, hope I made myself clear enough)

 

I'm using Tweenmax for the first part, so you can give me examples on using that, I saw a Sine ease in but I want to move in both directions so I couldn't use it.

 

tnx!

Link to comment
Share on other sites

you can either use the bezierThrough plugin. visit http://www.tweenmax.com and look in the plugin explorer.

 

or do something like this:

 

import com.greensock.*;
import com.greensock.easing.*;


TweenMax.to(b, 6, {x:400, ease:Linear.easeNone});
TweenMax.to(b, 1, {y:"100", repeat:5, yoyo:true, ease:Sine.easeInOut});

 

Thanks, I tried your code but it moves very very linearly .

Link to comment
Share on other sites

you could also use TweenMax to move an object along the x axis and call onUpdate function that runs your own calculation on the y.

 

this might help:

http://www.splung.com/content/sid/2/page/shm

 

I made this 6 years ago with a basic onEnterFrame script:

http://doyouhaveapen.com/junkyart/sinulator.html

 

I was going to turn the ball into a pig that you threw into a pile of birds but thought it was too silly.

 

the GreenSock Tweening Platform doesn't support want you want directly but there are plenty of ways to get a sine wave style animation with AS3 and perhaps a touch of TweenLite.

Link to comment
Share on other sites

Thanks for your interest in this topic,

I currently use a precalculated array of cosine and use it as:

TweenMax.to(this, 2, { bezier:[ {  x:Registry.player.x + xIterator} ] } );
			TweenMax.to(this, 2, { bezier:[ {  y:Registry.player.y - 40 + sineWavePreCalculated[sineWaveIterator] } ] } );

But the results are fixed and I can't have randomness in it. Since this is movement for my enemies, they will move just like each other so if I add a little random number anywhere, it will break apart. That's why I wanted to use TweenMax's own sine methods.

 

If only you could help me to replace second line's precalculated cosine values with help of TweenMax, that would be great.

Link to comment
Share on other sites

I'm sorry, I don't understand what you mean by "TweenMax's own sine methods".

 

if you were able to pre-calculate the values I'd say you're much more ahead of the curve than me when it comes to trig.

 

I just found it somewhere but because it limits my movements, as I mentioned I can add variations to it, I wanted to find a way to do it with Tweenmax because I know somewhere inside it has this functionality but I can't just find it via preview demos on the site.

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