Randomly generated Circles share the same x and y coordinates

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
KowalewskajA
Prole
Posts: 3
Joined: Mon Oct 15, 2018 11:10 pm
Location: Berlin/Germany
Contact:

Randomly generated Circles share the same x and y coordinates

Post by KowalewskajA »

Hi Folks,

first post here and a bloody beginner - nevertheless I could find a solution for all of my issues on my own - not with that issue.

So the short story is, I followed adnzzzzZ's Blog earlier this year but never made it to really get my hands dirty. So I decided to start it and I am currently at Areas/Rooms but my problem is that all the spawned Circle Instances (exercise 48) do not keep their position. Furthermore they all share the same position as you can see:

Image

I did not append a love file but you can see the "code" on the rep on github.

Was thinking a lot about it and could not get an approach to find a solution.

Any advice would be appreciated,
KowalewskajA
therektafire
Prole
Posts: 12
Joined: Mon May 22, 2017 3:45 am

Re: Randomly generated Circles share the same x and y coordinates

Post by therektafire »

I haven't looked at the code but I have a suspicion you may be overwriting each circle with the next one. If you want to create a certain number of unique circles you need to create a table to contain them and then create the circles in a for loop, adding the new circle to the table in each iteration. Something like this:

Code: Select all

local numcircles = somenumber
local circles = {}

for i = 1, numcircles do
    local newcircle = {}
    newcircle.x = math.random(0, love.graphics.getWidth())
    newcircle.y = math.random(0, love.graphics.getHeight())
    --any other code related to creating your particular brand of circle goes here
    circles[i] = newcircle
end

--now somewhere in love.draw draw the new circles in whatever way you need to
User avatar
KowalewskajA
Prole
Posts: 3
Joined: Mon Oct 15, 2018 11:10 pm
Location: Berlin/Germany
Contact:

Re: Randomly generated Circles share the same x and y coordinates

Post by KowalewskajA »

Aloa therektafire,

first of all thank your very much for your reply.

As there seems to be an issue with the img referencing I just included the URL to an Screenshot that might help understanding it more:
https://love2d.org/imgmirrur/RFeOZ9s.png

The idee behind - separating the more general game logic into specific room objects (like Stage for example) that include an area object that handles all the gameobject releted stuff - seems still benefital.

The Area class has a table that holds all the game_objects and in the corresponding update and draw functions it iterates over all the game_objects.

Code: Select all

Area:update(dt)
Area:draw()
Area:addGameObject(game_object_type, x, y, opts)
The Stage (Room) uses a local (hump.)timer and creates a a circle with the Constructor of that class

Code: Select all

--in Stage.lua
self.timer:every(0.1, function () self:createObject('Circle') end)
The constructor of each circle instance uses its own timer that it inhereted from the gameobject class and destroys itself after 2s

Code: Select all

--in Circle.lua, Circle:new(...)
self.timer:after(2, function() self.dead = true end)
So the setup is like you mentioned just splitted in a more oo-way. As you can see in the screenshot (printed game_object table) there exist around 25 game-objects (20 expected 0.1 fits 20 times in 2 [creation/destruction]) but they all share the same uuid and hereby the same coordinates. So the same Circle is drawn 20 times and not 20 different ones as expected.

My next approach will be to safe the references within the stage as well but that is redudant and shifts the oo approach ad absurdum but anyway I will keep digging ;).

Cheers KowalewskajA
Post Reply

Who is online

Users browsing this forum: No registered users and 53 guests