• Runtimes
  • [LibGDX] Doubts about changing colors, and body parts

Related Discussions
...

Hi again 🙂

I continue learning about Spine, but I am finding difficulties when applying the functionality in java.

the firts question is about the color.

I see when you select on the editor a attachment, you can change the color, I find this very usefull for implement hair color customs!

but no idea how make this work on code.

since some post are very old, and they talk about a setColor that actually i cant find. so I tried

   if(Gdx.input.justTouched()){
         Slot slot = skeleton.findSlot("main_hair");
         slot.getData().getColor().add(new Color(1,0,0,1)); //just try to color to a simple red hair         
skeleton.setSlotsToSetupPose(); skeleton.setBonesToSetupPose(); }

but dont work, and no idea what to do 🙁


the seccond question is about change a body part:

for this case I want to use goblins as an example.

at runtime if i implement for change the full skin:

public void handdleInput(){
   if(Gdx.input.justTouched()){
   if(isMaleGob ){
      isMaleGob  =false;
      skeleton.setSkin("goblingirl");

   }else{
      isMaleGob = true;
      skeleton.setSkin("goblin");
   }

this work perfect, but is here a simple way to change a single body part? for example I want switch de goblinGirl Head to the Male one.

public void handdleInput(){
   if(Gdx.input.justTouched()){
      
Skin skin = skeleton.getData().findSkin("goblin"); //pick info from the skin i want Slot slot = skeleton.findSlot("head"); int slotIndex = slot.getData().getIndex(); skeleton.setAttachment("head", skin.getAttachment(slotIndex,"head").getName()); skeleton.setSlotsToSetupPose(); skeleton.setBonesToSetupPose();

but get: java.lang.IllegalArgumentException: Attachment not found: goblin/head, for slot: head


Well I dont know if i go in the rigth way.... but atleas this work

public void handdleInput(){
   if(Gdx.input.justTouched()){
      Slot slot = skeleton.findSlot("head");
      RegionAttachment attachment = (RegionAttachment) slot.getAttachment();
      attachment.setRegion(atlas.findRegion("goblin/head")); //pick male head
      attachment.getColor().set(new Color(1,0,0,1));  //change color head
      attachment.updateOffset();
      skeleton.findSlot("head").setAttachment(attachment);
   }
}
quarzoq escribió

but dont work, and no idea what to do 🙁

The code to set the color you posted changes the slot data, also it uses add not set. You probably mean to change the slot:

slot.getColor().set(Color.RED);

Be wary if you apply an animation afterward that sets a different color. If so, either don't key the color in the animation or apply your animation, then set the color. You may need to do so every frame.

Alternatively you can set the color on the region attachment instead of the slot, as you do at the end of your post. Attachment color is not animated, so that won't have the problem with an animation changing your color. Note that the attachment is shared across all animation instances. If you don't want them to all have the same color, you'd need to copy the attachment and change each copy, or use the slot color.

The current attachment is stored on the slot. To set a different one, you can just set it on the slot directly. Again an animation may set a different attachment, you can solve that in the same was as the color, described above.

You could also change the attachment in the skin, as you found. This may be needed in some cases, such as when an animation keys some attachment changes and you want to change which attachments the animation will use


that is the main usefulness of using skins.

In your code at the end of your post you change the region for the attachment. That is fine as long as the new region is the same and you don't need the attachment in a different position. Also note the goblin head is animated with a deform timeline, so if you just change the region on the goblingirl head, that deform timeline won't affect the goblingirl head.

You could also change the attachment in the skin:

int slotIndex = skeletonData.findSlot("head").getIndex();
Attachment goblinHead = skeletonData.findSkin("goblin").getAttachment(slotIndex, "head");
skin.setAttachment(slotIndex, "head", goblinHead);
skeleton.setSkin(skin);

Nate,Thank you very much for your explanation, now it is clearer to me :ohyeah: