Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to position something according to if a number is even or odd?

Asked by
Vezious 310 Moderation Voter
6 years ago

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

2 answers

Log in to vote
0
Answered by
mraznboy1 194
6 years ago

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
0
Yeah I know how to determine if it's even or odd. What I'm having trouble on is how to script the position if it's even, and how to position if it's odd. Vezious 310 — 6y
Ad
Log in to vote
0
Answered by
fredfishy 833 Moderation Voter
6 years ago

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.

Answer this question