Freeze when running on iOS

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
slmjkdbtl
Prole
Posts: 14
Joined: Sat Feb 25, 2017 8:36 am
Location: New York
Contact:

Freeze when running on iOS

Post by slmjkdbtl »

Hi! I'm new to love2d and lua, I'm not sure if this piece of code is written in best practice because sometime it causes my phone to freeze when running the function. (the code is just to generate a new coordinate of 'food')

Is there anything wrong with the looping?

Code: Select all

function food.update()

	local rand = love.math.random

	x = rand(field.innerX + food.width * 2, field.innerX + field.innerWidth - food.width * 3)
	y = rand(field.innerY + food.height * 2, field.innerY + field.innerHeight - food.height * 3)

	while (x >= snave.x - snave.width * 2 and
		   x <= snave.x + snave.width * 3 and
		   y >= snave.y - snave.height * 2 and
		   y <= snave.y + snave.height * 3) do

		x = rand(field.innerX + food.width * 2, field.innerX + field.innerWidth - food.width * 3)
		y = rand(field.innerY + food.height * 2, field.innerY + field.innerHeight - food.height * 3)	

	end

	food.x = x
	food.y = y

end
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Freeze when running on iOS

Post by raidho36 »

See if the boundary of generated coordinates can be reached by the function you're using. If not, it will never generate valid coordinates and will loop forever.
User avatar
zorg
Party member
Posts: 3441
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Freeze when running on iOS

Post by zorg »

I'm pretty sure that you can optimize that loop out into simpler min/max operations:

Code: Select all

function food.update()

	local rand = love.math.random

	local xmin = math.max(snave.x + snave.width * 3, field.innerX + food.width * 2)
	local xmax = math.min(snave.x - snave.width * 2, field.innerX + field.innerWidth - food.width * 3)

	local ymin = math.max(snave.y + snave.height * 3, field.innerY + food.height * 2)
	local ymax = math.min(snave.y - snave.height * 2, field.innerY + field.innerHeight - food.height * 3)

	food.x = rand(xmin, xmax)
	food.y = rand(ymin, ymax)

end
This is basically what you want to do, based on the code; check whether it makes sense for you, and if not, maybe you'll find your issue easier like this.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
slmjkdbtl
Prole
Posts: 14
Joined: Sat Feb 25, 2017 8:36 am
Location: New York
Contact:

Re: Freeze when running on iOS

Post by slmjkdbtl »

raidho36 wrote: Sat Feb 25, 2017 10:52 am See if the boundary of generated coordinates can be reached by the function you're using. If not, it will never generate valid coordinates and will loop forever.
It could be reached and it runs normally on macOS
嗡嗡嗡
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Freeze when running on iOS

Post by raidho36 »

Well then it could be that it takes very long time on your phone, there's no JIT compilation enabled and it's not very fast to start with, too.
Post Reply

Who is online

Users browsing this forum: No registered users and 74 guests