Difference between revisions of "MoveObj (CMS)"
From The Official Visionaire Studio: Adventure Game Engine Wiki
(→Syntax Breakdown) |
m |
||
| (4 intermediate revisions by 2 users not shown) | |||
| Line 1: | Line 1: | ||
| − | {| class=" | + | {| class="ts" style="width:100%" |
|- | |- | ||
! style="text-align:left" | Name !! style="text-align:left" | Type !! style="text-align:left" | By | ! style="text-align:left" | Name !! style="text-align:left" | Type !! style="text-align:left" | By | ||
|- | |- | ||
| − | | moveObj("obj", x, y, | + | | moveObj("obj", x, y, duration, easing) || Definition || AFRLme |
|} | |} | ||
| − | This small function allows you to move an object from one position to another over x amount of time with | + | This small function allows you to move an object immediately from one position to another (absolute positioning); or over x amount of time with optional easing. |
| + | <hr> | ||
| + | {| class="ts" | ||
| + | |- | ||
| + | | ''Quick note: position is based on the top-left pixel of the sprite canvas belonging to the linked image or animation.'' | ||
| + | |} | ||
| + | <hr> | ||
== Instructions == | == Instructions == | ||
1. Add the [[#Main_Script|main script]] to the Visionaire Studio Script Editor & set the script as a definition script.<br/> | 1. Add the [[#Main_Script|main script]] to the Visionaire Studio Script Editor & set the script as a definition script.<br/> | ||
| − | + | 2a. Usage Example #1: move object immediately from current position to 100, 100. | |
| − | <syntaxhighlight> | + | <syntaxhighlight lang="lua"> |
| − | moveObj("object_name", | + | moveObj("object_name", 100, 100) |
| + | </syntaxhighlight> | ||
| + | 2b. Usage Example #2: move object over 2000ms from current position to 100, 100 with easeBounceOut easing. | ||
| + | <syntaxhighlight lang="lua"> | ||
| + | moveObj("object_name", 100, 100, 2000, easeBounceOut) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| + | |||
== Main Script == | == Main Script == | ||
| − | <syntaxhighlight> | + | <syntaxhighlight lang="lua"> |
| − | function moveObj(obj, x, y, | + | function moveObj(obj, x, y, duration, easing, pos) |
| − | obj = | + | duration = duration or 0 -- fallback in case duration equals nil |
| − | + | easing = easing or easeLinearInOut -- fallback in case easing equals nil | |
| + | obj = game.CurrentScene.Objects[obj] -- store object | ||
| + | -- + -- | ||
| + | if obj.Animation:isEmpty() then -- check if animation isn't linked to scene object | ||
| + | pos = obj.Sprite.Sprite:getPosition() -- store object sprite position (top left pixel) | ||
| + | else | ||
| + | pos = ActiveAnimations[obj.Animation:getName()].CurrentPosition -- store linked animation position (top left pixel) | ||
| + | end | ||
| + | -- + -- | ||
| + | obj:to(duration, { Offset = {x = x - pos.x, y = y - pos.y} }, easing) -- update object position | ||
end | end | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| + | |||
== Syntax Breakdown == | == Syntax Breakdown == | ||
| − | {| class=" | + | {| class="ts" style="width:100%" |
|- | |- | ||
! style="text-align:left" | Name !! style="text-align:left" | Type !! style="text-align:left;width:80%" | Description | ! style="text-align:left" | Name !! style="text-align:left" | Type !! style="text-align:left;width:80%" | Description | ||
|- | |- | ||
| − | | obj || "string" || This should be a "string" value containing the name of the object you want to | + | | obj || "string" || This should be a "string" value containing the name of the object you want to move. |
|- | |- | ||
| − | | x || integer | + | | x || integer || This should contain a number value consisting of the x coordinate you want to move the object to. |
|- | |- | ||
| − | | y || integer | + | | y || integer || This should contain a number value consisting of the y coordinate you want to move the object to. |
|- | |- | ||
| − | | | + | | duration || integer || This should be a an integer value of the amount of time in milliseconds it should take the object to get from current position to the target position. |
|- | |- | ||
| − | | easing || integer | + | | easing || integer || This should contain a number value, or the variable name of the easing type that you want to use. Here is a list of available [https://www.visionaire-studio.com/luadocs/lua.html#easing easing options]. |
| − | |} | + | |}{{toc}} |
Latest revision as of 17:11, 23 August 2022
| Name | Type | By |
|---|---|---|
| moveObj("obj", x, y, duration, easing) | Definition | AFRLme |
This small function allows you to move an object immediately from one position to another (absolute positioning); or over x amount of time with optional easing.
| Quick note: position is based on the top-left pixel of the sprite canvas belonging to the linked image or animation. |
Instructions
1. Add the main script to the Visionaire Studio Script Editor & set the script as a definition script.
2a. Usage Example #1: move object immediately from current position to 100, 100.
moveObj("object_name", 100, 100)
2b. Usage Example #2: move object over 2000ms from current position to 100, 100 with easeBounceOut easing.
moveObj("object_name", 100, 100, 2000, easeBounceOut)
Main Script
function moveObj(obj, x, y, duration, easing, pos)
duration = duration or 0 -- fallback in case duration equals nil
easing = easing or easeLinearInOut -- fallback in case easing equals nil
obj = game.CurrentScene.Objects[obj] -- store object
-- + --
if obj.Animation:isEmpty() then -- check if animation isn't linked to scene object
pos = obj.Sprite.Sprite:getPosition() -- store object sprite position (top left pixel)
else
pos = ActiveAnimations[obj.Animation:getName()].CurrentPosition -- store linked animation position (top left pixel)
end
-- + --
obj:to(duration, { Offset = {x = x - pos.x, y = y - pos.y} }, easing) -- update object position
end
Syntax Breakdown
| Name | Type | Description |
|---|---|---|
| obj | "string" | This should be a "string" value containing the name of the object you want to move. |
| x | integer | This should contain a number value consisting of the x coordinate you want to move the object to. |
| y | integer | This should contain a number value consisting of the y coordinate you want to move the object to. |
| duration | integer | This should be a an integer value of the amount of time in milliseconds it should take the object to get from current position to the target position. |
| easing | integer | This should contain a number value, or the variable name of the easing type that you want to use. Here is a list of available easing options. |