0

我正在尝试使用 Flex 库和 FlashDevelop IDE 在 ActionScript 3 中编写程序。该程序需要在舞台上绘制五个空白方块,单击其中任何一个方块将独立呈现一个独特的图案(由几何图元组成),以代替该方块。再次单击这些图案会将它们切换回空白方块。

我决定将这种行为抽象为一个用户定义的类,它是 的扩展Sprite,称为PatternTile. 但是,我收到一个程序错误,PatternTile尽管已将其添加到舞台(由事件处理程序确认),但它本身不可见。默认情况下,对象应在中央舞台渲染,并且根据参数,矩形周围至少应有黑色轮廓。单击时,对象应该通过清除和重新指定图形属性来重绘自己。

构造函数有一个参数可以切换这些属性。现在它们都是等价的。

下面是PatternTile类的代码:

package  
{
    /**
     * ...
     * @author ...
     */

    import flash.display.Shape;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.display.Stage;

    public class PatternTile extends Sprite
    {
        // OBJECTIVE: Generalize a tile object that can be drawn several times independently and change pattern when clicked.

        // DECLARE+INSTANCE VARS

        private var tileFlip : Boolean = new Boolean( true ) ; // tileFlip - switch for tracking state of shape when clicked on

        // PatternTile, for the sake of simplicity, will include all its potential shapes in its constructor.
        private var patternCode : int = new int(  ); // Stores pattern classification to switch what the pattern for this tile is.

        // CONSTRUCTOR
        public function PatternTile( xBase:int, yBase:int, xTrans:int , yTrans:int , patternType:int ):void
        {

            /* ARGUMENTS:
             * xBase - reference point for translations on the X axis.
             * yBase - reference point for translations on the Y axis.
             * xTrans:int - integer representing displacement of pixels from center on X axis. 
             * yTrans:int - integer representing displacement of pixels from center on Y axis. 
             * patternType:int - sets code for pattern
             */

            patternCode = patternType

            x = xBase + xTrans ; // x - new X position

            y = yBase + yTrans ; // y - new Y position

            // Assign Parametres to blankTile
            this.graphics.lineStyle( 5, 0x000000, 1)
            this.graphics.beginFill( 0xFFFFFF, 1 );
            this.graphics.drawRect( x , y , 200 , 200 );
            this.graphics.endFill(  );

            // EVENT LISTENERS - Clicking on the Sprite
            addEventListener( MouseEvent.CLICK , mouseClickHandler ); // Will this generalize to handling clicks on both pattern types (tile, altObject?); 
            addEventListener( Event.ADDED_TO_STAGE, addedToStage ); // Created event handler for debugging purposes.
        }

        // Mouse Click Function
        public function mouseClickHandler(event:Event):void
        {
            if (tileFlip) {

                this.graphics.clear(); // clear previous properties

                switch (patternCode) {
                // Because I know there are only five cases, I can include all of them within the object.
                    case 0: // Pattern 0

                        // Colour Properties
                        this.graphics.lineStyle( 1, 0x000000, 1 );
                        this.graphics.beginFill(0xFFFFFF, 1);
                        // Draw shape primitives here
                        this.graphics.drawCircle( x, y, 50 ); // PLACEHOLDER
                        this.graphics.endFill(  );
                        // trace("you are here")

                        break;

                    case 1: // Pattern 1

                        // Colour Properties
                        this.graphics.lineStyle( 1, 0x000000, 1 );
                        this.graphics.beginFill(0xFFFFFF, 1);
                        // Draw shape primitives here
                        this.graphics.drawCircle( x, y, 50 ); // PLACEHOLDER
                        this.graphics.endFill(  );

                        break;

                    case 2: // Pattern 2

                        // Colour Properties
                        this.graphics.lineStyle( 1, 0x000000, 1 );
                        this.graphics.beginFill(0xFFFFFF, 1);
                        // Draw shape primitives here
                        this.graphics.drawCircle( x, y, 50 ); // PLACEHOLDER
                        this.graphics.endFill(  );

                        break;

                    case 3: // Pattern 3

                        // Colour Properties
                        this.graphics.lineStyle( 1, 0x000000, 1 );
                        this.graphics.beginFill(0xFFFFFF, 1);
                        // Draw shape primitives here
                        this.graphics.drawCircle( x, y, 50 ); // PLACEHOLDER
                        this.graphics.endFill(  );

                        break;

                    case 4: // Pattern 4

                        // Colour Properties
                        this.graphics.lineStyle( 1, 0x000000, 1 );
                        this.graphics.beginFill(0xFFFFFF, 1);
                        // Draw shape primitives here
                        this.graphics.drawCircle( x, y, 50 ); // PLACEHOLDER
                        this.graphics.endFill(  );

                        break;

                    }

                tileFlip = false; // prime alternate state to be checked on next click

            } else {

                this.graphics.clear(); // clear previous properties         

                // Blank Tile Properties
                this.graphics.lineStyle( 1, 0x000000, 1 );
                this.graphics.beginFill(0xFFFFFF, 1);
                this.graphics.drawRect( x , y , 100 , 100 );
                this.graphics.endFill(  );

                tileFlip = true; // prime alternate state to be checked on next click
            }
        }

        public function addedToStage(e:Event):void 
        {
             trace("Added PatternTile to stage");           
        }

    }
}

下面是Main课程:

package 
{
    import flash.display.Shape;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;

    // User-Defined Classes
    import PatternTile

    /**
     * ...
     * @author kbruskie
     */
    public class Main extends Sprite 
    {
        // Center Stage, called throughout program.
        private var xCenter : int = new int( stage.stageWidth / 2 );
        private var yCenter : int = new int( stage.stageHeight / 2 );

        // Tiles
        private var tile1 : PatternTile ;
        /*      
        private var tile2 : PatternTile = new PatternTile( 0, 0, 0 );
        private var tile3 : PatternTile = new PatternTile( 0, 0, 0 );   
        private var tile4 : PatternTile = new PatternTile( 0, 0, 0 );   
        private var tile5 : PatternTile = new PatternTile( 0, 0, 0 );
        */

        public function Main():void 
        {
            trace(xCenter) // prints center of stage on X axis - working
            trace(yCenter) // prints center of stage on Y axis - working

            tile1 = new PatternTile( xCenter, yCenter, 200, -200, 1 ) // draws single clickable tile in the middle of the stage. Not yet added to stage.
            // instances the object but it isn't visibly drawn. What's going on?

            stage.addChild(tile1) // Finally - all the properties are set, and this object is ready for showbiz. Add to stage.
        }
    }
}

我不明白发生了什么,我对我的理论没有信心。任何人都可以帮忙吗?

4

1 回答 1

0

我假设您的对象在舞台上,但在可见区域之外......在 PatternTile 构造函数中,您正在设置位置,例如:

x = xBase + xTrans;

然后你从 x 开始画一个正方形:

this.graphics.drawRect( x , y , 200 , 200 );

所以正方形被有效地绘制在位置 x+x 处。不应该是这个吗?

this.graphics.drawRect( 0 , 0 , 200 , 200 );
于 2013-01-18T23:38:54.047 回答