box changes colour and number in order

im trying to figure out how to create a box changes colour in order and while changing colour its number also changes.for example; a box has a 34 number on it, when it collides with enemy, for every enemy he decrease number and changes colour depends on number. is this even possible in GDevelop? lol or help me pls ty^^

The most obvious way to do something like this is animations. Have you thought about that?
Just make all the different colour images with numbers on them in a paint tool and add the images as animations to the object. You can even make it so the animation numbers are identical to the numbers on the images so animation number 1 going to include the box with the number 1 on it and you can even name the animations to be identical to the colour of the images, so animation “red” going to include the red box image… And then when the box collide with enemy, simply change the animation of the box using the animation number or animation name as it fit you best.

If, for any reason you don’t want to use animations and you want to generate the colour and the number on the fly, you need to use a white image for the sprite and then you can change the colour of the sprite using the change sprite colour event and for the number you can use a text object on the top of the box object to display a number, in case the box is moving you need to move the text object too…

actually i want to create snake vs block like game. first i thought a spawner that generates random colour and number that matches with its colour.for example, every 5 is green,47 is orange… but couldnt understand text display options and GDevelop lack of documentation about that and someother things i think.
text and box moves together all way down.

i tried to do simple 1 ball and 2 objects colliding each other project. “1” is just 1 frame with number 1 on it. “2” is just 2 frame ( 1 and 2 )with number 2 on it. goal: everytime ball hits “1” it is destroyed. everytime ball hits “2” , “2” changes animation frame and become “1” then in next collide it is destroyed.

i tried so many different variations but always “2” object changed frame without colliding. when scene starts it changes.
this is my main problem now.

if i cant generate box sticked together with random number text on it, so will i create all number (1-50) animations ?i mean for “2” box : 2-1 frame
“33” box: 33-32-31…-1,

ty for help^^

I think you’re making it more complicated than it needs to be by having object 1 and object 2. All you need is a single object (maybe called Block) which has within it 2 animation images (in this case image 1 for the block with a 1 on it and image 2 for the block with a 2 on it).

You could then have a single event to detect a collision, with two sub-events to check which number is on the block.

Block is in collision with top                                              |              [No actions]
Trigger once                                       
                  The number of the current animation of Block=1             |               Delete object Block
                  The number of the current animation of Block>1             |               Do -1 to the number of current animation of Block

If you set it up like this then you could have blocks that require 3 or 4 hits without needing any extra events, you just add more images to the Block object.

did you run it? because it didnt work . it changes animation without colliding in the beginning of the scene and after turning into from 2 to 1 , 1 doesnt collide with ball. i think we ll solve this with amination frames

EDIT: This message was saved as a draft some time ago, I can see there are some extra replies now, so maybe I’ll repeat some info :slight_smile:

Wait!, you don’t need such huge number of animation and frames, if you use sprites you have two options:
*50 animations with a single frame, this frame will have a box color and number. Now, when the ball collides a box, you have to do: Box.Animation = Box.Animation - 1, i.e. decrease the animation in 1, unless the animation is 0 in which case the box is deleted.
*1 animation with 50 frames, if you use frames you have to pause the animation!, yes, pause the animation of every box at the beginning of the scene and pause the animation of every new created box. Then on each collision do: Box.Frame = Box.Frame - 1, unless the current frame is 0 in wich case the box is deleted.

Both ways are pretty similar, and better explained by MattLB in a previous post.

Now, if you want to use “procedural” boxes, you have to:

  • Create the box sprite in a white/grey color, to colorize it:
  • Create a list of variables in a structure, to access them dynamically. I will call the structure “Colors” and will have a list of variables called 1, 2, 3, …, 50, each one will have a string value representing a color (for example “150;220;150”), remember that if you set a string variable in the GUI, the string hasn’t quotes.
    // A note here, there is a bug, if a variable string starts with a number, as “150;220;150”, GD for web (in native there is no problem) will recognize it as a number and take only the first number “150”, to avoid it for now I will add a “c” before the number, and then ignore it:
    *Then each Box needs a variable to track their current number, I’ve called it “N”, this number goes from 1 to 50, when this number is 0 you have to delete the box.
    *Now the Box can access its color through:
VariableString color = VariableString(Colors[Box.VariableString(N)])

But this string has the “c” at the beginning, to remove the first character:

SubStr(VariableString(color), 1, StrLength(VariableString(color)) - 1)

This string can be used to colorize the box.

*Almost finished, you have to create the Text object, place it in the center of the box, set its string = N, and link the Box and the Text objet.
*Now, when the ball collides a Box, you have to subtract 1 from the variable N of the Box, and update the color and text. If N reaches 0, delete the Box and the Text

As you can see, using animations or frames is a lot easier, anyway here is the example of the procedural way. Instead a ball collision, to hit a box you have to click on it: (3.03 KB)

It’s a pity the “global color” action can’t take an expression/variable, as that would make dynamically changing the colour of an object a lot easier.

It can, check my example, I use string manipulation + variable expressions to change the color dynamically :wink:
The problem is that each parameter can have one button only, and the color selector dialog was the logic answer instead the string expression editor. So you have to copy the expression from the expression editor and paste it in the color parameter.

i did it in an example as u said before use it large scale.
1 pic with 5 frames, every ball touch changes animation +1;
animation 0=number 1
animation 1=number 2
animation 2=number 3
animation 3=number 4
animation 4=number 5

1.ball is collision with ball , do +1 the current animation of box.
trigger once
the number of current animation of box =4 ;delete object box

cant destroy last animation. i paused it and tried to destroy but nothing happened.and it doesnt collide with ball in that example.simple easy condition missing but ı couldnt see:)

Please send me that example, so I can check it out :slight_smile:

ok i just added last frame an empty png image so when it reaches max , it destroyes png. :slight_smile:

I was thinking that the lack of the usual expression button in the Edit action window meant you could only select with the colour picker.
Something like: Change color of Box to Random(256);Random(256);Random(256) would have been nice, but if you can turn variables into strings and pass the strings to the ‘Global color’ action, then it can be done that way. Thanks! :smiley:

Exactly, you can pass this to the global color action to get a random color:

ToString(Random(255)) + ";" + ToString(Random(255)) + ";" + ToString(Random(255))

Remember that you have to concatenate (+) strings, so the “;” have to be strings too, and that each component of the RGB color is an integer from 0 to 255 :slight_smile:

i m triny to make clear random spawnings looks like there is an invisible grid on the background.

what i tried to do is 1 time spawn–> 1 or 2 time not spawn–>1time spawn… but it keeps spawning in same routine.can u help me about it?

by the way dont mind the project’s name.i played a little bit with +Y frame per sec and spawn time so one spawned on top of another. if i say destroy object ,2 of them destroyed and couldnt make them collide. anyways that was a wrong reasoning.