Can I change an actors image with an off-screen script?
JCFord
Member Posts: 785
I have 20 buttons on-screen and when each button is clicked the button spawns a blank actor called 'MASTER SPAWNED' and depending on which of the 20 buttons was clicked I have a long script inside the 'MASTER SPAWNED' actor that will change the image to one of 20 possible images.
On each of the 20 buttons when clicked I change an attribute called 'Game.WhichImage' so if button 1 is pressed then Game.WhichImage = 1 etc and then this is then passed onto the 'SPAWNED MASTER' and this changes it to the relevant image. i.e.
CHANGE ATTRIBUTE
change: Self.WhichImage to Game.WhichImage
IF attribute Self.Which.Image = 1
Change image to Image1.png
IF attribute Self.Which.Image = 2
Change image to Image2.png
etc etc etc right the way through to:
IF attribute Self.Which.Image = 20
Change image to Image 20.png
Although this all works. I can have a large number of these 'MASTER SPAWNED' actors on screen, is there a way that I could optimise this long script by just having it once in a hidden actor off-screen rather than repeating it in every SPAWNED ACTORS. So the hidden actor would change the 'MASTER SPAWNED' image?
I have tried but cant seem to get it to work, as I can only see a way of having the changing image action in the relevant actor itself?
Hope this makes sense!
JCFord
On each of the 20 buttons when clicked I change an attribute called 'Game.WhichImage' so if button 1 is pressed then Game.WhichImage = 1 etc and then this is then passed onto the 'SPAWNED MASTER' and this changes it to the relevant image. i.e.
CHANGE ATTRIBUTE
change: Self.WhichImage to Game.WhichImage
IF attribute Self.Which.Image = 1
Change image to Image1.png
IF attribute Self.Which.Image = 2
Change image to Image2.png
etc etc etc right the way through to:
IF attribute Self.Which.Image = 20
Change image to Image 20.png
Although this all works. I can have a large number of these 'MASTER SPAWNED' actors on screen, is there a way that I could optimise this long script by just having it once in a hidden actor off-screen rather than repeating it in every SPAWNED ACTORS. So the hidden actor would change the 'MASTER SPAWNED' image?
I have tried but cant seem to get it to work, as I can only see a way of having the changing image action in the relevant actor itself?
Hope this makes sense!
JCFord
Comments
then use 'change attribute' behaviour to change image rather than 'change image' behaviour, seems to work well.
then have a game attribute called game.PicNumber and set it to one.
Then you can have a rule that says when touch is pressed (or some other attribute changes etc.) change attribute self.image to "pic"..game.PicNumber..".png"
This way whenever you change game.PicNumber it will cause the image to change to that file with the corresponding number.
button loads filename into text attribute
spawned actor loads self.image according to text attribute
I created a attribute called game.TextHolder (text) and for each of the 20 buttons when they are clicked write the PNG file name to game.TextHolder. Then in my 'MASTER SPAWNER' I now just have 2 behaviours
Change: self.TextHolder to game.TextHolder
Change: self.Image to self.TextHolder
Works a treat, so thanks Rob2 & Scitunes too.
In the spawned Actor:
Change Attribute
self.Image To: "image"..game.currentImageNumber..".png"
You just have to name your images in a particular way, like this:
image1.png
image2.png
image3.png
image4.png
What you're now doing with the text attribute works as well.
Basically, just use Change Attribute: self.Image instead of the Change Image behavior.
I asked for similar abilities in the Change Scene behavior which would be particularly helpful in games with many levels:
Change Scene To: "Scene"..(game.currentScene+1)
or:
Change Scene To: "Scene"..random(1,75)
This method could also work with Play Sound as well.
JCFord