Page 1 of 2
Music not working & cannot place squares
Posted: Thu Apr 05, 2012 6:10 am
by Yousar
The problem is my music isn't working and I have made a script to place squares.
But the script isn't placing the squares and since I am very (very) new to LUA, I have no idea what I'm doing wrong.
Please could you point out what's wrong with the script and that'll make my day.
Re: Music not working & cannot place squares
Posted: Thu Apr 05, 2012 6:11 am
by Davidobot
Yousar wrote:The problem is my music isn't working and I have made a script to place squares.
But the script isn't placing the squares and since I am very (very) new to LUA, I have no idea what I'm doing wrong.
Please could you point out what's wrong with the script and that'll make my day.
First of don't upload a .zip upload a .love
viewtopic.php?f=4&t=451
Re: Music not working & cannot place squares
Posted: Thu Apr 05, 2012 6:18 am
by Davidobot
Well first here is the music.lua: (Both edited)
Code: Select all
function love.music()
music = love.audio.newSource("hurricane.mp3") -- if "static" is omitted, LЦVE will stream the file from disk, good for longer music tracks
love.audio.setVolume(1.0)
love.audio.play(music)
end
And here is the main.lua:
Code: Select all
-- Tutorial 1: Hamster Ball
-- Add an image to the game and move it around using
-- the arrow keys.
-- compatible with lцve 0.6.0 and up
require "music.lua"
function love.load()
hamster = love.graphics.newImage("guy.png")
x = 50
y = 50
speed = 100
love.music()
end
function love.update(dt)
if love.keyboard.isDown("right") then
x = x + (speed * dt)
elseif love.keyboard.isDown("left") then
x = x - (speed * dt)
end
if love.keyboard.isDown("down") then
y = y + (speed * dt)
elseif love.keyboard.isDown("up") then
y = y - (speed * dt)
end
end
function love.draw()
love.graphics.draw(hamster, x, y)
end
function love.mousepressed(x, y, button)
if button == "1" then
love.graphics.rectangle("line", 10, 10, x, y)
end
end
Re: Music not working & cannot place squares
Posted: Thu Apr 05, 2012 6:23 am
by Yousar
Thanks Davidobot, I have noticed the mistakes you fixed and I hope I don't make that error again.
However, I still cannot place blocks, which slightly confuses me.
Do you know why?
Re: Music not working & cannot place squares
Posted: Thu Apr 05, 2012 7:01 am
by Davidobot
Yousar wrote:Thanks Davidobot, I have noticed the mistakes you fixed and I hope I don't make that error again.
However, I still cannot place blocks, which slightly confuses me.
Do you know why?
I have tried altering the code but I also couldn't place blocks. It maybe you should talk to a more expirienced dev.
Re: Music not working & cannot place squares
Posted: Thu Apr 05, 2012 7:29 am
by Yousar
Thanks for trying, +1 karma.
Re: Music not working & cannot place squares
Posted: Thu Apr 05, 2012 9:34 am
by iemfi
It doesn't work because you are calling love.graphics.draw from mousepressed.
Code: Select all
objects={}
objects.rectangles={}
function love.draw()
for i,v in ipairs(objects.rectangles) do --we loop through every entry in the objects.rectangle table
love.graphics.rectangle("line", 10, 10, v[1], v[2]) --draw the rectangle (I don't know why want them starting at 10,10).
end
love.graphics.draw(hamster, x, y)
end
function love.mousepressed(x, y, button)
if button == "1" then
table.insert(objects.rectangles,{x,y}) -- we add the mouse coordinates to the objects.rectangle table
end
end
This would work but I would suggest learning and using an Object Oriented method instead if you're going to make a more complex game.
Re: Music not working & cannot place squares
Posted: Thu Apr 05, 2012 10:23 am
by Yousar
I have no idea what you just wrote...
But thanks anyway.
Re: Music not working & cannot place squares
Posted: Thu Apr 05, 2012 10:55 am
by Davidobot
Yousar wrote:I have no idea what you just wrote...
But thanks anyway.
He wrote that you need to put the draw block thing into the draw function. But I tried it and it still didn't work.
Re: Music not working & cannot place squares
Posted: Thu Apr 05, 2012 11:10 am
by Petunien
iemfi wrote:
This would work but I would suggest learning and using an Object Oriented method instead if you're going to make a more complex game.
What method are you thinking of?
I also have some troubles getting started.
Edit:
iemfi wrote:Code: Select all
...
function love.mousepressed(x, y, button)
if button == "1" then
table.insert(objects.rectangles,{x,y}) -- we add the mouse coordinates to the objects.rectangle table
end
end
It should be:
Code: Select all
function love.mousepressed(x, y, button)
if button == "l" then
table.insert(objects.rectangles,{x,y}) -- we add the mouse coordinates to the objects.rectangle table
end
end
A "l" instead of a "1" in the if-statement.