Page 1 of 1

Attacking Functions.Please Help![.love]

Posted: Sat Dec 24, 2011 6:46 pm
by baconhawka7x
I have made a flat grass plane, and put two zombies and steve(my character) inside of it. To draw the zombies I use..

Code: Select all

function spawnZombie(x,y,speed,health,detection)
		table.insert(enemies, {x = x, y = y, speed = speed, health = health, detection = detection, pic = love.graphics.newImage("pics/zombie.png")})
		
	end
and inside of love draw I put..

Code: Select all

for i,v in ipairs(enemies) do
		love.graphics.draw(v.pic, v.x, v.y)
		
		
	end
I will put my .love up for everyone to look at. But the problem is with the way I make the player attack the zombies. The code for the enemy attacking the player is at the bottom of "tables.lua" under the "enemy_attack" function. And the Code for steve attacking the enemy is in controlls.lua.


For some reason you can only attack the second zombie drawn. I think I know why though. I'm pretty sure it has to do with this little bit of code right here..

Code: Select all

if key == "j" and
			side == right and
			steve.x > v.x - steve.reach and
			steve.x < v.x + steve.reach then
				v.health = v.health - steve.damage
				v.x = v.x - 30
				v.y = v.y - 15
			end
sence there are two "v"'s I'm pretty sure it's messing that up. So how would I re-write that?

Any and all help is appreciated:)

P.S. use "a,d" to move, "w" to fly(temp), and "J" to attack

Re: two zombies one room.

Posted: Sat Dec 24, 2011 7:16 pm
by osgeld
I was able to attack the first zombie after killing the second :crazy:

Re: two zombies one room.

Posted: Sat Dec 24, 2011 7:24 pm
by baconhawka7x
osgeld wrote:I was able to attack the first zombie after killing the second :crazy:
I know, but I want them to be able to kill them in any order haha:)

Re: two zombies one room.

Posted: Sat Dec 24, 2011 7:26 pm
by All0utWar
Couldn't you just make it so the second zombie goes into the room first?

Re: two zombies one room.

Posted: Sat Dec 24, 2011 7:39 pm
by baconhawka7x
All0utWar wrote:Couldn't you just make it so the second zombie goes into the room first?
Well not exactly. Because what if the player jumps over the first zombie and goes to the second?(I haven't gotten to writing jumping yet lol). Also I think it would be best for the future of the game, like if there is a zombie on either side of the player.

Re: two zombies one room.

Posted: Sat Dec 24, 2011 9:02 pm
by Lafolie
You can save lots of memory by doing this instead:

Code: Select all

--...in love.load
img_zombie = love.graphics.newImage("pics/zombie.png")

--...spawn function
function spawnZombie(x,y,speed,health,detection)
      table.insert(enemies, {x = x, y = y, speed = speed, health = health, detection = detection, pic = img_zombie})
end
It'll help cut lag too since loading images during 'gameplay' could potentially cause a short pause.

I'm pretty certain you could use pairs instead of ipairs in the draw loop too.

To fix the attacking thing you need to iterate through all the zombies you put in the enemies table and check them all. You did this with the draw loop, it's exactly the same principal.

Re: two zombies one room.

Posted: Sat Dec 24, 2011 9:43 pm
by baconhawka7x
Lafolie wrote:To fix the attacking thing you need to iterate through all the zombies you put in the enemies table and check them all. You did this with the draw loop, it's exactly the same principal.
I did though. That's why I'm confused. and thanks for that pre loaded pic. Totally slipped my mind.