Sprite teleporting off the screen

I am trying to make a sprite snap to the mouse pointer using this code:

function on_input(self, action_id, action)
	if action.x and action.y then
		go.set_position(vmath.vector3(action.x, action.y, 0))
		print('good')
		print(action.x, action.y, 0)
	end
end

when I run the game it teleports off the screen like this:


and I’m probably doing everything right while creating the sprite because it works when I comment out the go.set position function.

Code looks fine to me. Only thing that springs to mind is the z-value - perhaps the sprite is behind the background somehow. Trying changing the z component to 0.5 in your go.set_position statement.

2 Likes

Oh actually that’s pretty simple thx

Also could you help me with making a class I have no idea how to even though I googled it.

A class like in OOP languages? Lua introduces only tables (Lua’s associative array) as a type that is like a class: Programming in Lua : 16.
You can store functions in variables, because Lua treats functions as first-class types, so using such power you can add functions to tables and that’s how you make a structure/class! :wink:
If you seek for inheritance, check out: Programming in Lua : 16.2 or search for some repositories with libraries (modules) providing somehow different, maybe more convenient approach for OOP.
If you want to group some tables or functions you can utilize modules: Lua modules in Defold :wink:

So, simple example if you want to create a class for handling animations:

local Animator_Class = {
  current_anim = hash("idle")
  function play_anim(anim)
    if current_anjm ~= anim then
      sprite.play_flipbook("#sprite", anim)
      current_anim = anim
    end
  end
}

Then you can call functions from your class:

Animator_Class.play_anim(hash("run"))

Then you can advance your class, for example if you want to create different instances of the class you might want to write a function that returns an instance of your table (your class) or use metatables: Programming in Lua : 13

4 Likes