Page 1 of 1

Make this not dependent on the variable

Posted: Thu Feb 16, 2023 1:15 pm
by 20p_
Hello!
I'm trying to make a tower defense and I need to make my tower placement not dependent on this variable so that it can be placed infinite times, Here's the code of the tower class
Sling = Class{}

function Sling:init()
self.image = love.graphics.newImage("Sling.png")
self.x = 964
self.y = 64
self.width = self.image:getWidth()
self.height = self.image:getHeight()
MXCONSTANT = 964
MYCONSTANT = 64
end
function Sling:collide()
if ((self.x >= mouseX + 10) or
(self.x + 64 <= mouseX) or
(self.y >= mouseY + 10) or
(self.y + 64 <= mouseY)) then
return false
else return true
end
end
function Sling:update(dt)
SliCol = Sling:collide()
if SliCol and pressed and screws >= 100 then
Tower = true
elseif Tower and pressed then
Tower = false
screws = screws - 100
end
if Tower == true then
MXCONSTANT = mouseX - 25
MYCONSTANT = mouseY - 25
end
end
function Sling:render()
love.graphics.draw(self.image, self.x, self.y)
love.graphics.printf(tostring(screws), Window_W / 2, 500, 100, "center")
if Tower then
love.graphics.draw(self.image, mouseX - 25, mouseY - 25)
elseif not Tower then
love.graphics.draw(self.image, MXCONSTANT, MYCONSTANT)
end
end
Thanks for your help!

Re: Make this not dependent on the variable

Posted: Thu Feb 16, 2023 2:34 pm
by darkfrei
I don't know what is class, but:

Code: Select all

local image = love.graphics.newImage("Sling.png")
local slingPrototype =
{
	image = image,
	w = image:getWidth(),
	h = image:getHeight(),
}

function newSling(x, y)
	local newSling = {
		x = x,
		y = y,
		prototype = slingPrototype,
		tower = false,
	}
	return newSling
end

function collideSling (sling, mouseX, mouseY)
	if (	(sling.x >= mouseX + 10) or
		(sling.x + 64 <= mouseX) or
		(sling.y >= mouseY + 10) or
		(sling.y + 64 <= mouseY)) then
		return false
	else 
		return true
	end
end

function updateSling (sling, mouseX, mouseY, pressed)
	local col = collideSling (sling, mouseX, mouseY)
	if col  then
		sling.tower = true
	elseif sling.tower and pressed then
		sling.tower = false
	end
	if sling.tower then
		sling.x = mouseX - 25
		sling.y = mouseY - 25
	end
end

function drawSling (sling, mouseX, mouseY)
	local image = sling.prototype.image
	if sling.tower then
		love.graphics.draw(image, mouseX - 25, mouseY - 25)
	else
		love.graphics.draw (image, sling.x, sling.y)
	end
end
(not tested)

Re: Make this not dependent on the variable

Posted: Thu Feb 16, 2023 4:50 pm
by dusoft
20p_ wrote: Thu Feb 16, 2023 1:15 pm Hello!
I'm trying to make a tower defense and I need to make my tower placement not dependent on this variable so that it can be placed infinite times, Here's the code of the tower class
Sling = Class{}

function Sling:init()
end
You just need to return such sling and then store it in a table of (infinite) slings and work with them.

Re: Make this not dependent on the variable

Posted: Thu Feb 16, 2023 11:32 pm
by Bigfoot71
Just a little advice that has nothing to do by the way, avoid doing something like this:

Code: Select all

Obj = Class {
    init = function (self)
        self.image = love.graphics.newImage("my-image.png")
    end;
}
But do this instead:

Code: Select all

Obj = Class {

    image = love.graphics.newImage("my-image.png");

    init = function (self)
        -- Init something --
    end;

}
Because in the first example (what you did) the image is loaded with each call to :init() while in the second example the image is only loaded once and you can even create several " instances" of the class and they will all call the same image reference in memory ^^