Programmatic Image Reference
netdzynr
Member Posts: 296
Maybe this is old news, but after spending a fair amount of time searching for a way to randomly display images without using a bunch of tedious rules and actors, I came across one way to accomplish programmatic image reference.
Say you have 5 images named display1.png, display2.png, display3.png, etc that you want to display in one actor.
- Create a text attribute ImgName, set this to "display" (no quotes)
- Create a text attribute ImgType, set this to ".png" (no quotes)
- Create an empty text attribute ImgPath
- Create an empty integer attribute ImgNum
To assign a random image to an actor while running your game:
- set the value of ImgNum to your random number: game.ImgNum = random(1,5)
- set the value of ImgPath to game.ImgName..game.ImgNum..game.ImgType (note the double dots)
- set the self.Image of your actor to game.ImgPath
You actor should now be able to randomly display any imported image without having to use separate actors and rules for each image. If anyone knows a better way to do this, I would love to know how.
Say you have 5 images named display1.png, display2.png, display3.png, etc that you want to display in one actor.
- Create a text attribute ImgName, set this to "display" (no quotes)
- Create a text attribute ImgType, set this to ".png" (no quotes)
- Create an empty text attribute ImgPath
- Create an empty integer attribute ImgNum
To assign a random image to an actor while running your game:
- set the value of ImgNum to your random number: game.ImgNum = random(1,5)
- set the value of ImgPath to game.ImgName..game.ImgNum..game.ImgType (note the double dots)
- set the self.Image of your actor to game.ImgPath
You actor should now be able to randomly display any imported image without having to use separate actors and rules for each image. If anyone knows a better way to do this, I would love to know how.
Comments
So lets say you want to select a random image out of 5 when an object is spawned. Make sure all the images are named in a sequence (ex. myImage-1-name.png, myImage-2-name.png). Now add a Change Attribute rule to your actor and change self.Image to the following expression, including quotes):
"myImage-"..random(1,5).."name.png"
Now when the actor is created, one of the five images will be randomly chosen. I hope this helps someone to simplify the process.
YES, that helps and I will try this right now!
If it doesn't .... I'll be back!
What is the simplest way to call a random image onto a scene? To trigger the event, one would click a button!
thanks