ActionScript 3多个实例,同名,问题

时间:2009-06-08 18:00:28

标签: flash actionscript-3 multiple-instances

我正在尝试创建一个网格,用户可以在其中“绘制”并将网格方块的颜色更改为所选颜色。

在这段代码中,我正在用正方形创建网格。我的功能“正常”,但它只适用于最后一个实例。

如何让它在所有正方形上工作,而不仅仅是最后一个?

感谢您提供任何帮助。

JD -

package {
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.text.TextField;


public class ClassBoxColor extends MovieClip {
    public var boxColor = "0xFFFFFF";
    public var lineColor = "0x666666";

    public function ClassBoxColor() {

        // ****Create the Grid****
        var xpos:Number;
        var xposStart:Number = 20;  // Initial Placement of grid along x axis
        var ypos:Number = 100;      // Initial Placement of grid along y axis
        var xNum:Number = 10;       // Size of Grid across in squares
        var yNum:Number = 10;       // Size of Grid across in squares

        for (var yaxis:Number = 1; yaxis <= yNum; yaxis++) {
            xpos = xposStart;
            for (var xaxis:Number = 1; xaxis <= xNum; xaxis++) {
                // Draw the square
                var colorBox:Sprite = new Sprite();
                colorBox.graphics.beginFill(boxColor, 1 );
                colorBox.graphics.lineStyle(1, lineColor);
                colorBox.graphics.drawRect(0,0,20,20);
                colorBox.x = xpos;
                colorBox.y = ypos;
                colorBox.buttonMode = true;
                addChild(colorBox);
                xpos += 20;
            }
            ypos += 20;
        }

        // LISTENERS

        Grey_btn.addEventListener(MouseEvent.CLICK, setGrey);           // This button instance is onstage
        DarkGrey_btn.addEventListener(MouseEvent.CLICK, setDarkGrey);   // This button instance is onstage

        stage.addEventListener(MouseEvent.MOUSE_DOWN, drawColor);
        stage.addEventListener(MouseEvent.MOUSE_UP, stopDrawColor);
        colorBox.addEventListener(MouseEvent.CLICK, changeColor);

        // FUNCTIONS & ACTIONS

        Grey_btn.buttonMode = true;
        DarkGrey_btn.buttonMode = true;

        CurrentBoxColor_txt.text = boxColor;// Display the currently selected color in the CurrentBoxColor_txt instance textfield that is onstage

        function setGrey(event:MouseEvent):void {
            boxColor = "0xCCCCCC";
            CurrentBoxColor_txt.text = boxColor;
        }
        function setDarkGrey(event:MouseEvent):void {
            boxColor = "0x666666";
            CurrentBoxColor_txt.text = boxColor;
        }
        function changeColor(event:MouseEvent):void {
            colorBox.graphics.clear();
            colorBox.graphics.lineStyle(1, lineColor);
            colorBox.graphics.beginFill(boxColor, 1);
            colorBox.graphics.drawRect(0,0,20,20);
            colorBox.graphics.endFill();
        }
        function drawColor(event:MouseEvent):void {
            //colorBox.addEventListener(MouseEvent.MOUSE_DOWN, changeColor);
            colorBox.addEventListener(MouseEvent.ROLL_OVER, changeColor);
        }
        function stopDrawColor(event:MouseEvent):void {
            //colorBox.removeEventListener(MouseEvent.MOUSE_DOWN, changeColor);
            colorBox.removeEventListener(MouseEvent.ROLL_OVER, changeColor);
        }
    }
}

}

4 个答案:

答案 0 :(得分:3)

不能说我曾经使用AS但是......你不应该在for中添加监听器吗?你在每次迭代时都覆盖colorBox,所以最后只有最后一个会被它引用(这就是我甚至会编译它甚至编译的地方,因为colorBox似乎可以在范围之外访问; C程序员在我身上哭了。

答案 1 :(得分:0)

我是AS3的新手,但看起来你的问题是你初始化你的方块。并在colorBox上调用changeColor()。你只有一个颜色盒,你可以玩,而不是你想要的10个(?)。以下不是最佳解决方案,但它最接近当前代码库的解决方案。

创建一个名为colorboxArray的数组,并将颜色框添加到其中。

var colorBoxArray:Array = new Array();
 for (var yaxis:Number = 1; yaxis <= yNum; yaxis++) {
         xpos = xposStart;
         for (var xaxis:Number = 1; xaxis <= xNum; xaxis++) {
         // Draw the square
          var colorBox:Sprite = new Sprite();
          colorBoxArray[yaxis] = colorBox;
          ..
          colorBoxArray[yaxis].addEventListener(MouseEvent.CLICK, changeColor);
          ..
 }

并在colorBoxArray上调用changeColor [IdOfTheBoxYouWantToChangeColorOn]

更好的方法是将所有上述函数移出到名为“Box”的类中,并在上面的创建循环中创建Box类的实例,为所有框和集合添加侦听器。我喜欢数组;(

答案 2 :(得分:0)

未测试但应该正常工作...使用MouseEvent :: buttonDown来查看按钮是否已关闭...

package {
    import flash.display.MovieClip;
    import flash.display.Sprite;
    import flash.events.MouseEvent;
    import flash.text.TextField;


    public class ClassBoxColor extends MovieClip {
        public var boxColor = "0xFFFFFF";
        public var lineColor = "0x666666";

        public function ClassBoxColor() {

            // ****Create the Grid****
            var xpos:Number;
            var xposStart:Number = 20;      // Initial Placement of grid along x axis
            var ypos:Number = 100;          // Initial Placement of grid along y axis
            var xNum:Number = 10;           // Size of Grid across in squares
            var yNum:Number = 10;           // Size of Grid across in squares

            for (var yaxis:Number = 1; yaxis <= yNum; yaxis++) {
                xpos = xposStart;
                for (var xaxis:Number = 1; xaxis <= xNum; xaxis++) {
                    // Draw the square
                    var colorBox:Sprite = new Sprite();
                    colorBox.graphics.beginFill(boxColor, 1 );
                    colorBox.graphics.lineStyle(1, lineColor);
                    colorBox.graphics.drawRect(0,0,20,20);
                    colorBox.x = xpos;
                    colorBox.y = ypos;
                    colorBox.buttonMode = true;
                    addChild(colorBox);
                    colorBox.addEventListener(MouseEvent.CLICK, changeColor);
                    colorBox.addEventListener(MouseEvent.ROLL_OVER, changeColor);
                    xpos += 20;
                }
                ypos += 20;
            }

            // LISTENERS

            Grey_btn.addEventListener(MouseEvent.CLICK, setGrey);                   // This button instance is onstage
            DarkGrey_btn.addEventListener(MouseEvent.CLICK, setDarkGrey);   // This button instance is onstage

            // FUNCTIONS & ACTIONS

            Grey_btn.buttonMode = true;
            DarkGrey_btn.buttonMode = true;

            CurrentBoxColor_txt.text = boxColor;// Display the currently selected color in the CurrentBoxColor_txt instance textfield that is onstage

            function setGrey(event:MouseEvent):void {
                boxColor = "0xCCCCCC";
                CurrentBoxColor_txt.text = boxColor;
            }
            function setDarkGrey(event:MouseEvent):void {
                boxColor = "0x666666";
                CurrentBoxColor_txt.text = boxColor;
            }
            function changeColor(event:MouseEvent):void {
                if ((event.type == MouseEvent.CLICK) || (event.buttonDown)) {
                    colorBox.graphics.clear();
                    colorBox.graphics.lineStyle(1, lineColor);
                    colorBox.graphics.beginFill(boxColor, 1);
                    colorBox.graphics.drawRect(0,0,20,20);
                    colorBox.graphics.endFill();
                }
            }
        }
    }
}

一般来说,我认为你的方法不是很干净......你的班级依赖于一些Grey_btn和DarkGrey_btn以及其他外部事物......这是糟糕的风格......真的......还有一些事情我不完全理解,但好吧......那可能是我的错......:)

祝你好运......;)

格尔茨

back2dos

答案 3 :(得分:0)

你真的需要对这整个课程进行返工。您已经在构造函数中直接定义了所有代码和方法,某些实例名称未定义,等等。我对如何编译它感兴趣。作为旁注,不要将Class放在AS类的名称中。

你需要拥有一个ColorBox类来处理非常简单的事情,比如翻转等,以便自己管理颜色。将框的创建/放置保留在单个ColorBox类之外。

这是在Flash Player 10中工作正常的同一个类的返工。我将事物分成两个类。你开始使用的一些名字/风格仍在使用中。我没有重写每一行。

ColorBox是一个盒子。而已。它只会管理颜色。

ColorBoxRoot是文档根类。将你的FLA设置为这个班级,让我们撕开。打开一个新的fla进行测试。我删除了按钮代码以及文本字段代码,但添加了文本字段的跟踪。

希望这有帮助!

// ColorBox.as

  

包{       import flash.display.MovieClip;       import flash.display.Sprite;       import flash.events。*;

[Event(name="colorChange")]
public class ColorBox extends MovieClip{
//  CONSTANTS
    public static const COLOR_CHANGE:String = "colorChange";
    public static const DEFAULT_WIDTH:uint = 20;
    public static const DEFAULT_HEIGHT:uint = 20;

//  PROPERTIES
    private var _boxColor:uint = 0xFFFFFF;
    public function get boxColor():uint{ return _boxColor; }

    private var _lineColor:uint = 0x666666;

//  CONSTRUCTOR
    public function ColorBox(){
        addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
    }

//  EVENT LISTENERS
    private function onAddedToStage(event:Event):void{
        stage.addEventListener(MouseEvent.MOUSE_DOWN, setGrey);
        stage.addEventListener(MouseEvent.MOUSE_UP, resetColors);

        updateDisplay();

        addEventListener(MouseEvent.ROLL_OVER, setGrey);
        addEventListener(MouseEvent.ROLL_OUT, setDarkGrey);
    }

//  PRIVATE METHODS
    private function resetColors(event:Event=null):void{
        _boxColor = 0xFFFFFF;
        updateDisplay();
    }

    private function setGrey(event:MouseEvent):void {
        _boxColor = 0xCCCCCC;
        updateDisplay();
        dispatchEvent(new Event(COLOR_CHANGE));
    }
    private function setDarkGrey(event:MouseEvent):void {
        _boxColor = 0x666666;
        updateDisplay();
        dispatchEvent(new Event(COLOR_CHANGE));
    }

    private function updateDisplay():void {
        graphics.clear();
        graphics.lineStyle(1, _lineColor);
        graphics.beginFill(_boxColor, 1);
        graphics.drawRect(0,0,20,20);
        graphics.endFill();
    }
}

}

// ColorBoxRoot.as

  

包{       import flash.events.Event;       import flash.display.MovieClip;

/**
 * Document root class; Create a new FLA (empty) and set this class as the document root
 */
public class ColorBoxRoot extends MovieClip{
//  STAGE OBJECTS
    //public var Grey_btn:DisplayObject;
    //public var DarkGrey_btn:DisplayObject;

//  CONSTRUCTOR
    public function ColorBoxRoot(){
        addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
    }

//  EVENT LISTENERS
    /**
     * Called once the swf stage is ready
     */
    private function onAddedToStage(event:Event):void{
        initializeUI();
        createGrid();
    }

//  PRIVATE METHODS
    /**
     * Always try to initialize your UI in a method so you can recall it later to "reset" things, if needed
     */
    private function initializeUI():void{
        //Grey_btn.buttonMode = true;
        //DarkGrey_btn.buttonMode = true;
    }

    /**
     * Creates the ColorBox grid
     */
    private function createGrid():void{
        var xpos:Number;
        var xposStart:Number = 20;      // Initial Placement of grid along x axis
        var ypos:Number = 100;          // Initial Placement of grid along y axis
        var xNum:Number = 10;           // Size of Grid across in squares
        var yNum:Number = 10;           // Size of Grid across in squares
        var colorBox:ColorBox;

        for (var yaxis:Number = 1; yaxis <= yNum; yaxis++) {
                xpos = xposStart;
                for (var xaxis:Number = 1; xaxis <= xNum; xaxis++){
                        // Draw the square
                        colorBox = new ColorBox();
                        colorBox.addEventListener(ColorBox.COLOR_CHANGE, onBoxColorChange);
                        colorBox.name = "box" + xaxis + "_" + yaxis; //jcbii: give it an identifiable name; just for fun
                        colorBox.x = xpos;
                        colorBox.y = ypos;
                        addChild(colorBox);
                        xpos += ColorBox.DEFAULT_HEIGHT; //jcbii: never hardcode these values; use dynamic values as much as possible
                }
                ypos += ColorBox.DEFAULT_WIDTH; //jcbii: never hardcode these values; use dynamic values as much as possible
        }
    }

    private function onBoxColorChange(event:Event):void{
        trace(event.target.name, ColorBox(event.target).boxColor);
    }
}

}