Knockback (how to get the direction) [Solved]


(christopf) #1

Hi. I try to use a NumTween to get my hero knockbacked smooth. But my problem is, that the numtween aint getting any more far than the start value. can you maybe tell me what i forgot?

public function knockingBack():void
{			
	if (knock == true)
	{
		knodiz = new NumTween(knockbackComplete,0);
		knodiz.tween(10, 200, 5);
		addTween(knodiz, true);
			
		trace(knodiz.value);
			
		if ((letzteRichtung == "Nord") && (k != 0))
		{
			moveBy(0,knodiz.value,["solid","schlucht"],true);
		}
		else if ((letzteRichtung == "Sud") && (k != 0))
		{
			moveBy(0, -knodiz.value,["solid","schlucht"],true);
		}
		else if ((letzteRichtung == "West") && (k != 0))
		{
			moveBy(knodiz.value,0,["solid","schlucht"],true);
		}
		else if ((letzteRichtung == "Ost") && (k != 0))
		{
			moveBy(-knodiz.value,0,["solid","schlucht"],true);
		}			
	}
		
	if (k == 0)
	{
		knock = false; 
	}	
		
	if (knock = false)
	{
			
	}
}
	
private function knockbackComplete():void
{
	// stop sliding animation is missing
	knodiz.cancel();
	k = 0;
}

and do you have any advice for learning the tweening beside just playing around?

/to the code: knockingBack() is called in the update method and with knock:Boolean i shut off the possibility to give additional input


(christopf) #2

i guess my problem was that knockingback() was called through the update function which meant to start every time again. thats the reason the output was always the start value. so i moved it to the constructor and tried it another time with the override added() but it always start to count after entering the world and cancel did not work either. but this is a known problem i guess:

Continuing the discussion from What’s calling Tween.start()?:

so maybe the Math.sin and Math.cos could help with the knockback issue. i keep trying


(azrafe7) #3

I’ve filed an issue on the github repo as suggested by @jacobalbano and @zachwlewis in your linked post, but am not sure it’s the source of the problem in your specific case.

Well it could be, but don’t know how the other variables work (how’s k handled for example).


(christopf) #4

thanks for your reply. i already fixed my problem since there were 2 major mistakes: the tween and the position of the function called. so i rearranged this, dropped out the knock boolean (k is organizing everything now simultanously with the time), using a normal timer with math.sqrt. this looks good for me now, the code is cleaner and it works. but i have another problem, since i keep a container for the direction my hero is facing and upon that value my hero get knocked back. this works awkward when the hero get hit from the back so he get thrown through the enemy entity. i was thinking about using the entity.left (or right bot …) value but to now i didnt get it work (but i hadnt that much time for it yet)


(christopf) #5

since the knockbacking itself is working i’m wondering right now how to determine the direction where the collision happens. do you know if there is maybe a special method in FP for this? to now i tried it with collision type check and the left,right,top,bottom:

if (collide("schatten",left,bottom)) { KnockSeite = 1; trace("WEST Y") }
else if (collide("schatten",right,bottom)) { KnockSeite = 2; trace("OST Y") }
else if (collide("schatten",left-5,top)) { KnockSeite =3; trace("NORD Y") }
else if (collide("schatten",left-5,bottom)) { KnockSeite = 4; trace("SUD Y") }

but i get only west, north and east…south aint working. if i switch the x and y value a bit, i get south instead of north. west and east always overlays on y direction. thats my status quo. is it stupid this way?


(David Williams) #6

This is the code I use for knockback in one of my games (decompiled, please excuse the variable names):

public function moveFromPlayer(param1:Number, param2:Entity) : void
{
	var _loc_3:int = FP.angle(this.x, this.y, param2.x, param2.y);
	_loc_3 = _loc_3 * FP.RAD;
	param2.moveBy(Math.cos(_loc_3) * param1, Math.sin(_loc_3) * param1);
}

(Jacob Albano) #7

Cleaned up a bit:

public function moveFromPlayer(strength:Number, enemy:Entity)
{
    var angle:Number = FP.angle(x, y, enemy.x, enemy.y);
    angle *= FP.RAD;
    enemy.moveBy(Math.cos(angle) * strength, Math.sin(angle) * strength);
}

(christopf) #8

Thank you both. Your code @Deamonr made me heading into FP.angle and i was playing with it for a few hours now and in combination with the tutorial to accessing entities of chevy ray you transported @jacobalbano i got this working. now when i see your code totally encrypted deamonr im impressed and thinking about the possibility to not just get knocked back into one of the four cardinal directions. makes me dream. but for now this is enough. i go to some other stuff that got to be done with this thread marked to come back when i’ve muse again.