【问题标题】:AS3: How do i draw shapes in my own classAS3:我如何在自己的班级中绘制形状
【发布时间】:2013-08-23 04:44:56
【问题描述】:

我是 AS3 的新手。我想创建一个我在自己的构造函数类中定义的形状。

它应该在创建类时创建一个形状。 (构造函数)

我在下面的代码中评论了我的需求:

ballShape 类

public class ballShape {

        public function ballShape() {
            // define shape properties.
            // create shape and put that in x = 0, y = 0 
        }

    }

任何帮助都会很棒。

【问题讨论】:

    标签: actionscript-3 flash oop


    【解决方案1】:

    您可以在将类扩展到 Shape 或 Sprite 时轻松做到这一点

    这是你的代码

    public class ballShape extends Sprite {
    
        public function ballShape() {
            // define shape properties. The graphics object is already added to your Sprite, no need to manually addChild() this object.
            graphics.beginFill(color, alpha); // you can begin a fill with this method, there are also methods to start a bitmap fill, gradient fill. 
            graphics.drawRect( x, y, width, height ); // draw a shape
            graphics.endFill();
        }
    
    }
    

    虽然 Shape 可以具有绘制形状和线条的相同功能,但我选择了 Sprite,因为:

    • 您将具有交互性并能够从该类调度事件
    • 您将拥有一组 Sprite 具有的有用属性。

    有关 Graphics 类的更多信息,请参阅http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/Graphics.html

    【讨论】:

    • 感谢您的解释。我正在尝试使用您的方法创建一个形状,您可以在here 的第三方站点中查看我的代码。从ballShape 创建实例后,它会正确地跟踪某些内容。但运行后不显示任何形状。
    • 当您创建 ballShape 对象 (var ball:ballShape = new ballShape() ) 时,您需要将该对象添加到显示列表 ( addChild(ball) );否则 ballShape 会创建一个形状,但 ballShape 本身不会添加到舞台中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-17
    相关资源
    最近更新 更多