Hey I try to make a 2D tile game, but can't get the "hero" to collide with the tiles, he just fall right through them. Any idea?
My idea:
bool AbleToFall()
{
for(int n = 0; n < 200; n++)
{
if ( dbSpriteY(900) + 1 == dbSpriteY(n) && dbSpriteX(900) > dbSpriteX(n) - 1 && dbSpriteX(900) < dbSpriteX(n) + 15 )
{
return false;
}
else
{
return true;
}
}
return true;
}
Full Code:
#include "DarkGDK.h"
#define manyblocks 200
const int MAP_WIDTH = 20;
const int MAP_HEIGHT = 20;
int TestMap[MAP_HEIGHT][MAP_WIDTH]=
{
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1},
{1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1},
{1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1},
{1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,1},
{1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,1},
{1,2,2,2,2,2,2,2,2,2,2,2,2,1,1,1,1,1,1,1},
{1,2,2,2,2,2,2,2,2,2,2,2,2,1,1,1,1,1,1,1},
{1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1},
{1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1},
{1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1},
{1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1},
{1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1},
{1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1},
{1,2,2,2,2,2,2,2,2,2,2,2,2,1,1,1,1,1,1,1},
{1,2,2,2,1,1,2,2,2,2,2,2,1,1,1,1,1,1,1,1},
{1,2,2,2,1,1,2,2,2,2,2,1,1,1,1,1,1,1,1,1},
{1,2,2,2,1,1,2,2,2,2,1,1,1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ateClient();
bool AbleToGoLeft();
bool AbleToGoRight();
bool AbleToFall();
bool AbleToGetUp();
void DarkGDK ( void )
{
dbSyncOn ();
dbSyncRate ( 60 );
dbSetDisplayMode( 300, 300, 32 );
dbColorBackdrop( dbRGB( 0, 0, 0 ) );
dbSetImageColorKey ( 255, 0, 255 );
DrawMap();
while ( LoopGDK ( ) )
{
Game();
dbSync ( );
}
return;
}
void DrawMap()
{
dbLoadImage( "Tile1.bmp", 1);
dbLoadImage( "Tile2.bmp", 2);
dbLoadImage( "Grass1.bmp", 3);
dbLoadImage( "Man.bmp", 4);
int n;
int m;
for(int y = 0; y < MAP_HEIGHT; y++)
{
for(int x = 0; x < MAP_WIDTH; x++, n++, m++)
{
if( TestMap[y][x] == 1 )
{
dbSprite( n, (15 * x), (15 * y), 1 );
dbSetSpritePriority ( n, 1 );
if( TestMap[y-1][x] != 1 )
{
dbSprite( m, (15 * x), (15 * y) - 15, 3 );
dbSetSpritePriority ( m, 5 );
}
}
if( TestMap[y][x] == 2 )
{
dbSprite( n, (15 * x), (15 * y), 2 );
dbSetSpritePriority ( n, 1 );
//struct posofblock
//{
// int XPos;
// int YPos;
//} Block[n];
}
if( TestMapEnt[y][x] == 1 )
{
Player.XPos = (15 * x);
Player.YPos = (15 * y) + 15;
dbSprite( 900, Player.XPos, Player.YPos - 20, 4 );
dbSetSpritePriority ( 900, 900 );
//dbSprite( 901, Player.XPos + 6, Player.YPos + 1, 5 );
//dbSprite( 902, Player.XPos + 7, Player.YPos + 1, 5 );
//dbSprite( 903, Player.XPos - 1, Player.YPos - 1, 5 );
//dbSprite( 904, Player.XPos + 14, Player.YPos - 1, 5 );
//dbSprite( 905, Player.XPos + 6, Player.YPos - 21, 5 );
//dbSprite( 906, Player.XPos + 7, Player.YPos - 21, 5 );
}
}
}
}
void Game()
{
UpdateClient();
AbleToFall();
//AbleToGetUp();
//AbleToGoRight();
//AbleToGoLeft();
if ( AbleToFall() == true )
{
Player.YPos = Player.YPos + 1;
Player.YPos = Player.YPos + 1;
}
}
void UpdateClient()
{
dbSprite( 900, Player.XPos, Player.YPos - 20, 4 );
//dbSprite( 901, Player.XPos + 6, Player.YPos + 1, 5 );
//dbSprite( 902, Player.XPos + 7, Player.YPos + 1, 5 );
//dbSprite( 903, Player.XPos - 1, Player.YPos - 1, 5 );
//dbSprite( 904, Player.XPos + 14, Player.YPos - 1, 5 );
//dbSprite( 905, Player.XPos + 6, Player.YPos - 21, 5 );
//dbSprite( 906, Player.XPos + 7, Player.YPos - 21, 5 );
}
bool AbleToGoRight()
{
for(int n = 0; n < 200; n++)
{
if ( dbSpriteX(900) + 14 == dbSpriteX(n) && dbSpriteY(900) > dbSpriteY(n) - 1 && dbSpriteY(900) < (dbSpriteY(n) + 15 ) )
{
return false;
}
else
{
return true;
}
}
}
bool AbleToGoLeft()
{
for(int n = 0; n < 200; n++)
{
if ( dbSpriteX(900) - 1 == dbSpriteX(n) && dbSpriteY(900) > dbSpriteY(n) - 1 && dbSpriteY(900) < (dbSpriteY(n) + 15 ) )
{
return false;
}
else
{
return true;
}
}
return true;
}
bool AbleToFall()
{
for(int n = 0; n < 200; n++)
{
if ( dbSpriteY(900) + 1 == dbSpriteY(n) && dbSpriteX(900) > dbSpriteX(n) - 1 && dbSpriteX(900) < dbSpriteX(n) + 15 )
{
return false;
}
else
{
return true;
}
}
return true;
}
bool AbleToGetUp()
{
for(int n = 0; n < 200; n++)
{
if ( dbSpriteY(900) - 14 == dbSpriteY(n) && dbSpriteX(900) > dbSpriteX(n) - 1 && dbSpriteX(900) < (dbSpriteX(n) + 15 ) )
{
return false;
}
else
{
return true;
}
}
return true;
}
Found this and it works in flash, but have no idea how to use it in c++:
fscommand("allowscale", false);
fscommand("allowscale", false);
//our map is 2-dimensional array
myMap = [[1, 1, 1, 1, 1, 1, 1, 1], [1, 0, 0, 0, 0, 0, 0, 1], [1, 0, 1, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 1, 0, 1], [1, 0, 0, 0, 0, 0, 0, 1], [1, 1, 1, 1, 1, 1, 1, 1]];
//declare game object that holds info
game = {tileW:30, tileH:30};
//walkable tile
game.Tile0 = function () { };
game.Tile0.prototype.walkable = true;
game.Tile0.prototype.frame = 1;
//wall tile
game.Tile1 = function () { };
game.Tile1.prototype.walkable = false;
game.Tile1.prototype.frame = 2;
//declare char object, xtile and ytile are tile where chars center is
char = {xtile:2, ytile:1, speed:4};
//building the world
function buildMap(map) {
//attach empty mc to hold all the tiles and char
_root.attachMovie("empty", "tiles", 1);
//declare clip in the game object
game.clip = _root.tiles;
//get map dimensions
var mapWidth = map[0].length;
var mapHeight = map.length;
//loop to place tiles on stage
for (var i = 0; i<mapHeight; ++i) {
for (var j = 0; j<mapWidth; ++j) {
//name of new tile
var name = "t_"+i+"_"+j;
//make new tile object in the game
game[name] = new game["Tile"+map[i][j]]();
//attach tile mc and place it
game.clip.attachMovie("tile", name, i*100+j*2);
game.clip[name]._x = (j*game.tileW);
game.clip[name]._y = (i*game.tileH);
//send tile mc to correct frame
game.clip[name].gotoAndStop(game[name].frame);
}
}
//add the character mc
game.clip.attachMovie("char", "char", 10000);
//declare clip in the game object
char.clip = game.clip.char;
//calculate starting position
char.x = (char.xtile*game.tileW)+game.tileW/2;
char.y = (char.ytile*game.tileW)+game.tileW/2;
//add char dimensions to char object, half of clips width and height
char.width = char.clip._width/2;
char.height = char.clip._height/2;
//place char mc
char.clip._x = char.x;
char.clip._y = char.y;
}
function getMyCorners(x, y, ob) {
//find corner points
ob.downY = Math.floor((y+ob.height-1)/game.tileH);
ob.upY = Math.floor((y-ob.height)/game.tileH);
ob.leftX = Math.floor((x-ob.width)/game.tileW);
ob.rightX = Math.floor((x+ob.width-1)/game.tileW);
//check if they are walls
ob.upleft = game["t_"+ob.upY+"_"+ob.leftX].walkable;
ob.downleft = game["t_"+ob.downY+"_"+ob.leftX].walkable;
ob.upright = game["t_"+ob.upY+"_"+ob.rightX].walkable;
ob.downright = game["t_"+ob.downY+"_"+ob.rightX].walkable;
}
function moveChar(ob, dirx, diry) {
//vertical movement
//where are our edges?
//first we look for y movement, so x is old
getMyCorners(ob.x, ob.y+ob.speed*diry, ob);
//move got dammit... and check for collisions.
//going up
if (diry == -1) {
if (ob.upleft and ob.upright) {
//no wall in the way, move on
ob.y += ob.speed*diry;
} else {
//hit the wall, place char near the wall
ob.y = ob.ytile*game.tileH+ob.height;
}
}
//if going down
if (diry == 1) {
if (ob.downleft and ob.downright) {
ob.y += ob.speed*diry;
} else {
ob.y = (ob.ytile+1)*game.tileH-ob.height;
}
}
//horisontal movement
//changing x with speed and taking old y
getMyCorners(ob.x+ob.speed*dirx, ob.y, ob);
//if going left
if (dirx == -1) {
if (ob.downleft and ob.upleft) {
ob.x += ob.speed*dirx;
} else {
ob.x = ob.xtile*game.tileW+ob.width;
}
}
//if going right
if (dirx == 1) {
if (ob.upright and ob.downright) {
ob.x += ob.speed*dirx;
} else {
ob.x = (ob.xtile+1)*game.tileW-ob.width;
}
}
//update char position
ob.clip._x = ob.x;
ob.clip._y = ob.y;
//face the direction
ob.clip.gotoAndStop(dirx+diry*2+3);
//calculate the tile where chars center is
ob.xtile = Math.floor(ob.clip._x/game.tileW);
ob.ytile = Math.floor(ob.clip._y/game.tileH);
return (true);
}
function detectKeys() {
var ob = _root.char;
var keyPressed = false;
if (Key.isDown(Key.RIGHT)) {
keyPressed = _root.moveChar(ob, 1, 0);
} else if (Key.isDown(Key.LEFT)) {
keyPressed = _root.moveChar(ob, -1, 0);
} else if (Key.isDown(Key.UP)) {
keyPressed = _root.moveChar(ob, 0, -1);
} else if (Key.isDown(Key.DOWN)) {
keyPressed = _root.moveChar(ob, 0, 1);
}
//walk animation
if (!keyPressed) {
ob.clip.char.gotoAndStop(1);
} else {
ob.clip.char.play();
}
}
//make the map
buildMap(myMap);
stop();
Thanks for the help