Make this not dependent on the variable

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
20p_
Prole
Posts: 1
Joined: Wed Feb 15, 2023 7:02 pm

Make this not dependent on the variable

Post 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!
User avatar
darkfrei
Party member
Posts: 1209
Joined: Sat Feb 08, 2020 11:09 pm

Re: Make this not dependent on the variable

Post 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)
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
dusoft
Party member
Posts: 676
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: Make this not dependent on the variable

Post 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.
User avatar
Bigfoot71
Party member
Posts: 287
Joined: Fri Mar 11, 2022 11:07 am

Re: Make this not dependent on the variable

Post 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 ^^
My avatar code for the curious :D V1, V2, V3.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 9 guests