Help me please with sprites in ES.D (SOLVED)

Hello fellow developers.
I have some strange problem and stuck with it.
All code works but when the sprite is out of view and I tried to show it (set tint/alpha to 1) and move to screen I see only part of it.
Game object is composite: body/outfit/emotions

I tried all I can, but this does not help (fight with this bug for weeks)
That’s is great if you can help me with this pesky blocker bug. I cannot continue development until the scene is fixed.

Here is code for sprites manipulation:

 elseif( message_id == hash("show_animated_from_left"))then
		print('slavya_manager:show_animated_from_left')
		print('emotion: '..emotion..' outfit: '..outfit)
		enable()
		set_slavya_alpha( 1.0 )
		set_new_emotion_and_outfit()
		set_position( 0, CENTER_Y )
		animate_pos( CENTER_X, 0, 1.0)

function enable()
	msg.post("#emotion", "enable" )
	msg.post("#body", "enable")     	
	msg.post("#outfit", "enable")
        msg.post(blink_sprite, "enable")
end

 function set_slavya_alpha( to_a )
	set_slavya_tint( old_r, old_g, old_b, to_a )
end

local old_r = 1
local old_g = 1
local old_b = 1
local old_a = 1

function set_slavya_tint( r, g, b, a )
	if( a == nil )then a = 1 end	

	old_r = r
	old_g = g
	old_b = b
	old_a = a


	sprite.set_constant("#emotion", "tint", vmath.vector4(r, g, b, a))
	sprite.set_constant("#body", "tint", vmath.vector4(r, g, b, a))
	sprite.set_constant("#outfit", "tint", vmath.vector4(r, g, b, a))
end

function set_new_emotion_and_outfit()
   	set_emotion( emotion, outfit )
end

function set_emotion( emotion, outfit )
	print("slavya_manager.script: set_emotion (emotion: " .. tostring(emotion) .. ", outfit: ".. tostring(outfit) .. ")")

	if(outfit == nil)then
		print("slavya_manager.script: set_emotion outfit is nil")
		outfit = OUTFIT_PIONEER
	end
	
	local pose = POSES[emotion]
	local outfit = pose.outfit[outfit]
	print('Pose: body>'..pose.body..' emotion>'..pose.emotion..' outfit>'..outfit)

	msg.post("#emotion", "play_animation", {id=hash(pose.emotion)})
	msg.post("#body", "play_animation", {id=hash(pose.body)})     	
	msg.post("#outfit", "play_animation", {id=hash(outfit)})
	
	if( blink_sprite ~= "" )then
		msg.post( blink_sprite, "disable" )
	end
	
	blink_sprite = pose.blink.sprite
	blink_animation = pose.blink.animation
	
	print( "emotion " .. emotion .. " outfit " .. outfit .. " blink animation " .. blink_animation )
	
	if( blink_sprite ~= "" )then
		msg.post( blink_sprite, "enable")
	else
		msg.post(blink_sprite, "disable")
	end  
end

function set_position( to_x, to_y )
	local pos = vmath.vector3()
	pos.x = to_x
	pos.y = to_y
	go.set_position( pos )	
end

function animate_pos( to_x, to_y, duration )
	if( to_x ~= 0 )then
		go.animate(".", "position.x", go.PLAYBACK_ONCE_FORWARD, to_x, go.EASING_LINEAR, duration )
	end

	if( to_y ~= 0 )then
		go.animate(".", "position.y", go.PLAYBACK_ONCE_FORWARD, to_y, go.EASING_LINEAR, duration )
	end	
end

After some investigation I found that code:

set_position(-200,0) 

function set_position( to_x, to_y )
	local pos = vmath.vector3()
	pos.x = to_x
	pos.y = to_y
	go.set_position( pos )	
end

is not working. I use animation for showing/hiding

function animate_pos( to_x, to_y, duration )
	if( to_x ~= 0 )then
		go.animate(".", "position.x", go.PLAYBACK_ONCE_FORWARD, to_x, go.EASING_LINEAR, duration )
	end

	if( to_y ~= 0 )then
		go.animate(".", "position.y", go.PLAYBACK_ONCE_FORWARD, to_y, go.EASING_LINEAR, duration )
	end	
end

This code changes game object body position and set_position is set it relative to the current position (I do not know why).
And part of the game object (body) is moved out of the screen.

Strange bug.
I found a workaround but will be very glad to see your opinion on this problem (I definitely need to set the position of my game object by hand by scenario)

What’s the z position of the character sprites and background?

2 Likes

body: 0
emotion: 0.2
outfit: 0.1

Do you think that background randomly placed over the body?
Which kind of Z should I make to resolve this issue?

Thank you. I change all backgrounds to Z 0 and all works fine.
(my current background had z 0.1)

3 Likes

By default Defold only renders sprites between -1 and 1. This is true for GUI stuff also (but you shouldn’t need to fiddle with Z possitions there). You can change this behaviour by copying and the render_script and putting in your own values.

3 Likes

Thank you for a detailed answer. I will use this range for future sprites as well

1 Like

Cool. The precision is good on these values so you can have things on 0.001 and 0.002 etc if you want, and even more fine-grained.

1 Like

Thank you. Almost forgot about how precise float can be (up to 8 digits).

If you want to know how precision is affected with the magnitude of the number, there is a post about floating point numbers

3 Likes

Thank you for the interesting link. I read it thoroughly

1 Like