Creating more than one block through 2 lua files

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.
Evil_Bartek
Prole
Posts: 43
Joined: Sun Dec 25, 2011 5:13 pm

Creating more than one block through 2 lua files

Post by Evil_Bartek »

I have 2 lua files:

main.lua:

Code: Select all

function love.load()
	require "conf.lua"
	require "block_damage.lua"
	rect = {x = 0, y = 0, width = 40, height = 40, health = 100}
	controls = {down = "down", up = "up", left = "left", right = "right"}
	love.graphics.setFont(nokiafc22)
end

function love.draw()
	love.graphics.rectangle("fill",rect.x,rect.y,rect.width,rect.height)
end

function love.update(dt)
	if love.keyboard.isDown(controls.down) then
		rect.y = rect.y + 200*dt
	end
	if love.keyboard.isDown(controls.up) then
		rect.y = rect.y - 200*dt
	end
	if love.keyboard.isDown(controls.left) then
		rect.x = rect.x - 200*dt
	end
	if love.keyboard.isDown(controls.right) then
		rect.x = rect.x + 200*dt
	end
	warpScreen()
end

function warpScreen()
	local width = love.graphics.getWidth()
	local height = love.graphics.getHeight()
	
	if rect.x <= -40 then
		rect.x = (width + 40) - rect.width
	elseif rect.x >= (width) then
		rect.x = (width - 40) - width
	end
	
	if rect.y <= -40 then
		rect.y = (height + 40) - rect.height
	elseif rect.y >= (height) then
		rect.y = (height - 40) - height
	end
end
block_damage:

Code: Select all

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

function drawDamageBlock(x,y)
	size = {x = 40, y = 40}
	love.graphics.rectangle("fill",x,y,size.x,size.y)
end

function love.draw()
	drawDamageBlock(100,100)
end
When i run the game, it draws the damage block but not the player... and also the damage block is white and its men't to be red...
Any help?
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Creating more than one block through 2 lua files

Post by bartbes »

The callbacks don't get called for each file, there's only one of them, the one defined last, and files do mess with each other. So when you load the second file, it overrides the love.draw in the first one. Similarly, love.load in the second file never gets called, since it is only loaded after love.load has been called.
Evil_Bartek
Prole
Posts: 43
Joined: Sun Dec 25, 2011 5:13 pm

Re: Creating more than one block through 2 lua files

Post by Evil_Bartek »

What can i do then? Should i make 2 funcrions? like draw player and draw block damage? i dunno :(
Evil_Bartek
Prole
Posts: 43
Joined: Sun Dec 25, 2011 5:13 pm

Re: Creating more than one block through 2 lua files

Post by Evil_Bartek »

Bump
(i hope somebody doesn't decrease my karma again for saying that...)
User avatar
MarekkPie
Inner party member
Posts: 587
Joined: Wed Dec 28, 2011 4:48 pm
Contact:

Re: Creating more than one block through 2 lua files

Post by MarekkPie »

Well...when you bump something that is still on the main page...only 30 minutes after you first replied to it...yeah, your karma is going down.
Evil_Bartek
Prole
Posts: 43
Joined: Sun Dec 25, 2011 5:13 pm

Re: Creating more than one block through 2 lua files

Post by Evil_Bartek »

MarekkPie wrote:Well...when you bump something that is still on the main page...only 30 minutes after you first replied to it...yeah, your karma is going down.
Thanks, btw. If you are gonna post, help me, if you gonna be trollin' around, then don't post.
If you think that decreasing my karma is gonna affect me in any way, then you're wrong. I signed up to this forum because I wanted other people to help me. I don't give a crap at all.

I dunno what del is so:
test
User avatar
The Burrito
Party member
Posts: 153
Joined: Mon Sep 21, 2009 12:14 am
Contact:

Re: Creating more than one block through 2 lua files

Post by The Burrito »

Basically you don't want to define things like love.draw or love.load outside of main.lua, what you should do is maybe rename them to blockdamageDraw or something and then call that within love.draw in main.lua so you have something like:

Code: Select all

function love.draw()
   love.graphics.rectangle("fill",rect.x,rect.y,rect.width,rect.height)
   blockdamageDraw()
end
Side note: You don't need to require conf.lua, löve does that automatically, and you should probably leave off the .lua in the file name when using require.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Creating more than one block through 2 lua files

Post by bartbes »

Evil_Bartek wrote:I signed up to this forum because I wanted other people to help me. I don't give a crap at all.
Well, that's sad. See, if everyone dislikes you for not obeying to the forum rules, they won't help you either. So please do, for all of us.
Evil_Bartek
Prole
Posts: 43
Joined: Sun Dec 25, 2011 5:13 pm

Re: Creating more than one block through 2 lua files

Post by Evil_Bartek »

bartbes wrote:
Evil_Bartek wrote:I signed up to this forum because I wanted other people to help me. I don't give a crap at all.
Well, that's sad. See, if everyone dislikes you for not obeying to the forum rules, they won't help you either. So please do, for all of us.
Yeah I actually don't care in that moment or in any other moment. I found a way better engine which is called flixel and use AS3 so in any second now this account been deleted. O btw. if you want to see which is better (flixel or love2d) then check some pages like:
http://spottedsun.com/flixel-2-0-vs-love2d/
http://forums.tigsource.com/index.php?P ... 793.0;wap2
Well there you have it.
(I predict that I will be banned)
So according to me:
If you want to be a real game programmer, then use the java based programming languages, I can name you way more people that were famous for making AS3 games than Lua games...
Bye!
PS.Trolololololo lololo lololo oy oy oyo yoyoyo

PSS.OBEY RULE SUCKS(That rule always sucked)
User avatar
MarekkPie
Inner party member
Posts: 587
Joined: Wed Dec 28, 2011 4:48 pm
Contact:

Re: Creating more than one block through 2 lua files

Post by MarekkPie »

What a dork.
Locked

Who is online

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