Enemies crash game, need help. (SOLVED)

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
User avatar
Rafer45
Prole
Posts: 12
Joined: Wed Apr 10, 2013 3:22 pm

Enemies crash game, need help. (SOLVED)

Post 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!
Last edited by Rafer45 on Tue Sep 03, 2013 9:29 pm, edited 7 times in total.

Code: Select all

while game.engine == "LÖVE2D" do
	game.awesome = true
	community.helpful = true
end
User avatar
Sheepolution
Party member
Posts: 264
Joined: Mon Mar 04, 2013 9:31 am
Location: The Netherlands
Contact:

Re: Enemies not working, need help.

Post by Sheepolution »

Psst, you forgot to attach the file :P
User avatar
Rafer45
Prole
Posts: 12
Joined: Wed Apr 10, 2013 3:22 pm

Re: Enemies not working, need help.

Post 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.
Last edited by Rafer45 on Sun Sep 01, 2013 3:27 pm, edited 1 time in total.

Code: Select all

while game.engine == "LÖVE2D" do
	game.awesome = true
	community.helpful = true
end
User avatar
veethree
Inner party member
Posts: 875
Joined: Sat Dec 10, 2011 7:18 pm

Re: Enemies not working, need help.

Post 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.
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Enemies crash game, need help.

Post 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
User avatar
Rafer45
Prole
Posts: 12
Joined: Wed Apr 10, 2013 3:22 pm

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

Post by Rafer45 »

Thanks for the help!
I'm going to look over the code right now :3

Oi, what does the hashtag do?

Code: Select all

while game.engine == "LÖVE2D" do
	game.awesome = true
	community.helpful = true
end
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

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

Post 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.
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
User avatar
Qcode
Party member
Posts: 170
Joined: Tue Jan 10, 2012 1:35 am

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

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

Who is online

Users browsing this forum: pgimeno and 98 guests