Discord
Login
Community
DARK THEME

Create objects

I want to draw 10 sprite in the screen with random position so i create this:

createObject = object
  
  Update = function()
    local objects = []
    
    for i=1 to 10
      local myObject = object
        sprite = "myObject"
        x = random.nextInt(400) - 200
        y = random.nextInt(200) - 100
      end
      objects.push(myObject)
    end
    return objects
  end
  
  Draw = function()
    local objects = createObject.Update()
    for o in objects
      screen.drawSprite(o.sprite, o.x, o.y, 20)
    end
  end
  
end

And in main i write:

init = function()
  createObject = 0
end


update = function()
  if createObject == 0 then
    createObject.Update()
    createObject = 1
  end
end


draw = function()  
  createObject.Draw()
end

But the sprite still changing their position... I am doing something wrong or there is another way to do this?

Your sprites keep shifting because you're calling your Update() method in every call of draw(), about 60 times per second. You should generally only update the state of your game within update(). Draw() should only contain stuff directly connected with drawing.

Thanks it work now:)

Post a reply

Progress

Status

Preview
Cancel
Post
Validate your e-mail address to participate in the community