0
votes

I am new to coding and am having a bit of a hard time finding an answer to this.

Basically, I am trying to make a dress-up game in Flash CS5.5 with ActionScript 3. The current issue is that I am trying to make colors persistent through the different pieces of of clothing (ex: if you have a blue T-shirt, then change to dresses, the dress should be blue. If you change the color to red in the dress, and you change back to T-shirts, then the T-shirt should be red now.)

I am using assets I created in Photoshop, and I am putting all of the assets inside Movie Clip symbols (so all T-shirts are inside the Symbol T-shirt, all the dresses are inside a Symbol Dress, etc.); and on top of that I am putting all the clothes of the same color in the same keyframe number of their respective symbol (all red clothes are in keyframe 1, all blue clothes are in keyframe 2, etc).

What I am trying to do is to assign a number to a variable in the Symbol timeline whenever the player clicks on the color button, then have the variable read in the Main Timeline, store that variable in another variable in the Main Timeline so it persists even when the Symbol changes, and then have the other symbol read the Variable on the Main Timeline to apply to a function that tells it where to go.

My current problem is that I cannot for the life of me have the variable on the main timeline read the variable on the Symbol, and as such is not updating.

Here is the code:

//MAIN SCENE SCRIPT

//STOP EVERYTHING FROM STARTING
stop();

//-----------------------------------------------------------------------
//-----------------------------------------------------------------------

//DECLARE COLOR VARIABLE IN MAIN TIMELINE AND ASSIGN VALUE FROM SQUARE 
SYMBOL
var color = square.KeyColor;
//MOVE COLOR VARIABLE TO PERMANENT VARIABLE
var KeepColor:int = color;

// change color variable IF THE VALUE IS NOT 1
function ChangeColor () : void
{
  if(KeepColor != 1)
    {
      if (square.KeyColor != 1)
      {
        KeepColor = square.KeyColor;
        trace("color set to square value" )
      }

      else if (pentagon.KeyColor != 1)
      {
        KeepColor = pentagon.KeyColor;
        trace("color set to pentagon value")
      }

      else
      {
        trace ("there is an error")
        //break;
      }

    }
}

//-----------------------------------------------------------------------
//-----------------------------------------------------------------------

//CALL FUNCTION TO GO TO SQUARE SYMBOL AT THE CLICK OF BUTTON 1
btn1.addEventListener(MouseEvent.CLICK, printNumber);

//FUNCTION TO GO TO SQUARE SYMBOL
function printNumber(event:MouseEvent):void
{

    //GO TO FIRST KEYFRAME IN MAIN TIMELINE AND STOP
    gotoAndStop(1)
    //ASSIGN KEYCOLOR VALUE
    square.KeyColor = color;
    //PRINT COLOR CODE
    trace("color is  " + color);

}

//-----------------------------------------------------------------------
//-----------------------------------------------------------------------

//CALL FUNCTION TO GO TO PENTAGON SYMBOL AT THE CLICK OF BUTTON 2
btn2.addEventListener(MouseEvent.CLICK, printNumber2);

//FUNCTION TO GO TO PENTAGON SYMBOL
function printNumber2(event:MouseEvent):void
{

    //GO TO FIRST KEYFRAME IN MAIN TIMELINE AND STOP
    gotoAndStop(2)
    //PRINT COLOR CODE INSIDE OF THE PENTAGON SYMBOL
    trace("color is " + color);

}

//-----------------------------------------------------------------------
//-----------------------------------------------------------------------

//CHECK THAT THE COLOR CODE IN TH MAIN TIMELINE IS THE SAME AS THE COLOR 
FROM THE SQUARE 

//PRINT THE CODE AT THE CLICK OF THIS BUTTON
btnK.addEventListener(MouseEvent.CLICK, checkOtherKey);

//DECLARE FUNCTION TO PRINT KeepColor VARIABLE
function checkOtherKey(event:MouseEvent):void
{
trace("the current color is " + KeepColor);
}

//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------

//SQUARE SYMBOL SCRIPT

//STOP EVERYTHING FROM STARTING
stop();

//-----------------------------------------------------------------------
//-----------------------------------------------------------------------

//DECLARE KEYFRAME CODE FOR COLOR
var KeyColor:int = 1;

//-----------------------------------------------------------------------
//-----------------------------------------------------------------------

//CALL FUNCTION TO GO TO COLOR 1 AT THE CLICK OF BUTTON 1
btn1.addEventListener(MouseEvent.CLICK, Keynumber);

//DECLARE FUNCTION  FOR CHANGING KEYFRAME
function Keynumber(event:MouseEvent):void
{

    //GO TO FIRST KEYFRAME IN SQUARE TIMELINE AND STOP
    gotoAndStop(1);
    //SET COLOR CODE TO 1
    KeyColor = 1;
    //PRINT COLOR CODE OF FIRST COLOR
    trace("SQUARE" + KeyColor);
    //change root color number
    MovieClip(root).color = KeyColor;

}

//-----------------------------------------------------------------------
//-----------------------------------------------------------------------

//CALL FUNCTION TO GO TO COLOR 2 AT THE CLICK OF BUTTON 2
btn2.addEventListener(MouseEvent.CLICK, Keynumber2);

//DECLARE FUNCTION  FOR CHANGING KEYFRAME
function Keynumber2(event:MouseEvent):void
{
    //GO TO FIRST KEYFRAME IN SQUARE TIMELINE AND STOP
    gotoAndStop(2);
    //SET COLOR CODE TO 2
    KeyColor = 2;
    //PRINT COLOR CODE OF SECOND COLOR
    trace("SQUARE" + KeyColor);
    MovieClip(root).color = KeyColor;

}

//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------

 // PENTAGON SYMBOL SCRIPT


//-----------------------------------------------------------------------
//-----------------------------------------------------------------------

//STOP EVERYTHING FROM STARTING
stop()

//-----------------------------------------------------------------------
//-----------------------------------------------------------------------

//DECLARE KEYFRAME CODE FOR COLOR
var KeyColor:int;

//-----------------------------------------------------------------------
//-----------------------------------------------------------------------

//CALL FUNCTION TO GO TO COLOR 1 AT THE CLICK OF BUTTON 1
btn1.addEventListener(MouseEvent.CLICK, Keynumber);

//-----------------------------------------------------------------------
//-----------------------------------------------------------------------

//DECLARE FUNCTION  FOR CHANGING KEYFRAME
function Keynumber(event:MouseEvent):void
{
    //GO TO FIRST KEYFRAME IN PENTAGON TIMELINE AND STOP
    gotoAndStop(1);
    //SET COLOR CODE TO 1
    KeyColor = 1;
    //PRINT COLOR CODE OF FIRST COLOR
    trace ("PENTAGON" + KeyColor);

}

//-----------------------------------------------------------------------
//-----------------------------------------------------------------------

//CALL FUNCTION TO GO TO COLOR 2 AT THE CLICK OF BUTTON 2
btn2.addEventListener(MouseEvent.CLICK, Keynumber2);

//DECLARE FUNCTION  FOR CHANGING KEYFRAME
function Keynumber2(event:MouseEvent):void
{
    //GO TO SECOND KEYFRAME IN PENTAGON TIMELINE AND STOP
    gotoAndStop(2);
    //SET COLOR CODE TO 2
    KeyColor = 2;
    //PRINT COLOR CODE OF SECOND COLOR
    trace("PENTAGON" + KeyColor);

}

Thanks for the help!

1

1 Answers

0
votes

You can save the frame number of each type of cloth, update it always when changing the color, and use it when changing cloth, by using .currentFrame property of a Movie Clip.

//Reference to the current shirt or dress:
var shirt:MovieClip = basicShirt; //update this when you change shirt to dress etc

//Variables for colors (frames)
var shirtColor:int = 1;

//Changing shirt color:
shirtColorButton.addEventListener(MouseEvent.CLICK, changeShirtColor);

function changeShirtColor(evt:MouseEvent):void{
    //Other handy property for you might be .totalFrames:
    if(shirt.currentFrame < shirt.totalFrames){
        shirtColor++;
    }else{
        shirtColor = 1;
    }
    shirt.gotoAndStop(shirtColor);
}

So now if you change shirt to dress, you can simply remove shirt from stage, add dress to stage, and:

//function that creates the shirt:
shirt = basicDress;
shirt.gotoAndStop(shirtColor);

And if you really need the color names, you could use frame labels, or just store the color names into array as the order is always the same, like:

var colorNames:Array = ["Blue","Red","Green"];
...
trace("Shirt color is "+colorNames[shirtColor - 1]);