haxeopenfl

How to get access to sprite 2D context?


Is it possible to get the CanvasRenderingContext2D at a sprite level instead of the whole canvas? Has anyone tried to have access for it? any idea?

I tried:

child.addEventListener(RenderEvent.RENDER_CANVAS, function(event)
        {
            var renderer:openfl.display.DisplayObjectRenderer = event.renderer;         
            var ctx:CanvasRenderingContext2D = renderer.context;
            ctx.shadowBlur = 20;
            ctx.shadowColor = "black";
        });

However, I am getting error:

openfl.display.DisplayObjectRenderer has no field context

How would I access the sprite’s CanvasRenderingContext2D ?


Solution

  • Try this casting the renderer to a CanvasRenderer:

    
    child.addEventListener(RenderEvent.RENDER_CANVAS, function(event)
            {
                var renderer:openfl.display.CanvasRenderer = cast(event.renderer);         
                var ctx:CanvasRenderingContext2D = renderer.context;
                ctx.shadowBlur = 20;
                ctx.shadowColor = "black";
            });