JuiceBoxx

Platformer Tutorial Code

Nov 29th, 2015
582
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. * PLAYSTATE.HX
  3. */
  4.  
  5. package;
  6.  
  7. import flixel.FlxG;
  8. import flixel.FlxSprite;
  9. import flixel.FlxState;
  10. import flixel.text.FlxText;
  11. import flixel.ui.FlxButton;
  12. import flixel.util.FlxMath;
  13. import flixel.util.FlxColor;
  14. import flixel.FlxCamera;
  15.  
  16. class PlayState extends FlxState
  17. {
  18.    
  19.     private var _level:TiledLevel;
  20.    
  21.     public var _player:Player;
  22.    
  23.     override public function create():Void
  24.     {
  25.         //Set the background color
  26.         FlxG.camera.bgColor = FlxColor.WHITE;
  27.        
  28.         //Load and add level elements
  29.         _level = new TiledLevel('assets/data/map.tmx');
  30.         add(_level.backgroundTiles);
  31.         add(_level.collisionTiles);
  32.        
  33.         _player = new Player(0, 0);
  34.         add(_player);
  35.        
  36.         FlxG.camera.follow(_player, FlxCamera.STYLE_PLATFORMER, 2);
  37.        
  38.         _level.loadObjects(this);
  39.        
  40.         super.create();
  41.     }
  42.  
  43.     override public function update():Void
  44.     {
  45.         super.update();
  46.        
  47.         FlxG.collide(_player, _level.collisionTiles);
  48.     }  
  49. }
  50.  
  51. /*
  52.  * MENUSTATE.HX
  53.  */
  54.  
  55. package;
  56.  
  57. import flixel.FlxG;
  58. import flixel.FlxSprite;
  59. import flixel.FlxState;
  60. import flixel.text.FlxText;
  61. import flixel.ui.FlxButton;
  62. import flixel.util.FlxMath;
  63. import flixel.util.FlxColor;
  64.  
  65. class MenuState extends FlxState
  66. {
  67.    
  68.     private var _enterText:FlxText;
  69.     private var _coin:FlxSprite;
  70.     private var _start:Bool = false;
  71.    
  72.     override public function create():Void
  73.     {
  74.         //On Load Things
  75.         FlxG.mouse.useSystemCursor = true;
  76.        
  77.         //Create Text
  78.         _enterText = new FlxText(0, 0, 256, 'Press Space', 32, false);
  79.        
  80.         _enterText.color = FlxColor.WHITE;
  81.        
  82.         _enterText.x = (FlxG.width / 2 - _enterText.width / 2);
  83.         _enterText.y = (FlxG.height / 2 - _enterText.height / 2);
  84.        
  85.         _enterText.alignment = "center";
  86.        
  87.         //Add Items
  88.        
  89.         add(_enterText);
  90.        
  91.         super.create();
  92.     }
  93.    
  94.     private function setStart():Void
  95.     {
  96.         _start = !_start;
  97.     }
  98.    
  99.     override public function destroy():Void
  100.     {
  101.         super.destroy();
  102.     }
  103.    
  104.     private function newState():Void
  105.     {
  106.         FlxG.switchState(new PlayState());
  107.     }
  108.  
  109.     override public function update():Void
  110.     {
  111.         super.update();
  112.        
  113.         //Switch State
  114.         if ((FlxG.keys.anyJustPressed(["SPACE"]))&&(_start == false))
  115.         {
  116.             newState();
  117.             _start = true;
  118.         }
  119.     }  
  120. }
  121.  
  122. /*
  123.  * PLAYER.HX
  124.  */
  125.  
  126. package;
  127.  
  128. import flixel.FlxBasic;
  129. import flixel.FlxObject;
  130. import flixel.FlxG;
  131. import flixel.FlxCamera;
  132. import flixel.FlxSprite;
  133.  
  134. class Player extends FlxSprite
  135. {
  136.     //Movement
  137.     public var _speed:Float = 275;
  138.     public var _jumpSpeed:Float = 1000;
  139.     public var _gravity:Float = 2500;
  140.    
  141.     //Input
  142.     private var _left:Bool;
  143.     private var _right:Bool;
  144.     private var _jump:Bool;
  145.  
  146.     public function new(X:Float, Y:Float)
  147.     {
  148.         super(X, Y);
  149.        
  150.         loadGraphic('assets/images/player.png', false, 64, 64);
  151.        
  152.         acceleration.y = _gravity;
  153.        
  154.        
  155.     }
  156.    
  157.     private function input():Void
  158.     {
  159.         _left = FlxG.keys.anyPressed(["LEFT", "A"]);
  160.         _right = FlxG.keys.anyPressed(["RIGHT", "D"]);
  161.         _jump = FlxG.keys.anyJustPressed(["SPACE"]);
  162.        
  163.         if (_left && _right)
  164.             _left = _right = false;
  165.     }
  166.    
  167.     private function movement():Void
  168.     {
  169.         velocity.x = 0;
  170.        
  171.         if (_left)
  172.             velocity.x = -_speed;
  173.         if (_right)
  174.             velocity.x = _speed;
  175.            
  176.         if ((isTouching(FlxObject.DOWN)) && (_jump))
  177.             velocity.y -= _jumpSpeed;
  178.     }
  179.    
  180.     override public function update()
  181.     {
  182.         movement();
  183.        
  184.         super.update();
  185.        
  186.         input();
  187.     }
  188.    
  189.    
  190. }
  191.  
  192. /*
  193.  * TILEDLEVEL.HX
  194.  */
  195.  
  196. package;
  197.  
  198. import openfl.Assets;
  199. import haxe.io.Path;
  200. import haxe.xml.Parser;
  201. import flixel.FlxG;
  202. import flixel.FlxObject;
  203. import flixel.FlxSprite;
  204. import flixel.group.FlxGroup;
  205. import flixel.tile.FlxTilemap;
  206. import flixel.addons.editors.tiled.TiledMap;
  207. import flixel.addons.editors.tiled.TiledObject;
  208. import flixel.addons.editors.tiled.TiledObjectGroup;
  209. import flixel.addons.editors.tiled.TiledTileSet;
  210.  
  211. class TiledLevel extends TiledMap
  212. {
  213.     private inline static var c_PATH_LEVEL_TILESHEETS = "assets/data/maps/";
  214.    
  215.     // Array of tilemaps used for collision
  216.     public var foregroundTiles:FlxGroup;
  217.     public var backgroundTiles:FlxGroup;
  218.     public var collisionTiles:FlxGroup;
  219.     private var collidableTileLayers:Array<FlxTilemap>;
  220.    
  221.     public function new(tiledLevel:Dynamic)
  222.     {
  223.         super(tiledLevel);
  224.        
  225.         foregroundTiles = new FlxGroup();
  226.         backgroundTiles = new FlxGroup();
  227.         collisionTiles = new FlxGroup();
  228.        
  229.         collisionTiles.visible = false;
  230.        
  231.         FlxG.worldBounds.set(0, 0, fullWidth, fullHeight);
  232.        
  233.         // Load Tile Maps
  234.         for (tileLayer in layers)
  235.         {
  236.             var tileSheetName:String = tileLayer.properties.get("tileset");
  237.             var tileLayerName:String = tileLayer.properties.get("layer");
  238.            
  239.             //Errors
  240.             if (tileSheetName == null)
  241.                 throw "'tileset' property not defined for the '" + tileLayer.name + "' layer. Please add the property to the layer.";
  242.            
  243.             if (tileLayerName == null)
  244.                 throw "'layer' property not defined for the '" + tileLayer.name + "' layer. Please add the property to the layer.";
  245.            
  246.            
  247.             var tileSet:TiledTileSet = null;
  248.             for (ts in tilesets)
  249.             {
  250.                 if (ts.name == tileSheetName)
  251.                 {
  252.                     tileSet = ts;
  253.                     break;
  254.                 }
  255.             }
  256.            
  257.             if (tileSet == null)
  258.                 throw "Tileset '" + tileSheetName + " not found. Did you mispell the 'tilesheet' property in " + tileLayer.name + "' layer?";
  259.                
  260.             var imagePath       = new Path(tileSet.imageSource);
  261.             var processedPath   = c_PATH_LEVEL_TILESHEETS + imagePath.file + "." + imagePath.ext;
  262.            
  263.             var tilemap:FlxTilemap = new FlxTilemap();
  264.             tilemap.widthInTiles = width;
  265.             tilemap.heightInTiles = height;
  266.             tilemap.loadMap(tileLayer.tileArray, processedPath, tileSet.tileWidth, tileSet.tileHeight, 0, tileSet.firstGID, 1, 1);
  267.            
  268.             if (tileLayerName == 'background')
  269.             {  
  270.                 if (collidableTileLayers == null)
  271.                     collidableTileLayers = new Array<FlxTilemap>();
  272.                
  273.                 collisionTiles.add(tilemap);
  274.                
  275.                 collidableTileLayers.push(tilemap);
  276.                
  277.                 backgroundTiles.add(tilemap);
  278.             }
  279.         }
  280.     }
  281.    
  282.     public function loadObjects(state:PlayState)
  283.     {
  284.         for (group in objectGroups)
  285.         {
  286.             for (o in group.objects)
  287.             {
  288.                 loadObject(o, group, state);
  289.             }
  290.         }
  291.     }
  292.    
  293.     private function loadObject(o:TiledObject, g:TiledObjectGroup, state:PlayState)
  294.     {
  295.         var x:Int = o.x;
  296.         var y:Int = o.y;
  297.        
  298.         // objects in tiled are aligned bottom-left (top-left in flixel)
  299.         if (o.gid != -1)
  300.             y -= g.map.getGidOwner(o.gid).tileHeight;
  301.        
  302.         switch (o.type.toLowerCase())
  303.         {
  304.             case 'player':
  305.                 state._player.x = x;
  306.                 state._player.y = y;
  307.         }
  308.     }
  309.    
  310.     public function collideWithLevel(obj:FlxObject, ?notifyCallback:FlxObject->FlxObject->Void, ?processCallback:FlxObject->FlxObject->Bool):Bool
  311.     {
  312.         if (collidableTileLayers != null)
  313.         {
  314.             for (map in collidableTileLayers)
  315.             {
  316.                 // IMPORTANT: Always collide the map with objects, not the other way around.
  317.                 //            This prevents odd collision errors (collision separation code off by 1 px).
  318.                 return FlxG.overlap(map, obj, notifyCallback, processCallback != null ? processCallback : FlxObject.separate);
  319.             }
  320.         }
  321.         return false;
  322.     }
  323. }
RAW Paste Data