Sprite doesn't update position immediately

I have a gameobject with 2 sprites on it. When I move the object sprite 1 is active when the object is stationary sprite 2 will be shown. When I enable sprite 2 the image will first show itself in the last postion it was active for one frame, and then teleport to the right position (where the gameobject is). How can I solve this problem?

I’m trying to reproduce your problem but I don’t see any error. Maybe I misunderstand what you do. Here’s what I tried:

  1. I created a game object with two sprites, “sprite1” and “sprite2”.
  2. I create a script component for the game object.

Script code:

function init(self)
	self.t = 0
	self.x = false
	go.set_position(vmath.vector3(100, 100, 0))
end

function update(self, dt)
	self.t = self.t + dt
	
	if self.t > 1 then
		if self.x then
			msg.post("#sprite2", "enable")
			go.set_position(vmath.vector3(100, 100, 0))
		else
			msg.post("#sprite2", "disable")
			go.set_position(vmath.vector3(200, 100, 0))
		end
		self.x = not self.x		
		self.t = 0
	end
end

If I understand your problem correctly “sprite2” would appear in the wrong place for 1 frame each second, but I don’t see that happening.

That’s almost what I’m trying to do. Although you seem to always have the sprite2 visible at (100, 100) location. Sprite 2 is never active at the (200, 100) location.

Maybe try reversing the order of these lines:
msg.post("#sprite2", “disable”)
go.set_position(vmath.vector3(200, 100, 0))

*Tried some version of it and couldn’t replicate.

Manage to replicate it with the following code:

function init(self)
self.t = 0
self.x = false
go.set_position(vmath.vector3(100, 100, 0.5))
msg.post("#sprite1", “disable”)
end

function update(self, dt)
self.t = self.t + dt

if self.t > 3 then
	if self.x then
		go.set_position(vmath.vector3(100, 100, 0.5))
		msg.post("#sprite1", "disable")
		msg.post("#sprite2", "enable")
		msg.post("#sprite2", "play_animation", {id = hash("anim")})
	else
		go.set_position(vmath.vector3(200, 100, 0.5))
		msg.post("#sprite1", "disable")
		msg.post("#sprite2", "enable")
		msg.post("#sprite2", "play_animation", {id = hash("anim")})
	end
	self.x = not self.x		
	self.t = 0
end

end

function on_message(self, message_id, message, sender)
if message_id == hash(“animation_done”) then
msg.post("#sprite1", “enable”)
msg.post("#sprite2", “disable”)
end
end

Can you share your project with me because I can’t replicate with your code?

Sure, how do I share my project?

Just add me to it in the dashboard. My mail is mikael@sicher.org

Make sure you have synced so I get everything I need.

So, you should be invited.
You should just have to build and play and you will get to a examplescene when you can observe the bugg

Ah, you sent me an invite. You need to add me to the project. Click on the project link and add my mail at the bottom of the “Members” list.

1 Like

Thanks. I see the issue here. Will investigate a bit.

We’ve looked at the issue and it is definitely a bug. Unfortunately have have no workaround at the moment but this needs to be fixed. I’m filing a bug report.

EDIT: This seems to only happen if you enable the sprite on the “animation_done” message.

Thanks for the find!

1 Like