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

displaying a piano, how would i calculate where to place the black keys?

Asked by 5 years ago

i made a piano that expands across the screen, the white keys and sizing of the black keys is working as expected, there is a folder in the same parent of the script called Piano, this is where the frames of the keys are stored, but as any piano looks, black keys are placed weirdly, how would i calculate the position of the black keys on the screen (Scale or Offset or both propertys of a udim2 can be used):

script.Parent.DrawPiano.Event:connect(function(octaves)
    local whitekeys = 7*octaves --calculate white keys amount
    local blackkeys = 5*octaves --calculate black keys amount
    for i=1,whitekeys,1 do
        local key = Instance.new("Frame", script.Parent.Piano)
        key.Size = UDim2.new(
            0,
            script.Parent.AbsoluteSize.X/whitekeys,
            0,
            150
        )
        key.Position = UDim2.new(
            (i-1)/whitekeys,
            0,
            1,
            -150
        )
        key.BackgroundColor3 = Color3.new(1,1,1)
        key.BorderColor3 = Color3.new(0.67, 0.67, 0.67)
        key.BorderSizePixel = 2
        key.ZIndex = 2
        key.Name = "WhiteKey"
    end
    for i=1,blackkeys,1 do
        local key = Instance.new("Frame", script.Parent.Piano)
        key.Size = UDim2.new(
            1/whitekeys,
            -15,
            0,
            75
        )
        key.Position = UDim2.new(
            (i-1)/blackkeys,
            0,
            1,
            -150
        )
        key.BackgroundColor3 = Color3.new(0.2,0.2,0.2)
        key.BorderColor3 = Color3.new(0,0,0)
        key.BorderSizePixel = 2
        key.ZIndex = 3
        key.Name = "BlackKey"
    end
end)

Answer this question