开发者

Why is the Sprite's graphics object not drawing to the screen?

When I debug my code nothing appears on screen. I've rechecked the code and consulted with others yet nothing appears. My html template is fine.

package {
    import flash.display.Sprite;
    import flash.events.*;


    public class asgnv2 extends Sprite
    { 
        var lineY = 0;
        public function asgnv2()
        {
            stage.addEventL开发者_如何学Cistener(Event.ENTER_FRAME, update);
            graphics.lineStyle(1);
        }
        function update(e){
            graphics.clear();
            graphics.moveTo(0 ,lineY);
            graphics.lineTo(100, lineY);
            lineY+=0.5;

            }

        }
    }


unless asgnv2 is Document class, it is not going to work, as you are registering ENTER_FRAME event on the stage inside the constructor of asgnv2. A DisplayObject can not access stage property until it is added to Stage Display List. So try the following.

public function asgnv2(){
  this.addEventListener(Event.ADDED_TO_STAGE, onAdded);
  graphics.lineStyle(1);
}

private function onAdded(e:Event):void { stage.addEventListener(Event.ENTER_FRAME, update); this.removeEventListener(Event.ADDED_TO_STAGE, onAdded); } private function update(e:Event):void{ //do the stuff }

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜