Set Position Of A Sprite In Code? (SOLVED)

Hi,

How do we set the position of a sprite in code?
Thanks!

Jesse

You position the game object and not the sprite. You can use go.set_position() for this.

Ok, think I understand…

The new sprite game object is called: “PauseBG.go”, how would I set it’s position?
Thanks!

Jesse

You create a new vector and set the position:

local position = vmath.vector3(100, 250, 0)

and set the position of the game object to the new vector:

 go.set_position(position, "PauseBG")

Ok, got that, but centering the sprite global object does not work?
(it’s not shown on the screen when game is run - screen size is 320x640)
Here is my code:
go.set_position( vmath.vector3(180, 320, 1) , "PauseBG")

Any ideas?

Jesse

Works with below:
go.set_position( vmath.vector3(180, 320, 0), "PauseBG" )

Really confused about “z”… setting above “z” to “1” does not seem to be working?

Jesse

This is presumably because you are using a default render script with a default range of -1 to 1, which means anything out of that z range would not be drawn. You need to copy the builtin render script and edit that range or use something like Orthographic or Rendercam which has it as an option.

You can also set the z value of things to very small incremental numbers like 0.001 as long as they are in the range they will render.

1 Like

It’s my understanding that “z” axis purpose is for layering images?
go.set_position( vmath.vector3(180, 320, 1) , "PauseBG")
Above should place image above everything else, but it fails to draw?
Totally confused…

You should check out the documentation, it’s really easy (and fast!) to look up this information yourself

5 Likes

Okay, you need to go into the collection editor and set the sprite’s z value to 0. Then, you can set the Game Object’s layer to 1 and it should render.
If the sprite’s z value is 1 and the game object that contains it is 1, then the total z value is 2, and the z value must between -1 and 1 to render (remember, you can set z to 0.01 or 0.0001)

3 Likes

ok, that makes some sense…
Thanks!