Roblox studio如何使框架上下摆动然后落在特定位置

时间:2019-03-01 15:29:27

标签: lua roblox

我正在尝试制作一个框架,该框架在触发事件时会上下波动(已经解决了该事件,并且效果很好),但是我不知道如何使用框架,我真的很想制作框架是否具有上述效果,还可以在触发另一个事件后向下滑动并滑出屏幕吗?

2 个答案:

答案 0 :(得分:0)

Heyo,开发人员中心tutorials for working with with Frames and Guis很棒! 听起来TweenService可以解决您的问题!

我不知道您正在使用什么信号,但这是您要执行的操作的一个简单示例:

1)在StarterGui中创建一个ScreenGui。

2)在ScreenGui中添加一个TextButton,我们将听其点击以切换框架的打开和关闭状态。

3)在ScreenGui中添加一个框架,并向其中添加一些内容。自定义它,移动它。

4)将LocalScript添加到ScreenGui。将此添加到脚本...

-- grab some UI Elements
local TweenService = game:GetService("TweenService")
local btn = script.Parent.TextButton
local testFrame = script.Parent.Frame

-- make some variables
local isVisible = false
local currentTween
local onscreenPos = testFrame.Position
local offscreenPos = UDim2.new(onscreenPos.X.Scale - 1,
    onscreenPos.X.Offset,
    onscreenPos.Y.Scale,
    onscreenPos.Y.Offset)

-- make a helper function for animating the frame
local function tweenToPos(thing, target)
    local tweenInfo = TweenInfo.new(0.5,  -- how long should this play (seconds)
        Enum.EasingStyle.Bounce, -- << This will give you the bounce in look
        Enum.EasingDirection.Out,
        0, -- number of times to repeat
        false, -- reverses
        0) -- how many seconds to delay the animation

    local propertyTable = {
        Position = target,
    }
    local tween = TweenService:Create(thing, tweenInfo, propertyTable)
    return tween
end

-- move the frame off-screen to begin with
testFrame.Position = offscreenPos

-- connect to the button and toggle between on/offscreen
btn.Activated:Connect(function(inputObj)
    -- if the tween is already running, cancel it
    if currentTween then
        if currentTween.PlaybackState == Enum.PlaybackState.Playing
            or currentTween.PlaybackState == Enum.PlaybackState.Delayed
            or currentTween.PlaybackState == Enum.PlaybackState.Paused then
            currentTween:Cancel()
        end
    end

    -- create a new tween to animate the frame
    if isVisible then
         currentTween = tweenToPos(testFrame, offscreenPos)
    else
         currentTween = tweenToPos(testFrame, onscreenPos)
    end

    -- play the animation
    currentTween:Play()

    -- toggle which tween to use next
    isVisible = not isVisible
end)

这对于进出应该有很好的弹跳效果。您可以将btn.Activated:Connect换成您正在收听的任何信号,这应该可以正常工作。

希望这对您有帮助!

答案 1 :(得分:0)

Heyo,由于您要在用户将鼠标悬停在框架上时进行动画处理,因此请执行以下步骤...

1)在StarterGui中创建一个ScreenGui。

2)在ScreenGui中添加一个Frame,将其名称更改为HoverFrame,我们将在此监听鼠标事件以切换动画状态。

3)将另一个Frame添加到ScreenGui,将其名称更改为TweenFrame,并向其中添加一些内容。对其进行自定义,四处移动,这就是我们在屏幕上进行动画处理的过程。

4)将LocalScript添加到ScreenGui。双击将其打开,然后将其添加到脚本中...

-- grab some UI Elements
local hoverFrame = script.Parent.HoverFrame
local testFrame = script.Parent.TweenFrame

-- make some variables
local TweenService = game:GetService("TweenService")
local currentTween
local onscreenPos = UDim2.new(0,0,0.443,0)
local offscreenPos = UDim2.new(0,0,0.914,0)

-- make a helper function for animating the frame
local function tweenToPos(thing, target)
    local tweenInfo = TweenInfo.new(0.5,  -- how long should this play (seconds)
        Enum.EasingStyle.Bounce, -- << This will give you the bounce in look
        Enum.EasingDirection.Out,
        0, -- number of times to repeat
        false, -- reverses
        0) -- how many seconds to delay the animation

    local propertyTable = {
        Position = target,
    }
    local tween = TweenService:Create(thing, tweenInfo, propertyTable)
    return tween
end

-- make another helper function for handling the animation tween
local function cancelTweenIfPlaying()
    if currentTween then
        if currentTween.PlaybackState == Enum.PlaybackState.Playing
            or currentTween.PlaybackState == Enum.PlaybackState.Delayed
            or currentTween.PlaybackState == Enum.PlaybackState.Paused then
            currentTween:Cancel()
        end
    end
end


-- listen for when the mouse hovers over the button, and animate the frame
hoverFrame.MouseEnter:Connect(function(x, y)
    -- if there is an animation playing, cancel it.
    cancelTweenIfPlaying()

    -- animate the frame to center stage
    currentTween = tweenToPos(testFrame, onscreenPos)
    currentTween:Play()
end)

-- listen for when the mouse stops hovering over the button
hoverFrame.MouseLeave:Connect(function(x, y)
    -- if the tween is already running, cancel it
    cancelTweenIfPlaying()

    -- animate to offscreen
    currentTween = tweenToPos(testFrame, offscreenPos)
    currentTween:Play()
end)

希望这对您有帮助!