• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

variable datatype question

New Here ,
Aug 01, 2009 Aug 01, 2009

Copy link to clipboard

Copied

I want to use a variable to set the color of text and graphics input by the user. I use the following code to set the variable:

var setColor:String;
setColor = "0xff0000";

strokeColorBtn_mc.addEventListener(MouseEvent.MOUSE_UP, setStrokeColor);

function setStrokeColor(event:MouseEvent):void {
    setColor = "0xCCCCCC";
    trace ("the new color is " + setColor);
}

This works, and I am able to use the variable to set the color of type in a text field:


textFormat.color = setColor;

But when I try and use the variable to set the color of a graphic element:


designContainer.graphics.beginFill(setColor, 0.1);

I get the following error:


1067: Implicit coercion of a value of type String to and unrelated type uint.

So I thought that I might have to cast the String as a uint, so I tried the following:

designContainer.graphics.beginFill(setColor as uint, 0.1);

That eliminated the error message, but it does not change the color.

Once again, my inexperience leaves me stumped.

TOPICS
ActionScript

Views

1.3K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Aug 02, 2009 Aug 02, 2009

again, you can't change setColor and expect a color change to an object that's already been created.  you can expect that anything that uses setColor, after it's been changed, will reflect the new color.

you should expect newLine to be red because the value of setColor, when it's used to assign that lineStyle, is 0xff0000.

Votes

Translate

Translate
Community Expert ,
Aug 01, 2009 Aug 01, 2009

Copy link to clipboard

Copied

you could use:

uint(setColor);

but why not type your color variable as an uint?  that's what it is.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 01, 2009 Aug 01, 2009

Copy link to clipboard

Copied

I tried:

var setColor:uint;
setColor = 0xff0000;

It still works for the text, but not for the graphics ... although no error message

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 02, 2009 Aug 02, 2009

Copy link to clipboard

Copied

use:

var setColor:uint;
setColor = 0xff0000;

strokeColorBtn_mc.addEventListener(MouseEvent.MOUSE_UP, setStrokeColor);

function setStrokeColor(event:MouseEvent):void {
    setColor = 0xCCCCCC;
designContainer.graphics.beginFill(setColor, 0.1);

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 02, 2009 Aug 02, 2009

Copy link to clipboard

Copied

kglad, so my problem is really a scope problem? Changing the value of the variable with a function only works within the function? I thought that if I defined the variable outside of a function I could use it anywhere.

My objective is greater than my examples. I want a button (strokeColorBtn_mc) that changes setColor. Eventually, I will want to have a selection of colors from which to choose. Then I want to draw a line that uses the setColor variable. Draw a red line, change the color, draw a blue line, etc...

I didn't include the code newLine.graphics.lineStyle(6, setColor); because I figured that if I could use the variable in the fill, I could make it work for the line.

But evidently, my understanding of variable scope is faulty

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 02, 2009 Aug 02, 2009

Copy link to clipboard

Copied

there's no scope problem.  you might have a logic problem, though.

you can't change setColor and expect a color change to an object that's already been created.  you can expect that anything that uses setColor, after it's been changed, will reflect the new color.

if you think you're changing setColor and then creating something that uses setColor and fails to reflect the new color, post your code.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 02, 2009 Aug 02, 2009

Copy link to clipboard

Copied

After your previous response, I moved my variable declaration to the very beginning of my code, and setColor did in fact work for the fill color.

But, I have tried testing this a number of ways.

When the file loads now, setColor is established as red. The fill is 10% red as expected. I select the drawing button, and can draw a red line as expected. I select the type button and enter red text as expected.

However when the file loads as before and I select the strokeColorBtn_mc BEFORE selecting either the drawing or type buttons, the text changes color as I would expect, but the line does not.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 02, 2009 Aug 02, 2009

Copy link to clipboard

Copied

show the code that you think fails.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 02, 2009 Aug 02, 2009

Copy link to clipboard

Copied

//Working with the color

var setColor:uint;
setColor = 0xff0000;

strokeColorBtn_mc.addEventListener(MouseEvent.MOUSE_UP, setStrokeColor);

function setStrokeColor(event:MouseEvent):void {
    setColor = 0xCCCCCC;
    trace ("the new color is " + setColor);
}


// Setting the line parameters

var newLine:Shape = new Shape();

newLine.graphics.lineStyle(6, setColor);
designContainer.addChild(newLine);

function drawLine(event:MouseEvent):void {
    newLine.graphics.lineTo(event.localX, event.localY);
}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 02, 2009 Aug 02, 2009

Copy link to clipboard

Copied

again, you can't change setColor and expect a color change to an object that's already been created.  you can expect that anything that uses setColor, after it's been changed, will reflect the new color.

you should expect newLine to be red because the value of setColor, when it's used to assign that lineStyle, is 0xff0000.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 02, 2009 Aug 02, 2009

Copy link to clipboard

Copied

kglad, you are brilliant! I moved the line of code that defined lineStyle to within the function and it works! I just don't think this way yet.

Thanks again!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 02, 2009 Aug 02, 2009

Copy link to clipboard

Copied

LATEST

you're welcome.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 02, 2009 Aug 02, 2009

Copy link to clipboard

Copied

Are you expecting the fill to be the gray or red? Your function changes setColor to gray—at 10% alpha you might not be able to see that color. If you want red you need to change setColor again

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 02, 2009 Aug 02, 2009

Copy link to clipboard

Copied

Rob, the settting the fill to the original setColor is my control. I am also trying to draw a line using the same setColor variable. Although I can tell the 10% grey from 10% red, I have raised the alpha value just to make sure. My variable works for:

     textFormat.color = setColor;

but not for:

     designContainer.graphics.beginFill(setColor, 0.1);

or

     newLine.graphics.lineStyle(6, setColor);         

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines