Whats wrong with my code?

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
User avatar
BEANSFTW
Prole
Posts: 28
Joined: Sun Apr 15, 2012 2:58 pm

Whats wrong with my code?

Post by BEANSFTW »

This is my attempt to make a Boulder drop. I have no idea what is wrong with it.

main.lua

Code: Select all



function love.load()

	require ("Rock")
	require ("anal")
	
	RockMoveS = 180
	RockMoveX = 355
	RockMoveY = 255
	
	rockbarf = love.graphics.newImage("Rock.png")
	Rockmonster = love.graphics.newImage("Rockmonster.png")
	backround = love.graphics.newImage("backround.png")

	RockWalkR = love.graphics.newImage("RockMonsterR.png") 
	RockWalkR:setFilter("nearest", "nearest")             
	MonsterR = newAnimation(RockWalkR, 45, 45, 0.1, 2)

	BoulderSpawn = true
	
	music = love.audio.newSource("doop.ogg", "steam")
	music:setLooping(true)
	love.audio.play(music)
	
end

function love.update(dt)

	for i = #all_rocks,1,-1 do
		local rock = all_rocks[i]
		if rock.y > love.graphics.getHeight() then
			table.remove(all_rocks,i)
		end
	end
	
	rock.y = rock.y + dt*rock.fallSpeed
	
	if love.keyboard.isDown("w") then
		RockMoveY = RockMoveY - RockMoveS*dt
		MonsterR:update(dt)
	end	
	
	if love.keyboard.isDown("s") then
		RockMoveY = RockMoveY + RockMoveS*dt
		MonsterR:update(dt)
	end

	if love.keyboard.isDown("d") then
		RockMoveX = RockMoveX + RockMoveS*dt
		MonsterR:update(dt)
	end

	if love.keyboard.isDown("a") then
		RockMoveX = RockMoveX - RockMoveS*dt
		MonsterR:update(dt)
	end	

	if love.keyboard.isDown(" ") and BoulderSpawn == true then
		Rocks_spawn(RockMoveX, rock.y, rockbarf)
		BoulderSpawn = false
	end	
	
	if love.keyboard.isDown("d") and love.keyboard.isDown("s") then
		RockMoveS = 210
	end	

	if love.keyboard.isDown("a") and love.keyboard.isDown("s") then
		RockMoveS = 210
	end
	
	if love.keyboard.isDown("a") and love.keyboard.isDown("w") then
		RockMoveS = 205
	end	
	
	if love.keyboard.isDown("d") and love.keyboard.isDown("w") then
		RockMoveS = 205
	end
	
	

	
	if RockMoveY > 555 then
		RockMoveY = 555
	end
	
	if RockMoveX > 755 then
		RockMoveX = 755
	end	
	
	if RockMoveY < 0 then
		RockMoveY = 0
	end
	
	if RockMoveX < 0 then
		RockMoveX = 0
	end
	
end
and

rock.lua

Code: Select all

	local all_rocks= {}

	function rock_spawn(x, y, rock)
		local rock = {x= x, y= y, rock = rock, fallspeed = 50}
		all_rocks[#all_rocks+1] = rock
		
	end
	

	
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Whats wrong with my code?

Post by Robin »

Could you please make a .love of your game? That would make it a lot easier for us to help you.

One thing I see straight away:

Code: Select all

require ("Rock")
You say the file is named "rock.lua". If that's true, you should change that line to:

Code: Select all

require ("rock")
And the same thing for the images.

But again, we need a .love.
Help us help you: attach a .love.
User avatar
T-Bone
Inner party member
Posts: 1492
Joined: Thu Jun 09, 2011 9:03 am

Re: Whats wrong with my code?

Post by T-Bone »

Also, you should mention what sort of error you get.
User avatar
BEANSFTW
Prole
Posts: 28
Joined: Sun Apr 15, 2012 2:58 pm

Re: Whats wrong with my code?

Post by BEANSFTW »

My error is:

main.lua:31: attempt to get length of global 'all_rocks' (a nil value)

Traceback

main.lua:31: in function 'update'
[C]: in function 'xpcall'
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Whats wrong with my code?

Post by Nixola »

all_rocks is a local variable inside rock.lua, if you want it to be accessible in main.lua you have to write all_rocks = {} instead of "local all_rocks = {}"
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Whats wrong with my code?

Post by Robin »

BEANSFTW wrote:My error is:
You still haven't made a .love. If Nixola's answer doesn't solve your problem completely, please make a .love and upload it as an attachment before you expect more help.

See the rules for asking for help.
Help us help you: attach a .love.
User avatar
BEANSFTW
Prole
Posts: 28
Joined: Sun Apr 15, 2012 2:58 pm

Re: Whats wrong with my code?

Post by BEANSFTW »

;
Game.love
Heres my code
(449.79 KiB) Downloaded 325 times
;
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Whats wrong with my code?

Post by Robin »

Okay, here's your problem: your game is very, very incomplete. You don't seem to have a Rock_draw(), for example.

But the issue that's directly causing your error is that line 38 is placed wrong. rock.y = rock.y + dt*rock.fallSpeed should be inside the for-loop you close just two lines above that line, because you want to do that for every rock.
Help us help you: attach a .love.
User avatar
BEANSFTW
Prole
Posts: 28
Joined: Sun Apr 15, 2012 2:58 pm

Re: Whats wrong with my code?

Post by BEANSFTW »

I'm not sure what for-loop is. Can you show me the code you made to make my code usable, and if you can, explain what you did?

if what your trying to say is VERY obvious and important, I don't know it because I'm a REALLY big noob in programming.
User avatar
substitute541
Party member
Posts: 484
Joined: Fri Aug 24, 2012 9:04 am
Location: Southern Leyte, Visayas, Philippines
Contact:

Re: Whats wrong with my code?

Post by substitute541 »

BEANSFTW wrote:I'm not sure what for-loop is. Can you show me the code you made to make my code usable, and if you can, explain what you did?

if what your trying to say is VERY obvious and important, I don't know it because I'm a REALLY big noob in programming.
Lua 5.1 Reference Manual.

For loops start from a starting number and, through a specified number of steps, ends at the end number. The structure is this.

Code: Select all

for variable = start number, end number, step do
	--zero or more statements
end
By default, the step is 1, "variable" is usually i, or j (if in a nested loop), but you can name it to any variable. Just note that "variable" is a local variable, that is, it cannot be accessed from outside that statement, only in the inside.
Currently designing themes for WordPress.

Sometimes lurks around the forum.
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests