Sprite Scale


#1

Hi,

I have the following code that randomize a value between 1.8 - 2.8. I would like to set the size of my sprite to the randomized value. However, when I print the value it is either 1 or 2.

function init(self)
	math.randomseed(socket.gettime()) math.random()math.random()
	local size_random = math.random(1.8, 2.8)
	self.resize_asteroid = vmath.vector3(size_random, size_random, 0)
	go.set("#asteroid", "scale", self.resize_asteroid)
end

What is wrong here?


#2

You must scale the GO the sprite is attached to if you want to scale a sprite at runtime.

The Defold way is to put GOs with sprites in them in collections and spawn them with collection factories. Then you can address the GOs relatively to each other in their scripts.


#3

Furthermore, when math.random is given a range of values, it only provides integer results.

Something like this should work instead:

local size_random = math.random(180, 280) / 100