Enemy Knockback Problem

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
dubem
Prole
Posts: 8
Joined: Fri Mar 27, 2015 1:42 pm

Enemy Knockback Problem

Post 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
Attachments
minifight.zip
Mini Fight
(192.69 KiB) Downloaded 102 times
User avatar
BOT-Brad
Citizen
Posts: 87
Joined: Tue Dec 02, 2014 2:17 pm
Location: England

Re: Enemy Knockback Problem

Post 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:.
Follow me on GitHub! | Send me a friend request on PSN!
dubem
Prole
Posts: 8
Joined: Fri Mar 27, 2015 1:42 pm

Re: Enemy Knockback Problem

Post 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
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 0 guests