My question is can you use one factory to span different sprites or does it require one factory one sprite?
So if i have one factory can it be used to select which sprite to spawn with any number of sprites at random?
Regards,
A
My question is can you use one factory to span different sprites or does it require one factory one sprite?
So if i have one factory can it be used to select which sprite to spawn with any number of sprites at random?
Regards,
A
You can change sprite quite easily by passing the name of the atlas image to the game object and in the init() function of a script attached to the game object you set the correct image for the sprite. Example:
factory.create("#factory", nil, nil, { image = "bullet2" })
SCRIPT ATTACHED TO THE GAME OBJECT YOU’RE CREATING:
-- expose a property that we use to decide which image in the sprite atlas to use
go.property("image", hash("bullet1"))
function init(self)
-- change sprite image to the one from the exposed property
msg.post("#sprite", "play_animation", { id = self.image })
end
When i add the image=“red” i get the following error, if i remove it then it generates all the sprites in the same places one under one another so to speak so im struggling with factory generation of different sprites and randomly selecting sprites
ERROR:GAMEOBJECT: Properties can not be of type ‘string’.
ERROR:GAMEOBJECT: Could not load properties when spawning ‘/asteroid/asteroid.goc’.
ERROR:GAMEOBJECT: Could not spawn an instance of prototype /asteroid/asteroid.goc.
asteroid_id=factory.create(“main:/asteroid/asteroid#factory”, vmath.vector3(x,y,0), nil, {score=score_val,posx=x,posy=y,dir=direction,side=left_right,rand_angle=rand_angle,spin=math.random(4),speed=math.random(3),size=new_size,image=“red”},new_size)
In our game we have done this with one of two different solutions.
The first:
function init(self) msg.post("#one", "disable") msg.post("#two", "disable") if math.random(1,100)<50 then msg.post("#one", "enable") else msg.post("#two", "enable") end end
The second:
This is more complicated but also more versatile. In that you create a game object for each different type you want to randomly create and then create another game object that acts as a proxy, with a factory for each of the type game objects. Attach a script to the proxy that fires one of the sub-factories and then delete the proxy. Something like:
function init(self) if math.random(1,100)<50 then factory.create("#one") else factory.create("#two") end end
Use hash values. String properties are not allowed.
Yeah, @AppsTrader, in my example I declared the property as a hash in go.property() but when I sent the property value via factory.create() I sent it as a string. It should have been like this:
factory.create("#factory", nil, nil, { image = hash("bullet2") })
IMPORTANT: With my suggested solution you should have one sprite. Not many as in your screenshot. The purpose of this line:
msg.post("#sprite", "play_animation", { id = self.image })
Is to tell the sprite component to change which image to use from the atlas you assigned to your sprite.