Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How to go about optimising this class?

Guest
Apr 30, 2013 Apr 30, 2013

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.

TOPICS
ActionScript
359
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 30, 2013 Apr 30, 2013

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Apr 30, 2013 Apr 30, 2013
LATEST

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines