Roblox Studio如何制作一个画框向上下弹跳后定位于某一位置

我试图制作一个画框,当事件被触发时可以向上下弹跳(已经解决了该事件,并且它运行得非常完美)。然而,我不知道如何处理画框,我真的希望能够让画框产生上述效果。另外一件事是,当另一个事件被触发时,它能否滑下并离开屏幕?

点赞
用户2860267
用户2860267

嘿,开发人员中心有关于使用Frames和Guis的教程

听起来TweenService是可以解决您的问题的东西!

我不知道您正在使用的信号,但是下面是一个简单的例子:

1)在StarterGui中创建一个ScreenGui。

2)在ScreenGui中添加一个TextButton,我们将在其上监听单击以打开和关闭Frame。

3)在ScreenGui中添加一个Frame,添加一些内容。自定义它,移动它。

4)在ScreenGui中添加一个LocalScript,并将以下代码添加到脚本中:

-- 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 ,这应该可以正常工作。

希望能对您有所帮助!

2019-03-03 01:39:12
用户2860267
用户2860267

嘿,既然你想在用户悬停在帧上时动画化,那么请按照以下步骤执行...

1)在StarterGui中创建一个ScreenGui。

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

3)在ScreenGui中添加另一个Frame,将其名称更改为TweenFrame,向其添加一些内容。自定义它,将它移动到屏幕上,这是我们将在屏幕上动画化的内容。

4)在ScreenGui中添加一个LocalScript。双击打开它,并添加以下内容到脚本中...

-- 获取一些UI元素
local hoverFrame = script.Parent.HoverFrame
local testFrame = script.Parent.TweenFrame

-- 创建一些变量
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)

-- 创建一个辅助函数来动画化帧
local function tweenToPos(thing, target)
    local tweenInfo = TweenInfo.new(0.5,  -- 播放时间(秒)
        Enum.EasingStyle.Bounce, -- << 这将为您提供反弹的外观
        Enum.EasingDirection.Out,
        0, -- 重复的次数
        false, -- 反方向
        0) -- 动画延迟的时间

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

-- 创建另一个辅助函数来处理动画化帧的过渡
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

-- 监听鼠标悬停在按钮上的事件,并动画化帧
hoverFrame.MouseEnter:Connect(function(x, y)
    -- 如果有正在播放的动画,则取消它。
    cancelTweenIfPlaying()

    -- 将帧动画化为中心位置
    currentTween = tweenToPos(testFrame, onscreenPos)
    currentTween:Play()
end)

-- 监听鼠标停止悬停在按钮上的事件
hoverFrame.MouseLeave:Connect(function(x, y)
    -- 如果动画正在运行,则取消它
    cancelTweenIfPlaying()

    -- 动画化为屏幕外位置
    currentTween = tweenToPos(testFrame, offscreenPos)
    currentTween:Play()
end)

希望能对你有所帮助!

2019-03-05 17:22:58