Page 1 of 1

Enemies crash game, need help. (SOLVED)

Posted: Sat Aug 31, 2013 7:55 pm
by Rafer45
So I lost my drive (as in motivation) back in may, so I'm trying to get it back with some coding. Not really working at the moment. :(
I coded some enemies into my game and thought I had all I needed to make it work, but apparently not. I have no idea what I did wrong, and my code all looks like jumble that makes sense, but not cool or fun jumble that makes sense like before.

Point is, I've no idea what is wrong with my enemies and why they won't spawn. I've attached the file. Because I've never understood how to use love.keypressed more than once, the problem could be in love.menu, where all of my love.keypressed things are. I'm aware it is inefficient, but I've reached a point when I feel it's unimportant to be efficient.

So, the answer to my drive problem or just to my enemy problem would both be greatly appreciated :/

The file should be attached now
veethree wrote:You can go into your control panel and delete some unnecessary attachments.
Thanks!

Re: Enemies not working, need help.

Posted: Sat Aug 31, 2013 8:54 pm
by Sheepolution
Psst, you forgot to attach the file :P

Re: Enemies not working, need help.

Posted: Sun Sep 01, 2013 1:35 am
by Rafer45
So, just to clarify everything, in the game you move with wasd and shoot with the arrow keys. The enemy is spawned by pressing the spacebar, in the coordinates 10x 10y. When spacebar is pressed, though, the game crashes. The enemy is supposed to send you back to the menu once it hits you, and it's supposed to be removed from the table when hit by bullets.

Re: Enemies not working, need help.

Posted: Sun Sep 01, 2013 3:02 am
by veethree
Rafer45 wrote:So apparently, even tough I've already attached the file several times, It's not showing up, and the board attachment quota has been reached.

So I guess I'm going to ask permission to make this post again :/
You can go into your control panel and delete some unnecessary attachments.

Re: Enemies crash game, need help.

Posted: Tue Sep 03, 2013 5:51 am
by micha
Here are some sidenotes:
In the main loop (in the update-function) you call the function escape and inside there you redefine the function love.keypressed. You do this each frame. It is enough to do this once. So put the definition of love.keypressed directly into the main.lua (that also makes it easier to find where the error is)

The bullet loop is like this:

Code: Select all

for i,v in ipairs(bullet) do
  [...]
  if v.x > 1500 or v.x < 0 - v.width or v.y > 900 or v.y < 0 - v.width then
    table.remove(bullet, i)
  end
end
That is likely to cause problems. The for loop traverses the numbers from 1 to n. But if you remove bullet i (in the center) then you change the table "bullet" while you loop over it. The save way is this (move backwards through the bullets):

Code: Select all

for i = #bullet,1,-1 do
  v = bullet[i]
  [...]
  if v.x > 1500 or v.x < 0 - v.width or v.y > 900 or v.y < 0 - v.width then
    table.remove(bullet, i)
  end
end

Now for the error:
The original error of your code is in enemy.lua:

Code: Select all

while player.xCenter > v.x do
  v.xvel = v.xvel + v.speed*dt
end
This is an infinite while loop. The condition itself does not change so the while loop will never finish. You need an if-condition here:

Code: Select all

if player.xCenter > v.x then
  v.xvel = v.xvel + v.speed*dt
end
Here is the corrected enemiesUpdate (note, I also added two lines at the end and I corrected a wrong sign in the acceleration part. Please compare carefully with your original code)

Code: Select all

function enemiesUpdate(dt)
	for i,v in ipairs(enemies) do
		v.xvel = v.xvel * (1 - math.min(dt*v.friction, 1))
		if player.xCenter > v.x then
			v.xvel = v.xvel + v.speed*dt
		end
		if player.xCenter < v.x then
			v.xvel = v.xvel - v.speed*dt
		end
		if player.yCenter > v.y then
			v.yvel = v.yvel + v.speed*dt
		end
		if player.yCenter < v.y then
			v.yvel = v.yvel - v.speed*dt
		end
	v.x = v.x+v.xvel
	v.y = v.y+v.yvel
	end
end

Re: Enemies crash game, need help. (SOLVED)

Posted: Tue Sep 03, 2013 9:28 pm
by Rafer45
Thanks for the help!
I'm going to look over the code right now :3

Oi, what does the hashtag do?

Re: Enemies crash game, need help. (SOLVED)

Posted: Wed Sep 04, 2013 12:24 am
by davisdude
Technically referred to as an 'octothrope', it gets the length of a table. :awesome:
Dropppin' the knowledge! :ultraglee:
Example:

Code: Select all

fruits = {
    apple = { 'red', 'green' }, 
    banana = 'yellow', 
    pear = 'green', 
}

print(#fruits) --prints 3
print(#fruits.apple) --prints 2
--NOTE: fruits.apple = fruits['apple']
--Whichever you prefer you can use.

Re: Enemies crash game, need help. (SOLVED)

Posted: Wed Sep 04, 2013 1:37 am
by Qcode
The # sign can also be used to get the length of a string (an alternative to string.len).

Code: Select all

cat = "keyboard"
print(#cat) -- prints 8