Games Modify image when creating game object from template

Andrew Gray

Member
Licensed User
Longtime User
I'm creating an object from a template in a tile map but I want to set the image programmatically at the time of object creation. Is this possible?

I tried the following, and various alternatives, but none worked:

B4X:
    Dim template As X2TileObjectTemplate = TileMap.GetObjectTemplateByName(ObjectLayer, "ball template")
    template.CustomProps.Put("graphic file 1", "ball" & Rnd(0, BallCount) & ".png")
    TileMap.CreateObject(template)
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
The graphics are part of the template and are created when you call TileMap.PrepareObjectsDef.

One possible way to change the image is by loading it yourself. For example:
B4X:
X2.GraphicCache.PutGraphic("coin", Array(X2.LoadBmp(File.DirAssets, "coin.png", 2, 2, True)))
Car.GraphicName = "coin" 'Car is a X2BodyWrapper
You can load all the different balls once and then change the GraphicName of each object.
 
Top