Tag Archives: flixel

Importare mappe di Ogmo Editor

29 Mar

Ed ecco un bello script per caricare le tilemap realizzate con Ogmo Editor.
source | pastebin.com/yStffX3z

    package com.tileisle

    {
            import flash.utils.ByteArray;
            import org.flixel.FlxG;
            import org.flixel.FlxTilemap;
            import org.flixel.FlxU;

            public class FlxTilemapExt extends FlxTilemap
            {

                    public function loadOgmoTileMap(Level:XML, MapLayer:String, TileGraphics:Class):FlxTilemapExt
                    {
                            _pixels = FlxG.addBitmap(TileGraphics);

                            var xml:XML = Level;
                            //FlxG.log( Level);
                            _tileWidth = xml.descendants(MapLayer).@tileWidth;
                            _tileHeight = xml.descendants(MapLayer).@tileHeight;

                            widthInTiles = xml.width / _tileWidth;
                            heightInTiles = xml.height / _tileHeight;

                            totalTiles = widthInTiles * heightInTiles;

                            width = xml.width;
                            height = xml.height;

                            _block.width = _tileWidth;
                            _block.height = _tileHeight;

                            // Quad tree tweaks
                            FlxU.setWorldBounds(0, 0, width, height, 3);

                            // Initialize the data
                            _data = new Array();
                            for(var di:int; di < totalTiles; di++)                                     _data.push(0);                             totalTiles = widthInTiles * heightInTiles;                             _rects = new Array(totalTiles);                             // Set tiles                             var i:XML;                             for each(i in xml.descendants(MapLayer)[0].tile)                                     this.setTile((i.@x / _tileWidth), (i.@y / _tileHeight), i.@id);                             // Update screen vars                             _screenRows = Math.ceil(FlxG.height / _tileHeight) + 1;                             if(_screenRows > heightInTiles) _screenRows = heightInTiles;

                            _screenCols = Math.ceil(FlxG.width / _tileWidth) + 1;
                            if(_screenCols > widthInTiles) _screenCols = widthInTiles;

                            // Refresh collision data
                            refreshHulls();

                            return this;
                    }

                    public function loadOgmoCollision(Level:String, TileGraphics:Class):FlxTilemapExt
                    {
                            //var file:ByteArray = new Level;
                            //var str:String = file.readUTFBytes(file.length);
                            //var xml:XML = new XML(file);

                            // Figure out the map dimensions
                            _tileWidth = 32;
                            _tileHeight = 32;

                            // Convert Ogmo data to CSV
                            var mapData:String = Level;
                            var rows:Array = mapData.split("\n");

                            var out:String = "";

                            for(var i:uint = 0; i < rows.length; i++)
                            {
                                    var l:uint = rows[i].length;
                                    var c:uint = 0;

                                    for(var j:uint = 0; j < l; j++)
                                    {
                                            out += rows[i].charAt(j);
                                            c++;
                                            if(c < l)
                                                    out += ",";
                                    }

                                    out += "\n";
                            }

                            // ... and do it the classic way 🙂
                            this.loadMap(out, TileGraphics, _tileWidth, _tileHeight);

                            return this;
                    }
            }
    }

Annunci

Introduzione

28 Mar

Questo blog mi serve solo a mantenere traccia del codice  che scrivo ed eventualmente deve fare da archivio alle soluzioni che trovo ai problemi in cui incapperò, per il resto non credo servirà a nessuno. Se tante volte qualcuno dovesse trovare utile ciò che scrivo, buon per lui.

y609