please delete this thread. (moved thread to support)

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
onedaysnotice
Citizen
Posts: 63
Joined: Sun May 13, 2012 2:49 am

please delete this thread. (moved thread to support)

Post by onedaysnotice »

**How do I delete a thread? o.o

||===========onedaysnotice help thread============||


Before i infuriate the whole love2d community with my incessant making of 'help me' and 'how to' threads, I've decided to create a centralised thread xD

Current problem: viewtopic.php?p=59997#p59997

-------

Okay, to the point. Why does only the player 2 selector collide with the buttons? :s I can't see why the player 1 selectors can't :S

Code: Select all

menu = {}
button_menu = {}

function menu.load()

	boxImg = love.graphics.newImage("images/placeholder_button.png")

	medium = love.graphics.newFont(45)
	menu_button_spawn(screenWidth/8, screenHeight/5, boxImg, 001)
	menu_button_spawn(screenWidth/8, (screenHeight/5)*2, boxImg, 002)
	menu_button_spawn(screenWidth/8, (screenHeight/5)*3, boxImg, 003)
	menu_button_spawn(screenWidth/8, (screenHeight/5)*4, boxImg, 004)

	menuSelector = {}

	menuSelector[1] = {
		x = screenWidth/2 - 1,
		y = screenHeight/2 
	}

	menuSelector[2] = {
		x = screenWidth/2 + 1,
		y = screenHeight/2
	}

	menuSelectorVel = 300

end

function menu.update(dt)

	for p = 1,2 do

		--======SELECTOR MOVEMENT======--

		if love.keyboard.isDown(pControl[p].Right) then

			menuSelector[p].x = menuSelector[p].x + menuSelectorVel * dt

			if love.keyboard.isDown(pControl[p].Up) then

				menuSelector[p].y = menuSelector[p].y - menuSelectorVel * dt

			elseif love.keyboard.isDown(pControl[p].Down) then

				menuSelector[p].y = menuSelector[p].y + menuSelectorVel * dt

			end

		elseif love.keyboard.isDown(pControl[p].Left) then

			menuSelector[p].x = menuSelector[p].x - menuSelectorVel * dt

			if love.keyboard.isDown(pControl[p].Up) then

				menuSelector[p].y = menuSelector[p].y - menuSelectorVel * dt

			elseif love.keyboard.isDown(pControl[p].Down) then

				menuSelector[p].y = menuSelector[p].y + menuSelectorVel * dt

			end

		elseif love.keyboard.isDown(pControl[p].Down) then

			menuSelector[p].y = menuSelector[p].y + menuSelectorVel * dt

		elseif love.keyboard.isDown(pControl[p].Up) then

			menuSelector[p].y = menuSelector[p].y - menuSelectorVel * dt

		end

		--======BUTTON COLLISION======--

		for i,v in ipairs(button_menu) do

			if menu_inRect(menuSelector[p].x, menuSelector[p].y, 0, 0, v.x, v.y, v.img:getWidth(), v.img:getHeight()) then

				v.inFocus = true

			else 

				v.inFocus = false

			end
		end
	end
end

function menu.draw()

	love.graphics.setColor(255,255,255)
	love.graphics.rectangle("fill",0,0,screenWidth, screenHeight)
	menu_button_draw()


	love.graphics.setColor(0, 255,0)
	for p = 1,2 do

		love.graphics.circle("fill", menuSelector[p].x, menuSelector[p].y, 3, 10)

	end

end

function menu.keypressed(key,unicode)

	if key == "escape" then

		state = "splash"
		button_fade = 255
		blackscreen = 0

	end

end

function menu_button_spawn(x, y, img, id)
	table.insert(button_menu, {x = x, y = y, img = img, id = id, inFocus = false})
end

function menu_button_draw()
	for i,v in ipairs(button_menu) do

		if v.inFocus == false then
			love.graphics.setColor(0,0,255)

		elseif v.inFocus == true then

			love.graphics.setColor(255,0,0)
		end

		love.graphics.draw(v.img,v.x,v.y)
	end
end

function menu_inRect(ax, ay, aw, ah, bx, by, bw, bh)
   if ax > (bx + bw) or (ax + aw) < bx then return false end
   if ay > (by + bh) or (ay + ah) < by then return false end
   return true
end
The problem should be only on menu.lua.

player1 moves with WASD, player2 moves with arrow keys
Attachments
Archive.love
(11.45 KiB) Downloaded 76 times
Last edited by onedaysnotice on Sun Jul 01, 2012 1:47 am, edited 2 times in total.
Santos
Party member
Posts: 384
Joined: Sat Oct 22, 2011 7:37 am

Re: Collision only happens for one player :s

Post by Santos »

Code: Select all

--======BUTTON COLLISION======--

      for i,v in ipairs(button_menu) do

         if menu_inRect(menuSelector[p].x, menuSelector[p].y, 0, 0, v.x, v.y, v.img:getWidth(), v.img:getHeight()) then

            v.inFocus = true

         else

            v.inFocus = false

         end
      end
When p is 1, and menuSelector[1] is over a button, v.inFocus will be set to true. However, when p is 2, and menuSelector[2] isn't over a button, v.inFocus will be set to false, even if menuSelector[1] is over a button.

Here is one possible solution:

Code: Select all

for i,v in ipairs(button_menu) do
	v.inFocus = false

	for p = 1,2 do

		if menu_inRect(menuSelector[p].x, menuSelector[p].y, 0, 0, v.x, v.y, v.img:getWidth(), v.img:getHeight()) then
			v.inFocus = true
		end

	end
end
onedaysnotice
Citizen
Posts: 63
Joined: Sun May 13, 2012 2:49 am

Re: Collision only happens for one player :s

Post by onedaysnotice »

Santos wrote:

Code: Select all

--======BUTTON COLLISION======--

      for i,v in ipairs(button_menu) do

         if menu_inRect(menuSelector[p].x, menuSelector[p].y, 0, 0, v.x, v.y, v.img:getWidth(), v.img:getHeight()) then

            v.inFocus = true

         else

            v.inFocus = false

         end
      end
When p is 1, and menuSelector[1] is over a button, v.inFocus will be set to true. However, when p is 2, and menuSelector[2] isn't over a button, v.inFocus will be set to false, even if menuSelector[1] is over a button.

Here is one possible solution:

Code: Select all

for i,v in ipairs(button_menu) do
	v.inFocus = false

	for p = 1,2 do

		if menu_inRect(menuSelector[p].x, menuSelector[p].y, 0, 0, v.x, v.y, v.img:getWidth(), v.img:getHeight()) then
			v.inFocus = true
		end

	end
end
ahh i see thanks :D
Post Reply

Who is online

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