开发者

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

开发者 https://www.devze.com 2022-12-19 22:43 出处:网络
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.

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

精彩评论

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