My Function isn't activating, please help

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
BigNoob
Prole
Posts: 1
Joined: Sun Apr 18, 2021 4:03 am

My Function isn't activating, please help

Post 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:
User avatar
togFox
Party member
Posts: 828
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

Re: My Function isn't activating, please help

Post by togFox »

What is the error and which line is it on?
Last project:
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Turn-based PBEM horse stable (racing) management sim: https://togfox.itch.io/horse-stable-manager
https://discord.gg/HeHgwE5nsZ
User avatar
pgimeno
Party member
Posts: 3656
Joined: Sun Oct 18, 2015 2:58 pm

Re: My Function isn't activating, please help

Post 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.
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests