Using for loop in my code [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
test
Prole
Posts: 28
Joined: Sun Apr 14, 2019 2:36 pm

Using for loop in my code [SOLVED]

Post by test »

I couldn't find a good title, sorry. I need to use for in my code. So I can do my job with less code.

Code: Select all

local touches = love.touch.getTouches()
	for i, id in ipairs(touches) do
		local x, y = love.touch.getPosition(id)
		if x < w / 2 then -- if player 1 touched her area
			if x > 200 then -- if player 1 touched her movement area
				p[1].xx = x -- set player 1's touchX (xx) to x
				p[1].yy = y
			else
				if y < 200 then -- if player 1 touched first skill rectangle
					if p[1].cd[1] <= 0 then -- if cooldown <= 0
						p[1].cd[1] = 1 -- set cooldown to 1
					end
				elseif y < 400 then -- if touched second skill rectangle
					if p[1].cd[2] <= 0 then
						p[1].cd[2] = 2
					end
				elseif y < 600 then
					if p[1].cd[3] <= 0 then
						p[1].cd[3] = 4
					end
				elseif y < 800 then
					if p[1].cd[4] <= 0 then
						p[1].cd[4] = 8
					end
				end
			end
		else  -- if player 2 touched her area
			if x < w - 200 then -- if player 2 touched her movement area
				p[2].xx = x
				p[2].yy = y
			else
				if y > h - 200 then -- if player 2 touched her first skill rectangle
					if p[2].cd[1] <= 0 then
						p[2].cd[1] = 1
					end
				elseif y > h - 400 then
					if p[2].cd[2] <= 0 then
						p[2].cd[2] = 2
					end
				elseif y > h - 600 then
					if p[2].cd[3] <= 0 then
						p[2].cd[3] = 4
					end
				elseif y > h - 800 then
					if p[2].cd[4] <= 0 then
						p[2].cd[4] = 8
				end
			end
		end
	end
full code is in the attachment. thanks in advance
Attachments
game.love
(3.76 KiB) Downloaded 59 times
Last edited by test on Tue May 07, 2019 7:14 pm, edited 1 time in total.
User avatar
zorg
Party member
Posts: 3444
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Using for loop in my code

Post by zorg »

Well, since the two players' areas are already separate, even if you used a for loop, you'd need to keep the area bounds in a table you could index easier; imo it's not worth the trouble, but i'm sure someone else will still provide that solution.
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.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 196 guests