AS3 扩展圆形时如何创建多种颜色?
How to create multiple colors when expanding circle shape AS3?
所以我想弄清楚如何做到这一点,或者如果可能的话。我通过代码动态创建我的圆圈形状,并希望能够在 mouse_UP 时让圆圈停止,当 mouse_DOWN 被触发时让圆圈继续扩大但具有新的颜色。依此类推,每次都添加新颜色。我将代码设置到它在 mouse_DOWN 上扩展的位置,它改变了颜色,但它改变了整个圆圈的颜色,这不是我想要的。
这是我的代码:
//创建圆
//MC's
circle = new Shape();
circle.graphics.beginFill(0x990000, 1); // Fill the circle with the color 990000
circle.graphics.drawCircle((stage.stageWidth) / 2, (stage.stageHeight ) / 2, cirRadius); // Draw the circle, assigning it a x position, y position, raidius.
circle.graphics.endFill(); // End the filling of the circle
addChild(circle); // Add a child
//进入帧函数
private function logicHandler(e:Event):void
{
if (bDown) // Have New color Expand from prev point with new color
{
cirRadius ++;
circle.graphics.beginFill(randColor, 1); // Fill the circle with the color 990000
circle.graphics.drawCircle((stage.stageWidth) / 2, (stage.stageHeight ) / 2, cirRadius);
}
}
//鼠标监听器
private function onUp(e:MouseEvent):void
{
bUp = true;
bDown = false;
trace("onUp");
circle.graphics.endFill();
}
private function onDown(e:MouseEvent):void
{
bDown = true;
bUp = false;
trace("onDOWN");
randColor = Math.random() * 0xFFFFFF;
}
这就是我的目标:
这里需要一点逻辑。未经测试,但我认为它会起作用。
var aColor:uint;
var aRadius:int;
var aShape:Shape;
// Subscribe for MOUSE_DOWN event.
stage.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
function onDown(e:MouseEvent):void
{
// Set up the new color.
changeColor();
// Set up the new circle and assign its reference to the variable.
// The previous shape, if any, doesn't get destroyed,
// just no longer accessible via the same variable.
aShape = new Shape;
aShape.x = stage.stageWidth / 2;
aShape.y = stage.stageHeight / 2;
aShape.graphics.lineStyle(0, 0x000000);
// Put it UNDER all the other circles.
addChildAt(aShape, 0);
// Turn ENTER_FRAME handler on.
addEventListener(Event.ENTER_FRAME, onFrame);
// Prepare to intercept the MOUSE_UP event.
stage.addEventListener(MouseEvent.MOUSE_UP, onUp);
}
function onFrame(e:Event):void
{
// Increase the radius.
aRadius++;
// Draw the circle inside the last added shape.
aShape.graphics.clear();
aShape.graphics.beginFill(aColor);
aShape.graphics.drawCircle(0, 0, aRadius);
aShape.graphics.endFill();
}
function onUp(e:MouseEvent):void
{
// Turn ENTER_FRAME handler OFF.
removeEventListener(Event.ENTER_FRAME, onFrame);
// Stop intercepting the MOUSE_UP event.
stage.removeEventListener(MouseEvent.MOUSE_UP, onUp);
}
function changeColor():void
{
aColor = 0x1000000 * Math.random();
}
所以我想弄清楚如何做到这一点,或者如果可能的话。我通过代码动态创建我的圆圈形状,并希望能够在 mouse_UP 时让圆圈停止,当 mouse_DOWN 被触发时让圆圈继续扩大但具有新的颜色。依此类推,每次都添加新颜色。我将代码设置到它在 mouse_DOWN 上扩展的位置,它改变了颜色,但它改变了整个圆圈的颜色,这不是我想要的。
这是我的代码:
//创建圆
//MC's
circle = new Shape();
circle.graphics.beginFill(0x990000, 1); // Fill the circle with the color 990000
circle.graphics.drawCircle((stage.stageWidth) / 2, (stage.stageHeight ) / 2, cirRadius); // Draw the circle, assigning it a x position, y position, raidius.
circle.graphics.endFill(); // End the filling of the circle
addChild(circle); // Add a child
//进入帧函数
private function logicHandler(e:Event):void
{
if (bDown) // Have New color Expand from prev point with new color
{
cirRadius ++;
circle.graphics.beginFill(randColor, 1); // Fill the circle with the color 990000
circle.graphics.drawCircle((stage.stageWidth) / 2, (stage.stageHeight ) / 2, cirRadius);
}
}
//鼠标监听器
private function onUp(e:MouseEvent):void
{
bUp = true;
bDown = false;
trace("onUp");
circle.graphics.endFill();
}
private function onDown(e:MouseEvent):void
{
bDown = true;
bUp = false;
trace("onDOWN");
randColor = Math.random() * 0xFFFFFF;
}
这就是我的目标:
这里需要一点逻辑。未经测试,但我认为它会起作用。
var aColor:uint;
var aRadius:int;
var aShape:Shape;
// Subscribe for MOUSE_DOWN event.
stage.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
function onDown(e:MouseEvent):void
{
// Set up the new color.
changeColor();
// Set up the new circle and assign its reference to the variable.
// The previous shape, if any, doesn't get destroyed,
// just no longer accessible via the same variable.
aShape = new Shape;
aShape.x = stage.stageWidth / 2;
aShape.y = stage.stageHeight / 2;
aShape.graphics.lineStyle(0, 0x000000);
// Put it UNDER all the other circles.
addChildAt(aShape, 0);
// Turn ENTER_FRAME handler on.
addEventListener(Event.ENTER_FRAME, onFrame);
// Prepare to intercept the MOUSE_UP event.
stage.addEventListener(MouseEvent.MOUSE_UP, onUp);
}
function onFrame(e:Event):void
{
// Increase the radius.
aRadius++;
// Draw the circle inside the last added shape.
aShape.graphics.clear();
aShape.graphics.beginFill(aColor);
aShape.graphics.drawCircle(0, 0, aRadius);
aShape.graphics.endFill();
}
function onUp(e:MouseEvent):void
{
// Turn ENTER_FRAME handler OFF.
removeEventListener(Event.ENTER_FRAME, onFrame);
// Stop intercepting the MOUSE_UP event.
stage.removeEventListener(MouseEvent.MOUSE_UP, onUp);
}
function changeColor():void
{
aColor = 0x1000000 * Math.random();
}