Okay so i’m a person who used to use Spritekit, and I used to use Cocos(But too much Resources… That’s why I moved to Defold) So I started using Defold because it is lightweight, easy to collaborate, and most importantly Cross-Platform… but here’s when I have a question, In both engines there’s actions that Sprites or objects can run to move or whatever… For example you could have a Sprite move to X coordinate 150; and it would, but importantly the engine would move it smoothly almost like an animation, Is there anyway to run “Actions” in Defold, for now I’m using go.set_position()
to change the position of the character but is there anyway to smoothly have the character “move(Not just teleport instantly, but to almost walk there)” to a certain position ?
This is basically what I want to do
2 Likes
You could do this with a simple script on the sprite’s object - I haven’t done that much with SpriteKit, so this might not quite parallel how things work in that but hopefully it’s close:
function init(self)
self.move_time = 0
self.velocity = vmath.vector3(0)
end
function update(self, dt)
local t0 = self.move_time
self.move_time = self.move_time - dt
if self.move_time < 0 then self.move_time = 0 end
local t1 = self.move_time
go.set_position(go.get_position() + self.velocity * (t0 - t1))
end
function on_message(self, message_id, message, sender)
if message_id == hash("move") then
self.move_time = message.time
self.velocity = message.displacement / message.time
end
end
If you attach this script to the object with the sprite, sending a message to your object like this should make it move (displacement) in (time) seconds:
msg.post("/my_object", "move", {time = (time), displacement = (displacement)})
Let me know if there’s anything I need to clarify, I’m trying to figure out how word my answer less awkwardly and it’s not working 
1 Like
So looks like it did not work… but I found an example that worked, I am just having a hard time understanding the logic of it… so this is the script I copied, an object sends a message after a trigger, and the camera Starts moving…
go.property("speed", 70)
Is_Started_Yet = false
currentPositionX = 100.67755126953
function init(self)
-- Add initialization code here
-- Remove this function if not needed
msg.post("#camera", "acquire_camera_focus")
end
function update(self, dt)
if Is_Started_Yet then
local pos = go.get_world_position()
pos.x = math.max(35, pos.x + self.speed * dt)
go.set_position(pos)
end
end
function on_message(self, message_id, message, sender)
-- Add message-handling code here
-- Remove this function if not needed
if message_id == hash("Started") then
Is_Started_Yet = true
end
end
'
So can you or another member here help me understand the logic of this script?
1 Like
go.property("speed", 70)
In stead of defining self.speed = 70
it’s defined as a property of det Gameobject. This can be set from the editor, when you click on the game object in your collection.
Is_Started_Yet = false
This is just a variable, tracking the movement-state of object. Could have been self.moving = false
as well. Prevents the camera from moving before it’s supposed to.
currentPositionX = 100.67755126953
Another variable containing the objects current position. Could have been self.currentPosition = XXX
. (Doesn’t look like it’s used for anything in your snippet)
function init(self)
-- Add initialization code here
-- Remove this function if not needed
msg.post("#camera", "acquire_camera_focus")
end
This just “activates” the camera upon initialization.
function update(self, dt)
-- Only move if told by message
if Is_Started_Yet then
-- Get game object world position
local pos = go.get_world_position()
-- Set pos.x to the maximum value between 35 and current position + (70 * dt)
pos.x = math.max(35, pos.x + self.speed * dt)
-- Move game object to new position.
go.set_position(pos)
end
end
Added comments
function on_message(self, message_id, message, sender)
-- Object got a message to start
if message_id == hash("Started") then
-- Toggle movement state
Is_Started_Yet = true
end
end
This is just the message receiver. Something is telling the camera to start moving.
2 Likes
If you just want to animate a game object, check out go.animate()! See http://www.defold.com/ref/go/#go.animate
3 Likes
So, I guess that there’s two main ways to move something:
- Move from point A to point B in a straight line and in a specific amount of time
- Move in varying direction with or without variable speed, based on either user input or some logic (AI or otherwise)
In case #1 the obvious and most performant choice would be to use go.animate() and animate position, specify a destination position, a duration and an easing function:
-- cancel any already existing animation
go.cancel_animations("my_go", "position")
-- move 100 to the right in 1.5 seconds with 0 seconds delay
go.animate("my_go", "position", go.PLAYBACK_ONCE_FORWARD, go.get_position() + vmath.vector3(100, 0, 0), go.EASING_LINEAR, 1.5, 0)
I use this kind of animation to move the aliens in the Space Invaders game I made.
In case #2 you most likely need to animate the game object programmatically based on some algorithm and according to some rules. In such a case you read user input or set some variables (speed, direction etc) and use the update(self, dt)
function to continuously move the game object. Use delta-time to get consistent movement!
I created an example that shows the two ways of movement here: https://github.com/britzl/publicexamples/tree/master/examples/game_object_movement
4 Likes
Thank you all so much, makes so much more sense now!
1 Like