Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@player-03
Last active June 23, 2021 05:43
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save player-03/f28d41840d71f993f94c833be45c2875 to your computer and use it in GitHub Desktop.
Save player-03/f28d41840d71f993f94c833be45c2875 to your computer and use it in GitHub Desktop.
A demo of how to scroll tiles without creating a new array every frame.
package;
import openfl.Assets;
import openfl.Lib;
import openfl.display.Shape;
import openfl.display.Sprite;
import openfl.display.Tile;
import openfl.display.Tilemap;
import openfl.display.Tileset;
import openfl.events.Event;
import openfl.events.MouseEvent;
import openfl.geom.Rectangle;
import openfl.net.URLRequest;
import openfl.text.TextField;
import openfl.text.TextFormat;
import openfl.text.TextFieldAutoSize;
class Main extends Sprite {
/**
* Each tile is this many pixels across.
*/
private static inline var TILE_WIDTH:Float = 32;
/**
* The camera's viewport is this many tiles wide and this
* many tiles high.
*/
private static inline var TILES_ACROSS:Int = 15;
/**
* The camera defines which tiles are in sight.
*/
private var camera:Shape;
/**
* Contains just enough tiles to fill the camera's viewport, and
* tiles are never added or removed.
*
* Instead, the tiles will move and change images to give the
* illusion that the entire map is being rendered.
*/
private var tiles:Array<Array<Tile>>;
private var labels:Array<Array<TextField>>;
/**
* Defines which tile image goes with which point in space.
*/
private var map:Array<Array<Int>>;
private var previousStartColumn:Int = -1;
private var previousStartRow:Int = -1;
private var clickForSource:TextField;
public function new() {
super();
//Import the map.
var mapFile:String = Assets.getText("text/map.csv");
map = [
for(line in mapFile.split("\n"))
[for(tileID in line.split(","))
Std.parseInt(tileID)]
];
//Set up the Tilemap. Thanks to Sharm from OGA for the tiles!
//https://opengameart.org/content/16x16-overworld-tiles
var tileset:Tileset = new Tileset(Assets.getBitmapData("img/tiles.png"));
for(x in 0...3) {
tileset.addRect(new Rectangle(x * TILE_WIDTH, 0, TILE_WIDTH, TILE_WIDTH));
}
var tilemap:Tilemap = new Tilemap(stage.stageWidth, stage.stageHeight, tileset);
addChild(tilemap);
//Create the exact number of tiles and labels needed.
var textFormat:TextFormat = new TextFormat(null, 16, 0xFFFFFF);
tiles = [];
labels = [];
for(row in 0...(TILES_ACROSS + 1)) {
tiles.push([]);
labels.push([]);
for(column in 0...(TILES_ACROSS + 1)) {
var tile:Tile = new Tile();
tiles[row].push(tile);
tilemap.addTile(tile);
var label:TextField = new TextField();
label.defaultTextFormat = textFormat;
label.text = Std.string(row * (TILES_ACROSS + 1) + column);
label.autoSize = TextFieldAutoSize.CENTER;
label.selectable = false;
labels[row].push(label);
addChild(label);
}
}
//Create a black box to show the camera bounds.
camera = new Shape();
addChild(camera);
camera.graphics.lineStyle(1, 0x000000);
camera.graphics.drawRect(0, 0, TILE_WIDTH * TILES_ACROSS, TILE_WIDTH * TILES_ACROSS);
clickForSource = new TextField();
textFormat.color = 0x000000;
clickForSource.defaultTextFormat = textFormat;
clickForSource.text = "Click here for source code";
clickForSource.autoSize = TextFieldAutoSize.LEFT;
clickForSource.selectable = false;
addChild(clickForSource);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
stage.addEventListener(MouseEvent.CLICK, onClick);
}
private function onEnterFrame(e:Event):Void {
//Center the camera on the mouse.
camera.x = mouseX - camera.width / 2;
camera.y = mouseY - camera.height / 2;
//Stop the camera from going out of bounds.
if(camera.x < 0) {
camera.x = 0;
} else if(camera.x + camera.width > stage.stageWidth) {
camera.x = stage.stageWidth - camera.width;
}
if(camera.y < 0) {
camera.y = 0;
} else if(camera.y + camera.height > stage.stageHeight) {
camera.y = stage.stageHeight - camera.height;
}
//Start at the top-left corner of the visible area.
var startColumn:Int = Math.floor(camera.x / TILE_WIDTH);
var startRow:Int = Math.floor(camera.y / TILE_WIDTH);
for(row in startRow...(startRow + TILES_ACROSS + 1)) {
for(column in startColumn...(startColumn + TILES_ACROSS + 1)) {
var tile:Tile = tiles[row - startRow][column - startColumn];
//Set the image based on the map.
tile.id = map[row][column];
//Place the tile based on the camera's location.
tile.x = column * TILE_WIDTH;
tile.y = row * TILE_WIDTH;
//Move the label with the tile.
var label:TextField = labels[row - startRow][column - startColumn];
label.x = tile.x + (TILE_WIDTH - label.width) / 2;
label.y = tile.y + (TILE_WIDTH - label.height) / 2;
}
}
}
private function onClick(e:MouseEvent):Void {
if(e.stageX <= clickForSource.width && e.stageY <= clickForSource.height) {
var url:String = "https://gist.github.com/player-03/f28d41840d71f993f94c833be45c2875";
Lib.getURL(new URLRequest(url), "_blank");
}
}
}
@player-03
Copy link
Author

player-03 commented Aug 14, 2016

Compiled version.

The MIT License (MIT)

Copyright (c) 2016 Joseph Cloutier

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment