What is going wrong here.


Not an advanced coder at all here, but have always liked programming… Anyway, i am trying to make a really simple program that drops coins on the screen. I have some simple code here that should be creating and dropping one ‘penny’ every 2 seconds. But it is not working. The weird thing is, if I do some print() function of one of the variables in question every frame, the code appears to work! Thanks for any pointers

Main class:

-- Use this function to perform your initial setup
function setup()
    coins = {}
    table.insert(coins, Coin(35, math.random(122,725), 800))

-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    background(40, 40, 50)
    for i,v in pairs(coins) do

    difftime = os.clock() - starttime
    if (difftime >= 2) then
        starttime = os.clock()
        table.insert(coins, Coin(35, math.random(122,725), 800))
    -- if i uncomment the below line, the above if works...  
 --  print (difftime)

Coin class:

-- Coin Class --

Coin = class()

function Coin:init(radius, x, y)
    self.pos = vec2(x, y)
    self.radius = radius
    self.vel = vec2(0, 0)
    currentangle = 0

function Coin:draw()
    fill(113, 37, 31, 255)
    ellipse(self.pos.x, self.pos.y, 2 * self.radius)

function Coin:update()

self.vel.y = self.vel.y - .04
        self.pos = self.pos + self.vel

function Coin:drawmini()
    -- *ignore this. i may use it later
--    currentangle = currentangle + self.vel.x
--    if currentangle > math.abs(self.radius * 6.28319) then
--    currentangle = 0
--  end

stroke(203, 187, 187, 255)

-- draw the number
    line(self.pos.x, self.pos.y-(self.radius*.55),self.pos.x, self.pos.y+(self.radius*.55))


function Coin:left()
    return self.pos.x - self.radius

function Coin:right()
    return self.pos.x + self.radius

function Coin:top()
    return self.pos.y + self.radius

function Coin:bottom()
    return self.pos.y - self.radius

Omg formatting… I’m on my ipad - guess i dont know the code tags so that didn’t screw up. I’ll look around for that while you all point and laugh.

The code tags are “~~~” on their own line at the beginning and end of the code. I’ve fixed it for you.

I have a hunch - try using “ElapsedTime” rather than os.clock(), see if it works better.

Thank you

Any input on this? I converted the code to just using a generic increment counter to continue the project, but that of course isnt as time-accurate if the code starts lagging behind 60/second

Also, if i spam the program with “coins”, it does start lagging pretty quickly. Im confused why it does that, as i thought we had the ability to handle thousands of objects at once. Am I processing the update and draw functions inefficiently?

If you use ElapsedTime instead of os.clock() it should work

os.clock returns the CPU time of the program, so it’s not what you need here

@Coconut - I’m guessing your coins are elipses() - which are known performance sinks. Try “nosmooth” and see if it helps; if that isn’t enough, you might want to use setcontext() and create an image, and draw the elipse to that image - then sprite() the image, rather than re-drawing the ellipse. Faster still would be a mesh().

A much-expanded explanation, with code samples, should be put into the FAQ. This is a commonly-run-into issue.

Cool bortels. Thanks. I guess i need a lot more practice or understanding in creating sprites or meshes. I’m really much more of a logical coder than an artist!

Sprites (and images) are pretty easy - they’re just a blob of graphics you can stamp onto the screen. So - concrete example:

function setup()
   coin = Image(50, 50)
   setContext(coin)   -- draw into the coin image rather than the screen
   ellipse(blah I don't remember the arguments for ellipse)

function draw()
   -- all sorts of other stuff
   -- time to draw my coin image!
   sprite(coin, WIDTH/2, HEIGHT/2)

Clearly, not real code, but it has the parts - set the context to an image, draw your graphics - and now you have something you can stamp on the screen. The expensive part of drawing the ellipse, the smoothing and so on, all happens once, in setup().

Then, during your draw loop, when you have to draw the coin, BAM - it’s an image copy, which is crazy fast. The WIDTH/2, HEIGHT/2 are just where on the screen to draw - in this case, in the center.

A mesh() is, very much, just a collection of a sprites, with a ton of special rules having to do with the underlying graphics co-processor. What they let you do is draw a ton of sprites in a single operation, so it’s even faster.

Nice. I recoded my work, creating the images I need in the init first. Runs perfectly.

Now another dumb question… The description for spritely says that I can imports sprites i create there to other projects. How is that done? And what are other options for more complex graphics, such as street buildings, vehicles, etc.

Just trying to challenge myself as much as possible and learn in the process.

Spritely saves sprite images in global persistent memory; so you could grab them in another project to use. It’s a bit ugly, but prior to 1.4, it’s what you can do.

In 1.4, you’ll be able to modify spritepacks directly - cutting and pasting graphics, and saving and loading sprites programatically. if you have something complex in mind - I’d use the local sprites as placeholders until you can easily import your own graphics.