For every item in a player's backpack, a frame is created. After that, the frame is positioned horizontally. What I need help on is the positioning. If the number of frames is odd, like 1 or 3, to place the middle frame in the middle. But if the number of frames/items is even, like 2 or 4, then make the frames equidistance to the center.
Here is a visual concept, + = center.
+ Odd(3Frames): F F F Even(2Frames): F F
Hi,
One way to do this is using the modulo operation. The modulo is the remainder of two numbers, so if you take n%2 where n is your number, you will see if it is even (if the remainder is 0) or odd (if it is not 1). Then you just need an if statement.
if (number%2 == 0) then --even number frame.Position = frameposition1 else --odd number frame.Position = frameposition2 end
In pseudocode:
-- Store n elements in an array elements = {e_1, e_2, ..., e_n} -- Division is the gap between each element division <- total screen width / number of elements -- If odd, start at position 0, otherwise at offset if number is odd -- Start at the edge of the screen element_position <- 0 else -- Start half way through a division, giving a buffer of half a division either side element_position <- division / 2 -- Place elements on screen for i = 1 to n e_1.placeAt(element_position) element_position = element_position + division
I imagine something along those lines would work.