Skip to main content
Manic-Mars
Known Participant
May 24, 2018
Answered

Color picker in game, used for multiple objects?

  • May 24, 2018
  • 1 reply
  • 1287 views

Hi! I've been struggling all morning on this since there appears to be no easy answer. I'm building a character creator, and I want the user to be able to chose colors of skin tone and clothes color, etc. A search reveals the "easiest" way is to add a color picker. But as I want multiple objects to be changeable, I'm running into trouble.... Plus, I'm not a fan of the drag and drop method, way to messy... I already have a substitute method arranged that allows all customizations to be button selected.

Here is where my problem occurs... The code I found to create the color picker assumes all active movie clips are on the same frame, and possibly layer to the picker. Here is the tutorial I've found: Flash Actionscript 3 Tutorial by pichu90 on DeviantArt

I've adapted their code, and every time, at every arrangement I get errors... My current for is rather simple, from one & two unrelated loader code, frame 3 Button code for selecting body type as follows:

var myGenderArray = [male, female, none];

for each (var Gender in myGenderArray) {

  Gender.addEventListener(MouseEvent.CLICK, onGenderClick);

}

function onGenderClick (event:MouseEvent):void {

  MovieClip(parent).body1.gotoAndStop(event.target.name);

}

The listed values, male, female, none, all comply to a frame table on a movie clip where in the color picker code resides, as well as on buttons that can be clicked to activate those frames. 

On each frame an additional movie clip that is to be the color changing tone sits in line with my line art. They each have an instance name to be called upon.

Here is the code that is my main problem:

import fl.controls.ColorPicker;

import fl.events.ColorPickerEvent;

import flash.geom.ColorTransform;

var SkinFemalecolorInfo:ColorTransform = MovieClip().SkinFemale.transform.colorTransform;

MovieClip().CPer1.addEventListener(ColorPickerEvent.CHANGE, colorChange2);

function colorChange2(event:ColorPickerEvent):void {

MovieClip().SkinFemale.transform.colorTransform = MovieClip().CPer1.selectedColor;

}

This is the code for the female frame, on the male and none frames the "SkinFemale" is SkinMale or SkinNone. As well, CPer1 is the representative of the unique color picker, and there is also CPer2 and CPer3. Finally, functions have unique names, I believe, so the other two are called colorChange1 and colorChange3. You'll notice both SkinFemale & CPer1 have a MovieClip(). infant of them. This is because otherwise they will be undefined by flash as this code is within a movie clip its self. (or some logic to that effect, in my experience this is true please do offer alternate answer I've been looking)

I've messed with this code so much I don't know if it can even be saved.... but I'd greatly appreciate anyone who could solve my mystery.

This topic has been closed for replies.
Correct answer kglad

I found another place where I had forgotten to remove MovieClip() from CPer1. My current error is :

error 1067: Implicit coercion of a value of type flash.geom.ColorTransform.


click file>publish settings>swf and tick 'permit debugging'. retest and the problematic line of code will be referenced in the error message.

what line is triggering the problem?

1 reply

kglad
Community Expert
Community Expert
May 24, 2018

all your code that contains MovieClip() is going to trigger an error.

if you have a movieclip that's not recognized by animate as being a movieclip (ie, you're seeing something like: error, implicit coercion of type whatever to type MovieClip), you can explicitly cast it.  eg,

MovieClip(parent)

MovieClip(root)

or maybe MovieClip(SkinFemale)

if you don't understand any of that, just remove MovieClip() from every place you used it.

Colin Holgate
Inspiring
May 24, 2018

Also remove the leading period. ".SkinFemale.transform.colorTransform" would cause a problem.

Here's the function I usually use for tinting movieclips:

  private function setColor(mc:MovieClip,color:uint){

     var obj_color:ColorTransform = new ColorTransform();

     obj_color.color = color;

     mc.transform.colorTransform = obj_color;

  }

I would have swatches somewhere with the RGB value as a uint (unsigned integer), and call that function with the movieclip I want to tint, and the color to tint to.

Manic-Mars
Known Participant
May 25, 2018

Well... the idea of kglad's of putting the movie clip name in the brackets removed all loading errors to make it playable, however... when I loaded the game and clicked the display character button to reveal the frame where the color picker code is, I got a bit of a glitch... its rather hard to describe so I recorded it:

(mild flashing warning)

I am genuinely unsure what steps to take next, I want to be able to change the color completely, If anyone has a complete alternate code and instructions link I could sure use it... Otherwise I'll keep working around this code...