Keyframe
*Nội dung này được dịch bằng AI (Beta) và có thể có lỗi. Để xem trang này bằng tiếng Anh, hãy nhấp vào đây.
Một Khung chìa khóa giữ được áp dụng cho các khớp tại một điểm thời gian cụ thể trong một hoạt hiệu ứng động.Keyframes được lồng vào giữa trong lúc phát lại hoạt hình.
Lưu ý, trong hầu hết các trường hợp, nhà phát triển không cần phải thao tác KeyframeSequences vì trình chỉnh sửa hoạt họa bao gồm hầu hết chức năng hoạt họa.Tuy nhiên, trong một số trường hợp, một nhà phát triển có thể muốn tạo một hoạt hình từ một Script hoặc xây dựng plugin riêng của họ.
Cấu trúc
Khung chìa khóa được giữ trong một KeyframeSequence và chứa đối tượng Pose .Các tư thế được đặt tên theo cách phù hợp với chúng và được cấu trúc theo khía cạnh về cấu trúc chung.Điều này có nghĩa là mỗi Pose là cha của mỗi Pose tương ứng với phần nó đính kèm.
Lưu ý, như Poses được đặt tên theo cách tương ứng với BaseParts chúng phù hợp, các hoạt hình yêu cầu các tên phần riêng biệt để chơi đúng cách.
Nối kết
Trong lúc phát lại hoạt hình, các tư thế trong các khung chìa khóa khác nhau được giải quyết giữa.Điều này cho phép tạo một hoạt hình mượt mà mà không cần phải định nghĩa mỗi khung.Lưu ý, phong cách interpolation được xác định trong đối tượng Pose.Vật phẩm Keyframe chỉ giữ Poses tại một điểm thời gian xác định trong hoạt hình ( Keyframe.Time ).
Mẫu mã
This sample includes a function that will generate a 'blank' keyframe containing blank poses for all of the model's connected parts in the correct hierarchical order.
local function generateKeyframe(model)
if not model.PrimaryPart then
warn("No primary part set")
return
end
local rootPart = model.PrimaryPart:GetRootPart()
if not rootPart then
warn("Root part not found")
return
end
local partsAdded = {}
partsAdded[rootPart] = true
local function addPoses(part, parentPose)
-- get all of the joints attached to the part
for _, joint in pairs(part:GetJoints()) do
-- we're only interested in Motor6Ds
if joint:IsA("Motor6D") then
-- find the connected part
local connectedPart = nil
if joint.Part0 == part then
connectedPart = joint.Part1
elseif joint.Part1 == part then
connectedPart = joint.Part0
end
if connectedPart then
-- make sure we haven't already added this part
if not partsAdded[connectedPart] then
partsAdded[connectedPart] = true
-- create a pose
local pose = Instance.new("Pose")
pose.Name = connectedPart.Name
parentPose:AddSubPose(pose)
-- recurse
addPoses(connectedPart, pose)
end
end
end
end
end
local keyframe = Instance.new("Keyframe")
-- populate the keyframe
local rootPose = Instance.new("Pose")
rootPose.Name = rootPart.Name
addPoses(rootPart, rootPose)
keyframe:AddPose(rootPose)
return keyframe
end
local character = script.Parent
local keyframe = generateKeyframe(character)
print(keyframe)
Tóm Tắt
Thuộc Tính
Phương Pháp
Thêm một KeyframeMarker vào Keyframe bằng cách gắn nó vào khung chính.
Thêm một Pose vào Keyframe bằng cách gắn nó vào khung chính.
Trả về một array chứa tất cả KeyframeMarkers đã được thêm vào Keyframe .
Trả về một array chứa tất cả Poses đã được thêm vào một Keyframe .
Loại bỏ một KeyframeMarker từ Keyframe bằng cách thiết lập Instance.Parent của nó thành nil .
Loại bỏ một Pose từ Keyframe bằng cách đặt Instance.Parent của nó thành nil .
Thuộc Tính
Time
Thuộc tính này cung cấp vị trí thời gian Keyframe (theo giây) trong một hiệu ứng động.Điều này xác định thời gian mà Poses bên trong keyframe sẽ được hiển thị.
Lưu ý Keyframe với giá trị thời gian cao nhất trong một KeyframeSequence được sử dụng để xác định chiều dài của hoạt hiệu ứng động.
Mẫu mã
This sample contains a simple function that will get the length of a KeyframeSequence by finding the Keyframe with the highest Keyframe.Time value.
local function getSequenceLength(keyframeSequence)
local length = 0
for _, keyframe in pairs(keyframeSequence:GetKeyframes()) do
if keyframe.Time > length then
length = keyframe.Time
end
end
return length
end
local keyframeSequence = Instance.new("KeyframeSequence")
getSequenceLength(keyframeSequence)
Phương Pháp
AddMarker
Chức năng này thêm một KeyframeMarker vào Keyframe bằng cách gắn nó vào khung chìa khóa.Nó có chức năng tương tự như việc đặt thẻ chỉ thị của marker Instance.Parent vào Keyframe.
Lưu ý, chức năng này sẽ không xảy ra lỗi khi một ví dụ khác ngoài KeyframeMarker được cung cấp là tham số và sẽ nuôi nó thành công.
Thêm về Keyframes
Keyframe tên không cần phải duy nhất.Ví dụ, nếu một hoạt hình có ba khung chính được đặt tên là "Particles" thì sự kiện kết nối trả về bởi AnimationTrack:GetMarkerReachedSignal() sẽ bắn mỗi khi một trong những khung chính này được đạt tới.
Keyframe tên có thể được đặt trong Trình chỉnh sửa hoạt hình Roblox khi tạo hoặc chỉnh sửa một hiệu ứng động.Tuy nhiên, chúng không thể được đặt bởi một Script trên một hoạt hình hiện có trước khi chơi nó.
Xem thêm:
Tham Số
Các đối tượng KeyframeMarker được gán cho Keyframe .
Lợi Nhuận
Mẫu mã
Ví dụ này minh họa các chức năng Keyframe:AddMarker() và Keyframe:RemoveMarker() .Lưu ý những thứ này có chức năng tương đương với parenting và không chăm sóc các nhãn.
local keyframe = Instance.new("Keyframe")
keyframe.Parent = workspace
local marker = Instance.new("KeyframeMarker")
marker.Name = "FootStep"
marker.Value = 100
keyframe:AddMarker(marker) --marker.Parent = khung chìa khóa
task.wait(2)
keyframe:RemoveMarker(marker) --marker.Parent = nil
AddPose
Chức năng này thêm một Pose vào Keyframe bằng cách gắn nó vào khung chìa khóa.Nó có chức năng tương tự như việc đặt Instance.Parent tư thế vào khung chìa khóa.
Lưu ý, chức năng này sẽ không xảy ra lỗi khi một ví dụ khác ngoài Pose được cung cấp làm tham số tư thế và sẽ nuôi nó thành công.
Tham Số
Lợi Nhuận
Mẫu mã
This sample includes a function that will generate a 'blank' keyframe containing blank poses for all of the model's connected parts in the correct hierarchical order.
local function generateKeyframe(model)
if not model.PrimaryPart then
warn("No primary part set")
return
end
local rootPart = model.PrimaryPart:GetRootPart()
if not rootPart then
warn("Root part not found")
return
end
local partsAdded = {}
partsAdded[rootPart] = true
local function addPoses(part, parentPose)
-- get all of the joints attached to the part
for _, joint in pairs(part:GetJoints()) do
-- we're only interested in Motor6Ds
if joint:IsA("Motor6D") then
-- find the connected part
local connectedPart = nil
if joint.Part0 == part then
connectedPart = joint.Part1
elseif joint.Part1 == part then
connectedPart = joint.Part0
end
if connectedPart then
-- make sure we haven't already added this part
if not partsAdded[connectedPart] then
partsAdded[connectedPart] = true
-- create a pose
local pose = Instance.new("Pose")
pose.Name = connectedPart.Name
parentPose:AddSubPose(pose)
-- recurse
addPoses(connectedPart, pose)
end
end
end
end
end
local keyframe = Instance.new("Keyframe")
-- populate the keyframe
local rootPose = Instance.new("Pose")
rootPose.Name = rootPart.Name
addPoses(rootPart, rootPose)
keyframe:AddPose(rootPose)
return keyframe
end
local character = script.Parent
local keyframe = generateKeyframe(character)
print(keyframe)
This sample demonstrates quickly the Keyframe.AddPose, Keyframe.RemovePose and Pose.AddSubPose and Pose.RemoveSubPose functions. Note these are functionally equivalent to parenting and un-parenting the poses.
local keyframe = Instance.new("Keyframe")
keyframe.Parent = workspace
local pose = Instance.new("Pose")
pose.EasingStyle = Enum.PoseEasingStyle.Cubic
pose.EasingDirection = Enum.PoseEasingDirection.Out
local pose2 = Instance.new("Pose")
pose2.EasingStyle = Enum.PoseEasingStyle.Cubic
pose2.EasingDirection = Enum.PoseEasingDirection.Out
keyframe:AddPose(pose) -- pose.Parent = keyframe
task.wait(2)
keyframe:RemovePose(pose) -- pose.Parent = nil
task.wait(2)
keyframe:AddPose(pose) -- pose.Parent = keyframe
task.wait(2)
pose:AddSubPose(pose2) -- pose2.Parent = pose
task.wait(2)
pose:RemoveSubPose(pose2) -- pose2.Parent = nil
GetMarkers
Chức năng này trả về một mảng chứa tất cả KeyframeMarkers đã được thêm vào Keyframe .Lưu ý, chức năng này chỉ trả về instances của loại KeyframeMarker.
Thêm về Keyframes
Keyframe tên không cần phải duy nhất.Ví dụ, nếu một hoạt hình có ba khung chính được đặt tên là "Particles" thì sự kiện kết nối trả về bởi AnimationTrack:GetMarkerReachedSignal() sẽ bắn mỗi khi một trong những khung chính này được đạt tới.
Keyframe tên có thể được đặt trong Trình chỉnh sửa hoạt hình Roblox khi tạo hoặc chỉnh sửa một hiệu ứng động.Tuy nhiên, chúng không thể được đặt bởi một Script trên một hoạt hình hiện có trước khi chơi nó.
Xem thêm:
Lợi Nhuận
Một mảng chứa tất cả KeyframeMarkers đã được thêm vào Keyframe .
Mẫu mã
Ví dụ này minh họa các chức năng Keyframe:AddMarker() và Keyframe:GetMarkers() .Sau khi thêm hai thẻ, marker1 và marker2 vào khung chìa khóa, ví dụ này nhận và in tên của các thẻ được thêm.
local keyframe = Instance.new("Keyframe")
keyframe.Parent = workspace
local marker1 = Instance.new("KeyframeMarker")
marker1.Name = "FootStep"
marker1.Value = 100
local marker2 = Instance.new("KeyframeMarker")
marker2.Name = "Wave"
marker2.Value = 100
keyframe:AddMarker(marker1) --marker.Parent = khung chìa khóa
keyframe:AddMarker(marker2) --marker.Parent = khung chìa khóa
local markers = keyframe:GetMarkers()
for _, marker in pairs(markers) do
print(marker.Name)
end
GetPoses
Chức năng này trả về một mảng chứa tất cả Poses đã được thêm vào một Keyframe .
Lợi Nhuận
Một mảng của Poses .
Mẫu mã
This code sample includes a function to reset the CFrame of the Poses in a Keyframe.
local function resetPoses(parent)
-- both functions are equivalent to GetChildren
local poses = parent:IsA("Keyframe") and parent:GetPoses() or parent:IsA("Pose") and parent:GetSubPoses()
for _, pose in pairs(poses) do
if pose:IsA("Pose") then
pose.CFrame = CFrame.new()
-- recurse
resetPoses(pose)
end
end
end
RemoveMarker
Chức năng này xóa một KeyframeMarker từ Keyframe bằng cách thiết lập Instance.Parent của nó thành nil .
KeyframeMarker của Instance.Parent được đặt thành nil nhưng nó không bị phá hủy.Điều này có nghĩa, miễn là thẻ được tham chiếu nó có thể được trở lại sau.
Lưu ý, chức năng này sẽ không xảy ra lỗi khi một instance khác ngoài KeyframeMarker được cung cấp làm tham số.
Thêm về Keyframes
Keyframe tên không cần phải duy nhất.Ví dụ, nếu một hoạt hình có ba khung chính được đặt tên là "Particles" thì sự kiện kết nối trả về bởi AnimationTrack:GetMarkerReachedSignal() sẽ bắn mỗi khi một trong những khung chính này được đạt tới.
Keyframe tên có thể được đặt trong Trình chỉnh sửa hoạt hình Roblox khi tạo hoặc chỉnh sửa một hiệu ứng động.Tuy nhiên, chúng không thể được đặt bởi một Script trên một hoạt hình hiện có trước khi chơi nó.
Xem thêm:
Tham Số
Lợi Nhuận
Mẫu mã
Ví dụ này minh họa các chức năng Keyframe:AddMarker() và Keyframe:RemoveMarker() .Lưu ý những thứ này có chức năng tương đương với parenting và không chăm sóc các nhãn.
local keyframe = Instance.new("Keyframe")
keyframe.Parent = workspace
local marker = Instance.new("KeyframeMarker")
marker.Name = "FootStep"
marker.Value = 100
keyframe:AddMarker(marker) --marker.Parent = khung chìa khóa
task.wait(2)
keyframe:RemoveMarker(marker) --marker.Parent = nil
RemovePose
Chức năng này xóa một Pose từ Keyframe bằng cách đặt Instance.Parent của nó thành nil mà không phá hủy nó.Điều này có nghĩa là tư thế được cung cấp được tham chiếu và có thể được trở lại sau.
Lưu ý, chức năng này sẽ không xảy ra lỗi khi một ví dụ khác ngoài một Pose được cung cấp làm tham số tư thế.
Tham Số
Lợi Nhuận
Mẫu mã
This sample demonstrates quickly the Keyframe.AddPose, Keyframe.RemovePose and Pose.AddSubPose and Pose.RemoveSubPose functions. Note these are functionally equivalent to parenting and un-parenting the poses.
local keyframe = Instance.new("Keyframe")
keyframe.Parent = workspace
local pose = Instance.new("Pose")
pose.EasingStyle = Enum.PoseEasingStyle.Cubic
pose.EasingDirection = Enum.PoseEasingDirection.Out
local pose2 = Instance.new("Pose")
pose2.EasingStyle = Enum.PoseEasingStyle.Cubic
pose2.EasingDirection = Enum.PoseEasingDirection.Out
keyframe:AddPose(pose) -- pose.Parent = keyframe
task.wait(2)
keyframe:RemovePose(pose) -- pose.Parent = nil
task.wait(2)
keyframe:AddPose(pose) -- pose.Parent = keyframe
task.wait(2)
pose:AddSubPose(pose2) -- pose2.Parent = pose
task.wait(2)
pose:RemoveSubPose(pose2) -- pose2.Parent = nil