The Daily Click ::. Forums ::. Klik Coding Help ::. Smooth direction change
 

Post Reply  Post Oekaki 
 

Posted By Message

Bjoern



Registered
  08/04/2013 19:51:09
Points
  8
8th April, 2013 at 08/04/2013 20:01:44 -

Hello! I'm trying to make a game with an enemy that would move somewhat like a Medusa-head from Castlevania. You know, a constant X-value to either left or right and then an Y-value that switches after a certain height from positive to negative and vice versa. I managed to do this, it was simple enough but it doesn't look good at all. Is there any good way to "smooth out" when the Y is near the top/bottom of the desired height? Right now it only looks like a bouncing ball that has the same constant speed. :/ Would really appreciate any feedback as I can't figure it out.

 
n/a

s-m-r

Slow-Motion Riot

Registered
  04/06/2006
Points
  1078

Candle
8th April, 2013 at 08/04/2013 21:11:30 -

What I would recommend is have the Object controlled with a Flag and an Alterable Value.

FLAG = "Is the object moving to the left or to the right?" For example, On = Left. Off = Right.
ALTERABLE VALUE A = "When should the Object change directions?" This is a constantly-increasing timer. At a certain point, the timer resets to 0. The example below resets this at 10, but you can change it to pretty much any number you please.

See the example code below. I'm not at my MMF2 computer, but this ought to give you a fair idea of one way to achieve this effect.



IF Always
THEN Add 1 to Alterable Value A

IF Flag 0 = On
AND Always
THEN Set X Position of "Active Object" to (X ( "Active Object" )+ Alterable Value A)

IF Flag 0 = Off
THEN Set X Position of "Active Object" to (X ( "Active Object" )- Alterable Value A)

IF Flag 0 = On
AND IF Alterable Value A >10
THEN Set Flag 0 to Off

IF Flag 0 = Off
AND IF Alterable Value A >10
THEN Set Flag 0 to On



Edited by s-m-r

 
n/a

Bjoern



Registered
  08/04/2013 19:51:09
Points
  8
8th April, 2013 at 08/04/2013 21:38:27 -

Thanks for the reply man! I tried to do as you described but it didn't work at all! :/ Perhaps I did something wrong in this example?

http://www.mediafire.com/?5yhchmg35eb49s8

 
n/a

Sketchy

Cornwall UK

Registered
  06/11/2004
Points
  1971

VIP MemberWeekly Picture Me This Round 43 Winner!Weekly Picture Me This Round 47 WinnerPicture Me This Round 49 Winner!
8th April, 2013 at 08/04/2013 21:40:48 -

I'd just use a simple sin wave movement:

+ Always
-> Enemy: Set X position to X("Enemy") + 2
-> Enemy: Add 3 to YOffset
-> Enemy: Set Y position to YStart("Enemy") + (sin(YOffset("Enemy")) * MaxOffset)

where:
YStart = the vertical midpoint of the enemy's movement (set it at the start of the frame or when the enemy is created)
YOffset = just an alterable value...
MaxOffset = the amount of variation you want in Y position (eg. if MaxOffset = 50 and YStart = 100, then the Y position will vary between 50 and 150)

EDIT: In fact, there's a built-in sin wave movement that ought to work.


Edited by Sketchy

 
n/a

Bjoern



Registered
  08/04/2013 19:51:09
Points
  8
8th April, 2013 at 08/04/2013 22:13:31 -

Thank you so very much Sketchy!! This was perfect! I didn't want to use any in-built things, but this was just what I was looking for, cheers!

 
n/a

s-m-r

Slow-Motion Riot

Registered
  04/06/2006
Points
  1078

Candle
8th April, 2013 at 08/04/2013 23:25:52 -

Thanks for pointing out all that stuff, Sketchy. I was at my day job desk, and muddled through a few thoughts during a break.

 
n/a
   

Post Reply



 



Advertisement

Worth A Click