My code does not work [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

My code does not work [SOLVED]

Post by test »

Why doesn't my code work? And any suggestions? I am new to Lua.

Code: Select all

local p1 = {}
local p2 = {}
local p1.touch = {}
local p2.touch = {}
function love.load()
	p1.img = love.graphics.newImage('player.png')
	p1.x = p1.img:getWidth() / 2
	p1.y = love.graphics.getHeight() / 2
	p1.touch.x = p1.x
	p1.touch.y = p1.y
	p1.speed = 500
	p2.img = p1.img
	p2.x = love.graphics.getWidth() - p2.img:getWidth() / 2
	p2.y = love.graphics.getHeight() / 2
	p2.touch.x = p2.x
	p2.touch.y = p2.y
	p2.speed = 500
end
function love.update(dt)
	local touches = love.touch.getTouches()
	for i, id in ipairs(touches) do
		local x, y = love.touch.getPosition(id)
		if x < love.graphics.getWidth() / 2 then
			p1.touch.x = x
			p1.touch.y = y
		else
			p2.touch.x = x
			p2.touch.y = y
		end
	end
	local distance1 = math.dist(p1.x, p1.y, p1.touch.x, p1.touch.y)
	local stepSize1 = p1.speed * 2.5 * dt
	if distance1 > stepSize1 then
		p1.x = p1.x + (p1.touch.x - p1.x) / distance1 * stepSize1
		p1.y = p1.y + (p1.touch.y - p1.y) / distance1 * stepSize1
	end
	local distance2 = math.dist(p2.x, p2.y, p2.touch.x, p2.touch.y)
	local stepSize2 = p2.speed * 2.5 * dt
	if distance2 > stepSize2 then
		p2.x = p2.x + (p2.touch.x - p2.x) / distance2 * stepSize2
		p2.y = p2.y + (p2.touch.y - p2.y) / distance2 * stepSize2
	end
end
function love.draw()
	love.graphics.draw(p1.img, p1.x - p1.img:getWidth() / 2, p1.y - p1.img:getHeight() / 2)
	love.graphics.draw(p2.img, p2.x - p2.img:getWidth() / 2, p2.y - p2.img:getHeight() / 2)
end
function math.dist(x1, y1, x2, y2) return ((x2 - x1) ^ 2 + (y2 - y1) ^ 2) ^ 0.5 end
Last edited by test on Tue May 07, 2019 7:11 pm, edited 1 time in total.
User avatar
steVeRoll
Party member
Posts: 131
Joined: Sun Feb 14, 2016 1:13 pm

Re: My code does not work

Post by steVeRoll »

Hi and welcome to the forums! There seem to be syntax errors on lines 3 and 4: you don't have to write "local" before defining a field of another table.
Please be a little more specific and describe the error you're getting.
Post Reply

Who is online

Users browsing this forum: No registered users and 41 guests