Difference between revisions of "MoveObj (CMS)"

From The Official Visionaire Studio: Adventure Game Engine Wiki
m (Text replacement - "wikitable" to "ts")
m
 
(3 intermediate revisions by the same user not shown)
Line 3: Line 3:
 
! 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, delay, easing) || Definition || AFRLme
+
| 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 specified easing.
+
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/>
2. To use this function you should create an ''execute a script'' action containing...  
+
2a. Usage Example #1: move object immediately from current position to 100, 100.
<syntaxhighlight>
+
<syntaxhighlight lang="lua">
moveObj("object_name", 300, 450, 5000, easeQuintOut) -- move object_name to 350x450 over 5000 milliseconds with easeQuintOut
+
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, delay, easing)
+
function moveObj(obj, x, y, duration, easing, pos)
  obj = getObject("Game.GameCurrentScene.SceneObjects[" .. obj .. "]")
+
duration = duration or 0 -- fallback in case duration equals nil
  startObjectTween(obj, VObjectOffset, obj:getPoint(VObjectOffset), {x = x, y = y}, delay, easing)
+
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 ==
Line 28: Line 49:
 
! 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 affect.
+
| obj || "string" || This should be a "string" value containing the name of the object you want to move.
 
|-
 
|-
| x || integer (number) || This should be a an integer value of the x coordinate you want to move the object to.
+
| x || integer || This should contain a number value consisting of the x coordinate you want to move the object to.
 
|-
 
|-
| y || integer (number) || This should be a an integer value of the y 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.
 
|-
 
|-
| delay || integer (number) || This should be a an integer value of the amount of time (milliseconds) it should take the object to get from a to b.
+
| 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 (number) || This should contain the integer value or name of the easing you want to set.
+
| 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.