Page 1 of 1

Enemy Knockback Problem

Posted: Wed May 20, 2015 10:20 pm
by dubem
Hey Gang!

I'm trying to make a little fighting game and I'm very happy with what I have right now.

The problem is that if I attack the enemy from the left side and he is looking left, the knockback won't work.

It works fine from the right side and from both sides if the enemy is looking right.

Maybe someone has time to look over my code. I tried to keep it as simple as I can.

The green character is controled by the left and right arrow keys and "x" for jumping und "c" for attacking.

If something else is wrong with the code, please feel free to tell me.

Thank you guys and greetings from Berlin

- Alex

Re: Enemy Knockback Problem

Posted: Wed May 20, 2015 11:04 pm
by BOT-Brad
I think I fixed your bug :awesome:. In player.lua the mapColl function, you had this;

Code: Select all

-- LEFT/RIGHT COLLISION
if self.xvel < 0 then
	if not tileCollision(map,self,"left") then
		self.x = self.newX
	else
		self.x = self.newX + map.tilesize - (self.newX % map.tilesize)
		self.xvel = 0
	end
elseif self.dir == "right" then
	if not tileCollision(map,self,"right") then
		self.x = self.newX
	else
		self.x = self.newX - ((self.newX + self.w) % map.tilesize)
		self.xvel = 0
	end
end
Fixed code in player.lua

Code: Select all

if self.xvel < 0 then
	if not tileCollision(map,self,"left") then
		self.x = self.newX
	else
		self.x = self.newX + map.tilesize - (self.newX % map.tilesize)
		self.xvel = 0
	end
elseif self.xvel > 0 then	
	if not tileCollision(map,self,"right") then
		self.x = self.newX
	else
		self.x = self.newX - ((self.newX + self.w) % map.tilesize)
		self.xvel = 0
	end
end
The elseif self.dir == "right" was unnecessary, you only needed to check if the player had a non-zero xval property. I must say I enjoyed punching the red dino for way too long :ultraglee:.

Re: Enemy Knockback Problem

Posted: Thu May 21, 2015 7:06 am
by dubem
Thanks man!

Remember: before you recycle your old code, check it for the new purpose!

I try to keep you updated on this project.

-- Alex