Hi, I’m currently working on a level engine for a game I’m creating. The idea behind it is to chop the entire stage into segments by using a grid. I have used a nested for loop to sort through each segment, and if the container x position has reached a certain amount, it will add a new segment. The same thing will happen on the y.
Once its done it should work this way…
[disabled segment] [Visible Segment] [disabled segment]
[Visible segment] [Onstage Segment] [Visible segment]
[disabled segment] [Visible Segment] [disabled segment]
only the adjacent segments on each of the 4 corners will be visible and the others will be destroyed. I will work on the removal part later, I just need the segments to be loaded appropriately.
The problem I’m having is the last segment at the bottom row does not show consistently, and on other occasions many segments from the bottom row do not show up.
This is the function in question, inside infiltrateEngine.as
I have attached a zip file containing all the relevant files needed to run.
public function levelEngine(vx:Number, vy:Number) {
//move the levelContainer based on the hero's movement
levelContainer.x -= int(vx);
levelContainer.y -= int(vy);
//define the current position of levelContainer
var xPos:int = levelContainer.x;
var yPos:int = levelContainer.y;
//define the ends of the levelContainer clip
var xEnd:int = -levelContainer.width + stageRef.stageWidth;
var yEnd:int = -levelContainer.height + stageRef.stageHeight;
//padding distance before a new clip will be filled
var pad:int = 20;
//make xPos,yPos of levelContainer a positive int (shorthand if, faster than math.abs)
var absX:int = (xPos < 0 ? xPos * -1 : xPos);
var absY:int = (yPos < 0 ? yPos * -1 : yPos);
//define when a new cell should be added to the stage
var rowAmt:int = levelGrid.length;
//for loop of each row
//trace(rowAmt, columnAmt);
for (var i:int = 0; i < rowAmt - 1; ++i) {
var columnAmt:int = levelGrid*.length;
//for loop of each column in the row
for (var j:int = 0; j < columnAmt - 1; ++j) {
if (levelGrid*[j][2] == true) {
//check right cell
var rightCellX:int = levelGrid*[j + 1][0];
var distanceX:int = rightCellX - absX - stage.stageWidth;
if ((distanceX < pad) && (distanceX > -1)) {
//if the right cell is not visible fill it in
if (levelGrid*[j + 1][2] == false) {
trace("nextCellX", rightCellX, distanceX, levelArray*[j + 1]);
addCell(i, j + 1);
}
}
//check lower cell
var lowerCellY:int = levelGrid[i+1][j][1];
var distanceY:int = lowerCellY - absY - stage.stageHeight;
if ((distanceY < pad) && (distanceY > -1)) {
//if the lower cell is not visible fill it in
if (levelGrid[i + 1][j][2] == false) {
trace("lowerCellY", lowerCellY, distanceY, levelArray[i + 1][j]);
addCell(i + 1, j);
}
}
}
//trace(distance);
//levelGrid*[j][1] + stageRef.stageWidth);
//trace(cellX);
}
}
//level constraints at absolute ends
if (levelContainer.x >= 0) {
levelContainer.x = 0;
}else if (levelContainer.x <= xEnd) {
levelContainer.x = xEnd;
}
if (levelContainer.y >= 0) {
levelContainer.y = 0;
}else if (levelContainer.y <= yEnd) {
levelContainer.y = yEnd;
}
}
The entire infiltrateEngine.as file is below.
package
{
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.display.Stage;
import flash.events.Event;
import flash.geom.Rectangle;
import lt.uza.utils.*; //Global object
import com.k2xl.util.EventManager;
/**
* ...
* @author Conceptz @ Hitmen Studios
*/
public class InfiltrateEngine extends MovieClip
{
/* VARS
* levelArray - used to define the level grid
* levelClip - holds level graphic data
* levelRows - how many rows the level has
* levelColumns - how many columns the level has
* levelSpeed - the speed of the level scroll
* cellWidth - the width of each cell
* cellHeight - the height of each cell
*/
private var stageRef:Stage;
//level vars
private var levelArray:Array = [];
private var levelArtClass:Class;
private var levelGrid:Array = [];
private var levelRows:int;
private var levelColumns:int;
private var levelSpeed:int;
private var levelContainer:MovieClip;
//cell vars
private var activeCell:String;
private var cellWidth:int;
private var cellHeight:int;
private var global:Global;
/* FLOW
* pull in levelArray, levelClip
* set up the grid based on levelArray parameters
* make grid segments visible based on active segment
*
*
*/
public function InfiltrateEngine(StageRef:Stage, LevelArray:Array, LevelParameters:Array, LevelArtClass:Class) {
//VARS IN - stageRef, levelArray, levelParameters, levelArtClip
this.stageRef = StageRef;
this.levelArray = LevelArray;
this.levelArtClass = LevelArtClass;
this.levelRows = LevelParameters[0];
this.levelColumns = LevelParameters[1];
this.cellWidth = LevelParameters[2];
this.cellHeight = LevelParameters[3];
this.levelSpeed = LevelParameters[4];
global = Global.getInstance();
EventManager.addEventListener(this, Event.ENTER_FRAME, loop, false, 0, true, true);
initInfiltrate();
//trace(levelArtClass);
}
private function initInfiltrate():void {
var containerWidth:int = cellWidth * levelColumns;
var containerHeight:int = cellHeight * levelRows;
var xPos:int;
var yPos:int;
var containerSizer:Sprite = new Sprite();
//creates a graphic to expand the levelContainer
containerSizer.graphics.beginFill(0xffffff);
containerSizer.graphics.drawRect(0, 0, containerWidth, containerHeight);
containerSizer.graphics.endFill();
levelContainer = new MovieClip();
levelContainer.addChild(containerSizer);
stageRef.addChild(levelContainer);
//populate the levelGrid array with the relevant data
for (var i:int = 0; i < levelRows; i++) {
var rowArray:Array = [];
for (var j:int = 0; j < levelColumns; ++j) {
if (levelArray*[j] != null) {
var cellArray:Array = [];
//var cellIndex:String = String(* + "-" + [j]);
//cellArray(cellIndex:String, xPos:int, yPos:int, visible:Boolean);
cellArray.push(xPos, yPos, false)
} else {
cellArray.push(null);
}
xPos += cellWidth;
rowArray.push(cellArray);
}
xPos = 0;
yPos += cellHeight;
levelGrid.push(rowArray);
}
//activeCell = levelGrid[0][0][0];
addCell(0, 0);
}
private function loop(e:Event) {
//trace("looping");
levelEngine(global.heroVX, global.heroVY);
}
public function levelEngine(vx:Number, vy:Number) {
//move the levelContainer based on the hero's movement
levelContainer.x -= int(vx);
levelContainer.y -= int(vy);
//define the current position of levelContainer
var xPos:int = levelContainer.x;
var yPos:int = levelContainer.y;
//define the ends of the levelContainer clip
var xEnd:int = -levelContainer.width + stageRef.stageWidth;
var yEnd:int = -levelContainer.height + stageRef.stageHeight;
//padding distance before a new clip will be filled
var pad:int = 20;
//make xPos,yPos of levelContainer a positive int (shorthand if, faster than math.abs)
var absX:int = (xPos < 0 ? xPos * -1 : xPos);
var absY:int = (yPos < 0 ? yPos * -1 : yPos);
//define when a new cell should be added to the stage
var rowAmt:int = levelGrid.length;
//for loop of each row
//trace(rowAmt, columnAmt);
for (var i:int = 0; i < rowAmt - 1; ++i) {
var columnAmt:int = levelGrid*.length;
//for loop of each column in the row
for (var j:int = 0; j < columnAmt - 1; ++j) {
if (levelGrid*[j][2] == true) {
//check right cell
var rightCellX:int = levelGrid*[j + 1][0];
var distanceX:int = rightCellX - absX - stage.stageWidth;
if ((distanceX < pad) && (distanceX > -1)) {
//if the right cell is not visible fill it in
if (levelGrid*[j + 1][2] == false) {
trace("nextCellX", rightCellX, distanceX, levelArray*[j + 1]);
addCell(i, j + 1);
}
}
//check lower cell
var lowerCellY:int = levelGrid[i+1][j][1];
var distanceY:int = lowerCellY - absY - stage.stageHeight;
if ((distanceY < pad) && (distanceY > -1)) {
//if the lower cell is not visible fill it in
if (levelGrid[i + 1][j][2] == false) {
trace("lowerCellY", lowerCellY, distanceY, levelArray[i + 1][j]);
addCell(i + 1, j);
}
}
}
//trace(distance);
//levelGrid*[j][1] + stageRef.stageWidth);
//trace(cellX);
}
}
//level constraints at absolute ends
if (levelContainer.x >= 0) {
levelContainer.x = 0;
}else if (levelContainer.x <= xEnd) {
levelContainer.x = xEnd;
}
if (levelContainer.y >= 0) {
levelContainer.y = 0;
}else if (levelContainer.y <= yEnd) {
levelContainer.y = yEnd;
}
}
private function addCell(row:int, column:int):void {
trace("adding", row, column);
var levelCell = new levelArtClass();
levelCell.x = levelGrid[row][column][0];
levelCell.y = levelGrid[row][column][1];
levelGrid[row][column][2] = true;
levelCell.gotoAndStop(levelArray[row][column]);
levelContainer.addChild(levelCell);
}
private function clearCell(row:int, column:int):void {
trace("clearing", row, column);
levelGrid[row][column][2] = false;
levelContainer.removeChild(getChildByName(row+"-"+column));
}
private function cleanUp():void {
EventManager.removeAllListeners(null, Event.ENTER_FRAME, loop);
}
}
}
Any help/suggestions are appreciated.