Help about 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

Help about my code [SOLVED]

Post by test »

I couldn't figure out. :( what is the error?

Code: Select all

local w
local h
local p = {}
p[1] = {}
p[2] = {}
p[1].slot = {}
p[1].slot[1] = {}
p[1].slot[2] = {}
p[1].slot[3] = {}
p[1].slot[4] = {}
p[2].slot = {}
p[2].slot[1] = {}
p[2].slot[2] = {}
p[2].slot[3] = {}
p[2].slot[4] = {}
local spell = {}
spell[1] = {}
spell[2] = {}
spell[3] = {}
spell[4] = {}
spell[5] = {}
local p = {}
p[1] = {}
p[2] = {}
function love.load()
	w, h = love.graphics.getDimensions()
	p[1].x = w / 4
	p[2].x = 3 * w / 4
	spell[1].img = love.graphics.newImage('watersplash.png')
	spell[2].img = love.graphics.newImage('darkness.png')
	spell[3].img = love.graphics.newImage('fireball.png')
	spell[4].img = love.graphics.newImage('poisondrop.png')
	spell[5].img = love.graphics.newImage('lightning.png')
	for i = 1, 2 do
		p[i].img = love.graphics.newImage('p.png')
		p[i].y = h / 2
		p[i].w = p[i].img:getWidth()
		p[i].h = p[i].img:getHeight()
		p[i].xx = p[i].x
		p[i].yy = p[i].y
		p[i].slot[1] = spell[love.math.random(1, #spell)]
		p[i].slot[2] = spell[love.math.random(1, #spell)]
		p[i].slot[3] = spell[love.math.random(1, #spell)]
		p[i].slot[4] = spell[love.math.random(1, #spell)]
	end
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 < w / 2 then
			if x > 150 then
				p[1].xx = x
				p[1].yy = y
			end
		else
			if x < w - 150 then
				p[2].xx = x
				p[2].yy = y
			end
		end
	end
	for i = 1, 2 do
		local distance = math.dist(p[i].x, p[i].y, p[i].touch.x, p[i].touch.y)
		local stepSize = p[i].speed * dt

		if distance > stepSize then
			p[i].x = p[i].x + (p[i].touch.x - p[i].x) / distance * stepSize
			p[i].y = p[i].y + (p[i].touch.y - p[i].y) / distance * stepSize
		end
	end
end
function love.draw()
	love.graphics.draw(p[1].slot[1].img, 0, 0)
	love.graphics.draw(p[1].slot[2].img, 0, 200)
	love.graphics.draw(p[1].slot[3].img, 0, 400)
	love.graphics.draw(p[1].slot[4].img, 0, 600)
	love.graphics.draw(p[2].slot[1].img, w - 200, h - 200)
	love.graphics.draw(p[2].slot[2].img, w -200, h - 400)
	love.graphics.draw(p[2].slot[3].img, w - 200, h - 600)
	love.graphics.draw(p[2].slot[4].img, w - 200, h - 800)
end
Last edited by test on Tue May 07, 2019 7:09 pm, edited 1 time in total.
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: Help about my code

Post by grump »

The most glaring error is that you did not ask a real question.

At least have the courtesy to mention what error you're trying to solve and how it differs from the expected result. And if possible at all, please post something that can be analyzed with minimum effort. A love file that shows the error is a good starting point to get useful answers. Guesswork sucks.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Help about my code

Post by raidho36 »

First, you overwrite p[1] and p[2] with a pair of empty tables after defining p[1] and p[2] to contain slots and everything. Second, you never write "touch" field to p[i] so when you try to read it, it's nil.
test
Prole
Posts: 28
Joined: Sun Apr 14, 2019 2:36 pm

Re: Help about my code

Post by test »

I solved the errors.

Code: Select all

local w
local h
local p = {}
p[1] = {}
p[2] = {}
p[1].slot = {}
p[1].slot[1] = {}
p[1].slot[2] = {}
p[1].slot[3] = {}
p[1].slot[4] = {}
p[2].slot = {}
p[2].slot[1] = {}
p[2].slot[2] = {}
p[2].slot[3] = {}
p[2].slot[4] = {}
local spell = {}
spell[1] = {}
spell[2] = {}
spell[3] = {}
spell[4] = {}
spell[5] = {}
spell[6] = {}
function love.load()
	w, h = love.graphics.getDimensions()
	p[1].x = w / 4
	p[2].x = 3 * w / 4
	spell[1].img = love.graphics.newImage('watersplash.png')
	spell[2].img = love.graphics.newImage('darkness.png')
	spell[3].img = love.graphics.newImage('fireball.png')
	spell[4].img = love.graphics.newImage('poisondrop.png')
	spell[5].img = love.graphics.newImage('lightning.png')
	spell[6].img = love.graphics.newImage('speed.png')
	for i = 1, 2 do
		p[i].img = love.graphics.newImage('p.png')
		p[i].y = h / 2
		p[i].w = p[i].img:getWidth()
		p[i].h = p[i].img:getHeight()
		p[i].xx = p[i].x
		p[i].yy = p[i].y
		p[i].v = 750
		for ii = 1, 4 do
			p[i].slot[ii] = spell[love.math.random(1, #spell)]
		end
		for ii = 1, 4 do
			if p[i].slot[ii] == spell[6] then
				p[i].v = p[i].v * 2
				break
			end
		end
	end
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 < w / 2 then
			if x > 150 then
				p[1].xx = x
				p[1].yy = y
			end
		else
			if x < w - 150 then
				p[2].xx = x
				p[2].yy = y
			end
		end
	end
	for i = 1, 2 do
		local distance = math.dist(p[i].x, p[i].y, p[i].xx, p[i].yy)
		local stepSize = p[i].v * dt

		if distance > stepSize then
			p[i].x = p[i].x + (p[i].xx - p[i].x) / distance * stepSize
			p[i].y = p[i].y + (p[i].yy - p[i].y) / distance * stepSize
		end
	end
end
function love.draw()
	for i = 1, 2 do
		love.graphics.draw(p[i].img, p[i].x, p[i].y)
	end
	love.graphics.draw(p[1].slot[1].img, 0, 0)
	love.graphics.draw(p[1].slot[2].img, 0, 200)
	love.graphics.draw(p[1].slot[3].img, 0, 400)
	love.graphics.draw(p[1].slot[4].img, 0, 600)
	love.graphics.draw(p[2].slot[1].img, w - 200, h - 200)
	love.graphics.draw(p[2].slot[2].img, w -200, h - 400)
	love.graphics.draw(p[2].slot[3].img, w - 200, h - 600)
	love.graphics.draw(p[2].slot[4].img, w - 200, h - 800)
end

function math.dist(x1, y1, x2, y2) return ((x2 - x1) ^ 2 + (y2 - y1) ^ 2) ^ 0.5 end
Post Reply

Who is online

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