Hello! I am having an issue when it comes to tweening the color of an image button.
This function makes it so that the button given as the parameter will fade and get slightly bigger when your mouse enters it, and go back to normal when the mouse leaves.
I do this by storing the original color and size of the button in the beginning, then I have some functions to change the color and size of the button accordingly.
Everything works fine for a button that will never change colors, but the issue comes when the color of the button can change when I click on it.
Say for example I have a button that’s red, but turns green when you click on it.
What will happen is:
- My mouse will enter the button, making it turn a darker shade of red
- I click the button, making it turn green as intended
- My mouse leaves the button, and it turns back to red since that was stored as the original color
What can I do about this? Is there way of getting around it while still using most of what I have, or will I have to design the whole system differently?
My code is included below. I have it in a modulescript so I can use this for all the buttons in my game.
Main script:
function UIAnimations.SetUpButton(button : ImageButton)
local originalColor = button.ImageColor3
local originalSize = button.Size
local FADEAMOUNT = 1.5
local SCALE = 1.1
local SPEED = .08
local endSize = UDim2.new(
originalSize.X.Scale*SCALE,
0,
originalSize.Y.Scale*SCALE,
0
)
button.MouseEnter:Connect(function()
TweenImageButtonColor(button, GetFadeColor(originalColor, FADEAMOUNT), SPEED)
TweenButtonSize(button, endSize, Enum.EasingDirection.Out, SPEED)
end)
button.MouseLeave:Connect(function()
TweenImageButtonColor(button, originalColor, SPEED)
TweenButtonSize(button, originalSize, Enum.EasingDirection.In, SPEED)
end)
end
Functions used:
-- Tweens color for image buttons
local function TweenImageButtonColor(button, color, speed)
local goal = {ImageColor3 = color}
local tweenInfo = TweenInfo.new(speed)
local Tween = TweenService:Create(button, tweenInfo, goal)
Tween:Play()
end
-- Tweens size for buttons
local function TweenButtonSize(button, size, direction, speed)
button:TweenSize(
size,
Enum.EasingDirection.Out,
Enum.EasingStyle.Sine,
speed,
true
)
end
-- Returns darker shade of the given color
local function GetFadeColor(color : Color3, fade_amount)
return Color3.new(
color.R/fade_amount,
color.G/fade_amount,
color.B/fade_amount
)
end