ZFGC
Resources => Coding => Topic started by: ZoSo on August 18, 2012, 04:28:54 pm
-
I'm trying to add knock back to my engine so that when Link gets hit by an enemy or projectile, he gets pushed back 2 or 3 pixels. I have a terrible excuse for one implemented right now, but it's not accurate at all :/
Right now I only have 1 enemy in my engine, an octorock, and want Link to be pushed back when the rock hits him. I have this code in the rock object to detect what direction it should move:
link_pos = point_direction(objOctorok.x,objOctorok.y,objLink.x+1,objLink.y+20)
This works great. I was using kind of the same method to figure out which direction Link should be knocked back, but it doesnt take into consideration negative x and y values :/ Help anyone?
-
What you need is a normalized vector, I'm not sure how this would be done effectively in GM but I know it's possible.
http://www.fundza.com/vectors/normalize/index.html
-
You've got his direction all that's left is to move him. Typically you would calculate the x and y components of your vector with cosine and negative sine respectively, but Game Maker provides simple functions for doing it as well.
lengthdir_x(len,dir)
lengthdir_y(len,dir)
On another note, why are you adding 1 and 20 to the player's x and y values? If it's to adjust for his center things will be WAAY easier for you if you move the offsets on his sprites to his center point instead. That way his x and y values will already be at his center.