Page 1 of 1

My Function isn't activating, please help

Posted: Sun Apr 18, 2021 4:32 am
by BigNoob
I am trying to make a flappy bird replica but my "createrec" function does not seem to work... It is very possible that it is a different problem because as my name suggests, I am very bad.

This is the code:

function love.load()
love.physics.setMeter(64)

world = love.physics.newWorld(0, 9*64, true)

recs = {}
objects = {}

objects.ball = {}
objects.ball.body = love.physics.newBody(world, 650/2, 650/2, "dynamic")
objects.ball.shape = love.physics.newCircleShape(20)
objects.ball.fixture = love.physics.newFixture(objects.ball.body,
objects.ball.shape, 1)
objects.ball.fixture:setRestitution(0.9)


love.graphics.setBackgroundColor(0.41, 0.53, 0.97)
love.window.setMode(650, 650)
end


function love.update(dt)
world:update(dt)
end

function love.draw()

love.graphics.setColor(0.76, 0.18, 0.05)
love.graphics.circle("fill", objects.ball.body:getX(),
objects.ball.body:getY(), objects.ball.shape:getRadius())

for i,r in ipairs(recs) do
love.graphics.rectangle("line", r.x, r.Y, r.width, r.height)
end
end

function love.keypressed(key)
if key == "up" then
objects.ball.body:applyForce(0, -4000)
end

if key == "down" then
function createRecs()
end
end

function createRecs()
local rec = {}
rec.X = objects.ball.body:getX() + 300
rec.Y = love.math.random(200, 700)
rec.width = 50
rec.height = rec.Y
table.insert(recs, rec)
end
end

Thank you :awesome:

Re: My Function isn't activating, please help

Posted: Sun Apr 18, 2021 12:03 pm
by togFox
What is the error and which line is it on?

Re: My Function isn't activating, please help

Posted: Sun Apr 18, 2021 12:07 pm
by pgimeno
BigNoob wrote: Sun Apr 18, 2021 4:32 am I am trying to make a flappy bird replica but my "createrec" function does not seem to work... It is very possible that it is a different problem because as my name suggests, I am very bad.

This is the code:

Code: Select all

function love.load()
  love.physics.setMeter(64)
 
  world = love.physics.newWorld(0, 9*64, true)

  recs = {}
  objects = {} 

  objects.ball = {}
  objects.ball.body = love.physics.newBody(world, 650/2, 650/2, "dynamic")
  objects.ball.shape = love.physics.newCircleShape(20)
  objects.ball.fixture = love.physics.newFixture(objects.ball.body,
                                                 objects.ball.shape, 1)
  objects.ball.fixture:setRestitution(0.9) 


  love.graphics.setBackgroundColor(0.41, 0.53, 0.97)
  love.window.setMode(650, 650) 
end


function love.update(dt)
  world:update(dt) 
end

function love.draw()

  love.graphics.setColor(0.76, 0.18, 0.05)
  love.graphics.circle("fill", objects.ball.body:getX(),
                       objects.ball.body:getY(), objects.ball.shape:getRadius())
                       
  for i,r in ipairs(recs) do 
love.graphics.rectangle("line", r.x, r.Y, r.width, r.height)
  end 
end

function love.keypressed(key)
  if key == "up" then 
    objects.ball.body:applyForce(0, -4000)
  end

  if key == "down" then 
   function createRecs()
  end
end

function createRecs()
 local rec = {}
  rec.X = objects.ball.body:getX() + 300
  rec.Y = love.math.random(200, 700)
  rec.width = 50
  rec.height = rec.Y
  table.insert(recs, rec)
   end
end
Thank you :awesome:
Please use [code]...[/code] tags to format code, so that it's correctly formatted. I've added them for you in the quote above.

You have an unbalanced 'end', because you're defining createRecs() when you actually want to call it.

This part:

Code: Select all

  if key == "down" then 
   function createRecs()
  end
should actually be:

Code: Select all

  if key == "down" then 
   createRecs()
  end
Then everything is balanced like the indentation suggests, and one of the 'end's at the end should be removed.