Skip to main content
April 30, 2013
Question

How to go about optimising this class?

  • April 30, 2013
  • 1 reply
  • 383 views

I'm rather new to actionscript, but I'm figuring it out as I go.

I am making a game. One of the primary features of this game is darkness. I chose to use tiles to represent areas outside the players sight range, this works great on a small scale. The problem is that once there's lots of darkness tiles it gets a bit laggy.

This is the class for light sources, currently its a proof of concept.. I plan on having in betweens later so its not just a true or false.

package

{

    import flash.display.MovieClip;

    import flash.events.Event;   

    public class LightEmitter extends MovieClip

    {

        private var i:int = 0;

        private var closestH = 0;

        private var lightResistance = 0;

        private var brightness = 1;

        private var targetTile;

        private var baseSightLevel = 400;

        public function LightEmitter()

        {

            this.parent.setChildIndex(this, 0)

            stage.addEventListener(Event.ENTER_FRAME, onJoinFrame);

        }

        public function onJoinFrame(e:Event)

        {   

            //Hide darkness

            for (i = 0; i < MainDocument.darkTiles.length; i ++)

              {

                targetTile = MainDocument.darkTiles[0]

                lightResistance = MainDocument.darkTiles[1];

                   if (Math.sqrt(Math.pow(targetTile.x-this.x, 2) + Math.pow(targetTile.y-this.y, 2)) < (baseSightLevel*brightness)/lightResistance)

                {

                    targetTile.alpha = 0;

                }

                else

                {

                    targetTile.alpha = 0.6;

                }

            }

            this.x = parent.getChildByName("player").x

            this.y = parent.getChildByName("player").y

        }

    }

}

This is the class for a tile of darkness.

package

{

    import flash.display.MovieClip;

    public class Fog extends MovieClip

    {

        var aDarkTile: Array = new Array();

        public function Fog()

        {

            aDarkTile[0] = this;

            aDarkTile[1] = 1;

            MainDocument.darkTiles[MainDocument.darkTiles.length] = aDarkTile;

        }

    }

}

I need to load lots of tiles at a time in addition to any other elements of the game. (but I can unload, or at least hide, tiles that are off screen)

Any suggestions/tips/alternatives would be greatly appreciated.

This topic has been closed for replies.

1 reply

kglad
Community Expert
Community Expert
May 1, 2013

enable the cacheAsBitmap property of your tiles and remove tiles that are no longer on stage.

May 1, 2013

thanks, this helps, but its still causes too much lag so Im going to look for another way.