Count Objects With Same Color

Hi everyone

I am working on a simple click and color project on Wick Editor where i am able to color objects / movie clips using different colors which works fine. However, my objects or movie clips are in 5 groups marked A to E and would like to count how many objects or movie clips have the same color in each group and display the total through a textObject using setText.

For example,

redShapes
Group A = 2
Group B = 0
Group C = 1
Group D = 2
Group E = 0

Which i can then duplicate for other colors however, the max count of each color should be 5 per group or all groups combined as shown above.

I tried asking chatGPT but it gave me hundred of code variations with some counting colorObject colors and not those of movie clips and could not figure out how to do this and thought i ask the forum.

I would be super happy if someone could help as i am new to JS and in the process of learning.

Thank you and look forward to some responses.

Firstly, welcome to the forums.

While we are primarily here to help people with their Free Code Camp progress, we are open to people on other paths, too. Some of what you are asking is pretty trivial in the Free Code Camp context, so you might find that if you’re not getting the instruction and material you need in your current studies, the FCC curriculum will really help you get started. At a modest guess I’d say investing a 4-5 hours working through the curriculum here will really pay off. You can find the curriculum at https://www.freecodecamp.org/learn.

With your current questions, we don’t have enough context to know what you already know or don’t know, so it is impossible to guide you without just telling you the answer (which we won’t do).

It is pretty typical on here for people to share a codepen / repl.it / jsfiddle example of what they have tried so that anyone helping has more of an idea of what help is actually helpful.

Please provide some example of what you’ve tried and I’m sure you’ll get more help.

Happy coding :slight_smile:

Thanks for the feedback and info, i will certainly look through the Curriculum and see if there are any similar topics which i can learn from. However, i am a newbee in JS and coding in general but have some basic knowledge and understanding of how code works.

And as mentioned, i am working on Wick Editor trying to implement a colorCount function for different clips outputting the colorCount results on a textObject using setText (see code below).

This code almost works but the issue is that it also counts the color and textObjects on the page resulting in 3 on single colored clip simply because the textObject color is red and and the selected color dot is also red thus resulting in 3 instead of 1 and can not figure out how to correct it.


//FRAME DEFAULT CODE:

stop();

// mcArray for testing:
var mcArray = [mc1, mc2, mc3, mc4, mc5]; //

//Use these Arrays for Group Clips, make sure you change the Array name on each countColor function below. 

//var mcArrayA = [mc1, mc2, mc3, mc4, mc5]; //
//var mcArrayB = [mc6, mc7, mc8, mc9, mc10]; //
//var mcArrayC = [mc11, mc12, mc13, mc14, mc15]; //
//var mcArrayD = [mc16, mc17, mc18, mc19, mc20]; //
//var mcArrayE = [mc21, mc22, mc23, mc24, mc25]; //

// Optional colorObects - A
/*
var redColor = "rgb(255, 0, 0)"; // Red
var greenColor = "rgb(154, 205, 50)"; // YellowGreen
var blueColor = "rgb(30, 144, 255)"; // DodgerBlue
var orangeColor = "rgb(255, 165, 0)"; // Orange
var yellowColor = "rgb(255, 255, 0)"; // Yellow
var pinkColor = "rgb(255, 105, 180)"; // HotPink
var whiteColor = "rgb(255, 255, 255)"; // White
*/

// Optional colorObects - B

var redColor = "Red"; // Color name
var greenColor = "YellowGreen"; // Color name
var blueColor = "DodgerBlue"; // Color name
var orangeColor = "Orange"; // Color name
var yellowColor = "Yellow"; // Color name
var pinkColor = "HotPink"; // Color name
var whiteColor = "White"; // Color name

// Default colorObjects

colors = ["Red", "HotPink", "Yellow", "Orange", "DodgerBlue", "YellowGreen", "White"];
colorObjects = [];
currentColor = "White"; // Default color
id = "";
startX = pickColor.width;
startY = pickColor.width;

// Create the color choices (pick color UI)
for (var i = 0; i < colors.length; i++) {
    colorChoice = pickColor.clone();
    colorChoice.activeFrame._children[0].fillColor = colors[i];
    colorChoice.x = 124;
    colorChoice.y = 446;
    colorObjects.push(colorChoice);
    startX += pickColor.width;
    
    if ((i + 1) % 9 === 0) {
        startY += pickColor.width;
        startX = pickColor.width;
    }
}
project.activeFrame.addChild(highlight);

//ColorCounters (NB: Each color MUST have Groups A - E at the end !).
redCount = 0;
greenCount = 0;
blueCount = 0;
orangeCount = 0;
yellowCount = 0;
pinkCount = 0;

// countColor Function to count how many MovieClips have the same color in each Array ( make sure you duplicate this for each Array and that Array names are unique an based on Groups)
function countColor(color) {
    var count = 0;
    for (var i = 0; i < mcArray.length; i++) {
        if (mcArray[i].fillColor === color) {
            count++;
        }
    }
    return count;
}

//These count each of the color

var redCount = countColor(redColor); // Count clips with the color red
var greenCount = countColor(greenColor); // Count clips with the color red
var blueCount = countColor(blueColor); // Count clips with the color red
var orangeCount = countColor(orangeColor); // Count clips with the color red
var yellowCount = countColor(yellowColor); // Count clips with the color red
var pinkCount = countColor(pinkColor); // Count clips with the color red

// textObjects that display color counts using setText. For example, redShapes.setText("There are " + redCount + " MovieClips with the color " + redColor);

redShapes.setText(redCount);
greenShapes.setText(greenCount);
blueShapes.setText(blueCount);
orangeShapes.setText(orangeCount);
yellowShapes.setText(yellowCount);
pinkShapes.setText(pinkCount);

// HitTest Code (optional...!)
hitTestOptions({
    mode: 'RECTANGLE', // mode can be CONVEX, RECTANGLE, or CIRCLE
    offset: true,
    overlap: true,
    intersections: true
});


//FRAME KEYPRESSED CODE:

if(key==="1"){
   currentColor="HotPink";
      project.activeFrame.clips.forEach(clip => {
    if(clip.id==="shape"){
        highlight.activeFrame._children[0].fillColor='HotPink';
    }
});
}

if(key==="2"){
   currentColor="Yellow";
      project.activeFrame.clips.forEach(clip => {
    if(clip.id==="shape"){
        highlight.activeFrame._children[0].fillColor='Yellow';
    }
});
}

if(key==="3"){
   currentColor="Orange";
      project.activeFrame.clips.forEach(clip => {
    if(clip.id==="shape"){
        highlight.activeFrame._children[0].fillColor='Orange';
    }
});
}

if(key==="4"){
   currentColor="DodgerBlue";
      project.activeFrame.clips.forEach(clip => {
    if(clip.id==="shape"){
        highlight.activeFrame._children[0].fillColor='DodgerBlue';
    }
});
}

if(key==="5"){
   currentColor="YellowGreen";
      project.activeFrame.clips.forEach(clip => {
    if(clip.id==="shape"){
        highlight.activeFrame._children[0].fillColor='YellowGreen';
    }
});
}

if(key==="6"){
   currentColor="Red";
   project.activeFrame.clips.forEach(clip => {
    if(clip.id==="shape"){
        highlight.activeFrame._children[0].fillColor='Red';
    }
});
}

if(key==="7"){
   currentColor="White";
      project.activeFrame.clips.forEach(clip => {
    if(clip.id==="shape"){
        highlight.activeFrame._children[0].fillColor='White';
    }
});
}

if(key==="0"){
project.activeFrame.clips.forEach(clip => {
    if(clip.id==="shape"){
        clip.activeFrame._children[0].fillColor='White';
        
// Create 5 textObjects for each color, e.g redShapesA, B, C, D, E to match Group Arrays.    
        
redShapes.setText(0);
greenShapes.setText(0);
blueShapes.setText(0);
orangeShapes.setText(0);
yellowShapes.setText(0);
pinkShapes.setText(0);
    }
});
}

//CLIP DEFAULT CODE:

this.activeFrame._children[0].fillColor=currentColor;
this.id="shape";

var dragging = false;
onEvent('mousereleased', function () {
    dragging = false;
});

onEvent('mousepressed', function () {
    dragging = true;
});

onEvent('update', function () {
    if(dragging) {
        this.x = mouseX;
        //this.y = mouseY;
    }
});

//CLIP UPDATE CODE:

// "X" Coordinate limits...

this.highLimitX = project.width - this.width/1;
this.lowLimitX  = this.width/1;

// "Y" Coordinate limits...

this.highLimitY = project.height - this.height/1;
this.lowLimitY  = this.height/1;

// Limiting MC "X" limits...

if(this.x < this.lowLimitX)
{
    this.x = this.lowLimitX;
}
else if(this.x > this.highLimitX)
{
    this.x = this.highLimitX;
}

// Limiting MC "Y" limits...

/*if(this.y < this.lowLimitY)
{
    this.y = this.lowLimitY;
}
else if(this.y > this.highLimitY)
{
    this.y = this.highLimitY;
}*/


CLIP MOUSECLICK CODE:

this.activeFrame._children[0].fillColor=currentColor;

/*
redCount++;
greenCount++;
blueCount++;
orangeCount++;
yellowCount++;
pinkCount++;
*/

if (currentColor === "Red") {
            redShapes.setText(redCount);
        } else if (currentColor === "YellowGreen") {
            greenShapes.setText(greenCount);
        } else if (currentColor === "DodgerBlue") {
            blueShapes.setText(blueCount);
        } else if (currentColor === "Orange") {
            orangeShapes.setText(orangeCount);
        } else if (currentColor === "Yellow") {
            yellowShapes.setText(yellowCount);
        } else if (currentColor === "HotPink") {
            pinkShapes.setText(pinkCount);
        }

it’s impossible to debug without all the context