PDA

View Full Version : need advice in tile based gameing



Exos Sho
September 28th, 2006, 09:11 AM
i m trying to do the tonypa thing in as2(i doubt that i am the only one who is trying it), i made 4 classes, and i am sure there are errors ^^, because i tried it



dynamic class Tile
{
private var frame : Number;
private var walkable : Boolean;
private var door : Boolean;
//construktor
public function Tile (p1 : Number, p2 : Number)
{
if (p2 == "" || p2 == 1)
{
p2 = false;
} else if (p2 == 0)
{
p2 = true
}
var frame = p1;
var walkable = p2;
}
}
i know that there are better ways for declaring walkable tiles, but in this one are only two types of tiles.
i guess in a bigger game they do bitwise


drawMap:



dynamic class drawMap
{
private var i : Number;
private var j : Number;
//construktor
private function drawMap (map)
{
var mapWidth = map [0].length;
var mapHeight = map.length;
for (var i = 0; i < mapHeight; ++ i)
{
for (var j = 0; j < mapWidth; ++ j)
{
var tileID = map [i][j];
var type : Number = tileID + 1;
var name = "t_" + i + "_" + j;
game [name] = new Tile (type, tileID);
game.clip.attachMovie ("tile", name, i * 100 + j * 2);
game.clip [name]._x = (j * game.tileW);
game.clip [name]._y = (i * game.tileH);
game.clip [name].gotoAndStop (game [name].frame);
}
}
char.clip = game.clip.char;
char.x = (char.xtile * game.tileW) + game.tileW / 2;
char.y = (char.ytile * game.tileW) + game.tileW / 2;
char.width = char.clip._width / 2;
char.height = char.clip._height / 2;
char.clip._x = char.x;
char.clip._y = char.y;
}
}

game:



dynamic class game
{
private var tileW : Number = 30;
private var tileH : Number = 30;
private var clip : MovieClip = _root.attachMovie ("empty", "tiles", 1);
//construktor
public function game (w : Number, h : Number)
{
tileW = w;
tileH = h;
}
}

and i even tried an char-class:



dynamic class char
{
private var xtile : Number;
private var ytile : Number;
private var speed : Number;
private var clip : MovieClip = attachMovie ("char", "char", 10000);
//construktor
public function char (x : Number, y : Number, s : Number)
{
xtile = x;
ytile = y;
speed = s;
}
}



i guess the most problems are in the builMap class

Thinker2501
September 28th, 2006, 02:15 PM
I am working on the same thing myself right now and will be back in touch as I make progress. I just wanted to point out now, thought, that none of these classes need to be dynamic. You could save yourself a lot of potential runtime errors by changing that :thumb2:

I'm eager to see the progress you make.

Exos Sho
September 28th, 2006, 02:45 PM
OK thanks for the clue, i worked out a new kind of the tile class



class Tile
{
// 0 - 63 = walkable
//64 - 127 = wall
private var frame : Number;
private var walkable : Boolean;
private var door : Boolean;
//construktor
public function Tile (p1 : Number, p2)
{
if (p2 / 64 <= 0)
{
p2 = true;
} else if (p2 / 64 < 2 && p2 / 64 >= 1)
{
p2 = false;
}
var frame = p1;
var walkable = p2;
}
}


so u can make 64 tiles with different design, and they are all walk able, the next 64 designs are walls or trees etc.
but i think there are better ways
for example bitwise-things like


extraHD = true;
network = true;
burner = true;
tvcard = true;

extraHD = (1<<0); // 1 Bit: 0 (false), 1 (true)
network = (1<<1); // 2 Bit: 0 (false), 2 (true)
burner = (1<<2); // 3 Bit: 0 (false), 4 (true)
tvcard = (1<<3); // 4 Bit: 0 (false), 8 (true)

selection = extraHD | network | burner | tvcard;


function calc(selection) {
var price = 0;

if (selection & 1) {
trace("+ Extra HD");
price += 200;
}

if (selection & 2) {
trace("+ network");
price += 150;
}

if (selection & 4) {
trace("+ DVD Burner");
price += 450;
}

if (selection & 8) {
trace("+ TV-Card");
price+= 100;
}

return price;
}
trace(calc(selection));



but i first got to take a look closer ^^

Thinker2501
September 28th, 2006, 06:09 PM
I am using a Tile superclass which is extended by a large number of sub-classes. Each specific tile has it's own class. This allows for infinite scalability and makes creating a large number of custom tile types easy (such as moving, breakable, pushable tiles).

Generally with class files you want to start general and then get more specific as you move down the chain. So a 'Tile' superclass will contain the properties that all tiles will have in common, then additional tile classes will extend 'Tile' to add additional functionality.

Exos Sho
September 29th, 2006, 08:52 AM
char-class updated


class com.exossho.gameing.char
{
private var currentLifePower : Number;
private var alive : Boolean;
private var name : String;
private var clip : MovieClip;
private var xtile : Number;
private var ytile : Number;
private var speed : Number;
//magic values in class properties
private static var MAX_LIFE_POWER : Number = 100;
private static var CHAR_CLIP : String = "char_clip";
//construktor
public function char (charName : String, target : MovieClip, depth : Number, x : Number, y : Number, s : Number)
{
clip = target.attachMovie (char.CHAR_CLIP, "char" + depth, depth);
setName (charName);
currentLifePower = tamagotchi.MAX_LIFE_POWER;
alive = true;
Key.addListener (this);
setCoor (x, y);
setSpeed (s);
}
//functions
public function setCoor (newXtile : Number, newYtile : Number) : Void
{
xtile = newXtile;
ytile = newYtile;
}
public function setSpeed (newSpeed : Number) : Void
{
speed = newSpeed;
}
public function harmChar (minusLife:Number):Void
{
currentLifePower -= minusLife;
}
public function heelChar (plusLife:Number):Void
{
currentLifePower += plusLife;
}
}


i dont know how to set the Tile-Class up
the only common property, i think they have is the scale
so Tile super-class would only be:


class Tile {
private var tileW:Number;
private var tileH:Number;

public function Tile(width:Number, heiht:Number){
setTile(width, height);
}
public function setTile(w:Number, h:Number){
tileW = w;
tileH = h;
}
}

a sub-class would be:


class Water extend Tile{
private var walkable:Boolean;
private var swimmable:Boolean;
private var frame:Number;

public function Water(walk:Boolean, swimm:Boolean, pic:Number){
setWater(walk, swimm, pic);
}
public function setWater(w:Boolean, s:Boolean, p:Number){
walkable = w;
swimmable = s;
frame = p;
}
}

so i think that if i declare right at the start that Tile.setTile(30,30),
Water would have the same scale, or do i have to declare the scale in Tile in the class directly?

next question is, if its possible to do sub-sub-classes, like:


class deepWater extend Water{
private var diveable:Boolean

public function deepWater(dive:Boolean){
setDwater(dive:Boolean);
}
public function setDwater(d:Boolean){
diveable = d;
}
}


or is my understanding of the extending-Thing completely wrong?

Exos Sho
September 29th, 2006, 01:30 PM
not yet using the tile-super-class-sub-class-sub-sub-class-idea


class Map {
private var i:Number;
private var j:Number;
private var mapWidth:Number;
private var mapHeight:Number;
private var typ:Number;
private var name:String;
//construktor
public function Map(newMap:Array){
drawMap(newMap);
}
public var setMap(w:Number, h:Number){
mapWidth = w;
mapHeight = h;
}
private function drawMap(map:Array) {
setMap(map[0].length, map.length);
for (var i = 0; i<mapHeight; ++i) {
for (var j = 0; j<mapWidth; ++j) {
tileID = map[i][j];
typ = tileID+1;
name = "t_"+i+"_"+j;
game[name] = new Tile(typ, tileID);
game.clip.attachMovie("tile", name, i*100+j*2);
game.clip[name]._x = (j*game.tileW);
game.clip[name]._y = (i*game.tileH);
game.clip[name].gotoAndStop(game[name].frame);
}
}
/*char.clip = game.clip.char;
char.x = (char.xtile * game.tileW) + game.tileW / 2;
char.y = (char.ytile * game.tileW) + game.tileW / 2;
char.width = char.clip._width / 2;
char.height = char.clip._height / 2;
char.clip._x = char.x;
char.clip._y = char.y;*/
}
}

Exos Sho
September 29th, 2006, 04:47 PM
i think the char-, game- and Tile-classes are good sofar(atleast the compiler dont find any error^^), now i have to repair the Map-class
and then i hope it works, so far. when i got it working so far, im going to take a try at the super-class-sub-class-way

char.as


class char
{
private var currentLifePower : Number;
private var alive : Boolean;
private var name : String;
private var clip : MovieClip;
private var xtile : Number;
private var ytile : Number;
private var speed : Number;
//magic values in class properties
private static var MAX_LIFE_POWER : Number = 100;
private static var CHAR_CLIP : String = "char_clip";
//construktor
public function char (charName : String, target : MovieClip, depth : Number, x : Number, y : Number, s : Number)
{
clip = target.attachMovie ("char_clip", "char" + depth, depth);
currentLifePower = MAX_LIFE_POWER;
alive = true;
Key.addListener (this);
setCoor (x, y);
setSpeed (s);
}
//functions
public function setCoor (newXtile : Number, newYtile : Number) : Void
{
xtile = newXtile;
ytile = newYtile;
}
public function setSpeed (newSpeed : Number) : Void
{
speed = newSpeed;
}
public function harmChar (minusLife:Number):Void
{
currentLifePower -= minusLife;
}
public function heelChar (plusLife:Number):Void
{
currentLifePower += plusLife;
}
}



game.as


class game
{
private var tileW : Number;
private var tileH : Number;
private static var clip : MovieClip;
//construktor
public function game (w : Number, h : Number)
{
tileW = w;
tileH = h;
_root.attachMovie("empty", "tiles", 1);
clip=_root.tiles;
}
public function setTileW (wid : Number, hei : Number)
{
if ((wid = NaN) && (hei = NaN))
{
wid = 30;
hei = 30;
trace ("tileW and tileH have to be set (or atleast one of them), auto-Value = 30");
}
else if (wid != hei)
{
wid = 30;
hei = 30;
trace ("tileW and tileH have to be equal, auto-Value = 30");
}
else if ((wid = NaN) || (hei = NaN))
{
if (wid = NaN)
{
wid = hei;
trace ("tileW has to be set, auto-Value = tileH");
}
if (hei = NaN)
{
hei = wid;
trace ("tileH has to be set, auto-Value = tileW");
}
}
tileW = wid;
tileH = hei;
}
}



Tile.as


class Tile {
var frame:Number;
var walkable:Boolean;
var door:Boolean;
//construktor
public function Tile(p1:Number, p2) {
if (p2 == "") {
p2 = false;
} else if (p2 == 0) {
p2 = true;
} else if (p2 == 1) {
p2 = false;
}
var frame = p1;
var walkable = p2;
}
}



Map.as


class Map{
private var i:Number;
private var j:Number;
private var mapWidth:Number;
private var mapHeight:Number;
private var typ:Number;
private var name:String;
//construktor
public function Map(newMap:Array){
drawMap(newMap);
}
public function setMap(w:Number, h:Number){
mapWidth = w;
mapHeight = h;
}
private function drawMap(map:Array) {
setMap(map[0].length, map.length);
for (var i = 0; i<mapHeight; ++i) {
for (var j = 0; j<mapWidth; ++j) {
tileID = map[i][j];
typ = tileID+1;
name = "t_"+i+"_"+j;
game[name] = new Tile(typ, tileID);
game.clip.attachMovie("tile", name, i*100+j*2);
game.clip[name]._x = (j*game.tileW);
game.clip[name]._y = (i*game.tileH);
game.clip[name].gotoAndStop(game[name].frame);
}
}
/*char.clip = game.clip.char;
char.x = (char.xtile * game.tileW) + game.tileW / 2;
char.y = (char.ytile * game.tileW) + game.tileW / 2;
char.width = char.clip._width / 2;
char.height = char.clip._height / 2;
char.clip._x = char.x;
char.clip._y = char.y;*/
}
}

jim katz
September 29th, 2006, 05:43 PM
sorry

i think the char-, game- and Tile-classes are good sofar(atleast the compiler dont find any error^^), now i have to repair the Map-class
and then i hope it works, so far. when i got it working so far, im going to take a try at the super-class-sub-class-way

char.as


class char
{
private var currentLifePower : Number;
private var alive : Boolean;
private var name : String;
private var clip : MovieClip;
private var xtile : Number;
private var ytile : Number;
private var speed : Number;
//magic values in class properties
private static var MAX_LIFE_POWER : Number = 100;
private static var CHAR_CLIP : String = "char_clip";
//construktor
public function char (charName : String, target : MovieClip, depth : Number, x : Number, y : Number, s : Number)
{
clip = target.attachMovie ("char_clip", "char" + depth, depth);
currentLifePower = MAX_LIFE_POWER;
alive = true;
Key.addListener (this);
setCoor (x, y);
setSpeed (s);
}
//functions
public function setCoor (newXtile : Number, newYtile : Number) : Void
{
xtile = newXtile;
ytile = newYtile;
}
public function setSpeed (newSpeed : Number) : Void
{
speed = newSpeed;
}
public function harmChar (minusLife:Number):Void
{
currentLifePower -= minusLife;
}
public function heelChar (plusLife:Number):Void
{
currentLifePower += plusLife;
}
}



game.as


class game
{
private var tileW : Number;
private var tileH : Number;
private static var clip : MovieClip;
//construktor
public function game (w : Number, h : Number)
{
tileW = w;
tileH = h;
_root.attachMovie("empty", "tiles", 1);
clip=_root.tiles;
}
public function setTileW (wid : Number, hei : Number)
{
if ((wid = NaN) && (hei = NaN))
{
wid = 30;
hei = 30;
trace ("tileW and tileH have to be set (or atleast one of them), auto-Value = 30");
}
else if (wid != hei)
{
wid = 30;
hei = 30;
trace ("tileW and tileH have to be equal, auto-Value = 30");
}
else if ((wid = NaN) || (hei = NaN))
{
if (wid = NaN)
{
wid = hei;
trace ("tileW has to be set, auto-Value = tileH");
}
if (hei = NaN)
{
hei = wid;
trace ("tileH has to be set, auto-Value = tileW");
}
}
tileW = wid;
tileH = hei;
}
}



Tile.as


class Tile {
var frame:Number;
var walkable:Boolean;
var door:Boolean;
//construktor
public function Tile(p1:Number, p2) {
if (p2 == "") {
p2 = false;
} else if (p2 == 0) {
p2 = true;
} else if (p2 == 1) {
p2 = false;
}
var frame = p1;
var walkable = p2;
}
}



Map.as


class Map{
private var i:Number;
private var j:Number;
private var mapWidth:Number;
private var mapHeight:Number;
private var typ:Number;
private var name:String;
//construktor
public function Map(newMap:Array){
drawMap(newMap);
}
public function setMap(w:Number, h:Number){
mapWidth = w;
mapHeight = h;
}
private function drawMap(map:Array) {
setMap(map[0].length, map.length);
for (var i = 0; i<mapHeight; ++i) {
for (var j = 0; j<mapWidth; ++j) {
tileID = map[i][j];
typ = tileID+1;
name = "t_"+i+"_"+j;
game[name] = new Tile(typ, tileID);
game.clip.attachMovie("tile", name, i*100+j*2);
game.clip[name]._x = (j*game.tileW);
game.clip[name]._y = (i*game.tileH);
game.clip[name].gotoAndStop(game[name].frame);
}
}
/*char.clip = game.clip.char;
char.x = (char.xtile * game.tileW) + game.tileW / 2;
char.y = (char.ytile * game.tileW) + game.tileW / 2;
char.width = char.clip._width / 2;
char.height = char.clip._height / 2;
char.clip._x = char.x;
char.clip._y = char.y;*/
}
}

bombsledder
November 14th, 2006, 07:06 PM
looks neat, I created a Tile based class that works specifically on loading tiles on the go, so if the map is 1024 X 1024 the only tiles that will be loaded will be like a 8X8 for the screen, and the classes go like this





Object
+- Game

Object
+- Tile

Object
+- Item
- Container
- Teleport

Object
+- Creature
- Player
- Monster




maybe i will post the engine later