SFML и C++ Уроки \ Разработка игр › Форумы › SFML System, Window и другие вопросы › Урок 22.Проблема с загрузкой карты .tmx
В этой теме 1 ответ, 1 участник, последнее обновление rp88imxo 7 года/лет, 12 мес. назад.
-
АвторСообщения
-
Создал карту в Tile Map Editor, указал верные пути к тайлсету, однако после компиляции программы, персонаж,даже не вырисовываясь, падает бесконечно вниз на черном фоне.
PS Заметил один баг: если в строчке lvl.LoadFromFile(“map.tmx”); менять значение пути,программа не выдаст ошибки,что файл не был найден,даже если туда вбить рандомную строку,хотя определение данной функции в level.h говорит нам,что при неправильном пути должна выскакивать ошибка.Использую MVS 2015 Express.
C++123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367#include <SFML\Graphics.hpp>#include <iostream>#include "view.h"#include <sstream>#include "level.h"#include <vector>#include <list>using namespace sf;int heroDimensionX = 16;int heroDimensionY = 16;int dirTemp;class CHARACTER{public:std::vector<Object> obj;float x, y, w, h, dx, dy, speed,timer;int health;bool life, onGround, isMove;Texture texture;Sprite sprite;String name;CHARACTER(Texture &image, float X, float Y, float Weight, float Height,String charName){name = charName;isMove = false;timer = 0;onGround = false;life = true;speed = 0;dx = 0;dy = 0;sprite.setTexture(image);x = X; w = Weight;y = Y; h = Height;health = 100;}FloatRect getRect(){return FloatRect(x,y,w,h);}};class PLAYER : public CHARACTER{public:int playerScore;enum stateObject{left,right,up,down,jump,stay};stateObject state;PLAYER(Texture &image, float X, float Y,float Weight,float Height,String charName,Level &lvl1) : CHARACTER(image,X,Y,Weight,Height,charName){obj = lvl1.GetAllObjects();state = stay;playerScore = 0;sprite.setTextureRect(IntRect(80, 32, w, h));}void update(float time){control();switch (state){case PLAYER::left: dx = -speed;break;case PLAYER::right: dx = speed;break;case PLAYER::up:break;case PLAYER::down:break;case PLAYER::jump:break;case PLAYER::stay:sprite.setTextureRect(IntRect(80, 32, heroDimensionX, heroDimensionY));break;default:break;}x += dx * time;collision(dx, 0);y += dy * time;collision(0,dy);speed = 0;if (health <= 0) { life = false; }if ((dy != 0)){std::cout << dx << " , " << dy << std::endl;}dx = 0;dy += 0.0015*time; collision(0, dy);sprite.setPosition(x, y);}void control(){state = stay;if (Keyboard::isKeyPressed(Keyboard::D)){state = right;speed = 0.2;//CurrentFrame += 0.01 * time;//if (CurrentFrame > 2) CurrentFrame = 0;//player.heroSprite.setTextureRect(IntRect(80 + heroDimensionX * int(CurrentFrame), 32, heroDimensionX, heroDimensionY));getPlayerPosView(getPlayerPosX(),getPlayerPosY());}if (Keyboard::isKeyPressed(Keyboard::A)){state = left;speed = 0.2;//CurrentFrame += 0.01 * time;//if (CurrentFrame > 2) CurrentFrame = 0;//player.heroSprite.setTextureRect(IntRect(80 + heroDimensionX * int(CurrentFrame) + heroDimensionX, 32, -heroDimensionX, heroDimensionY));getPlayerPosView(getPlayerPosX(), getPlayerPosY());}if ((Keyboard::isKeyPressed(Keyboard::Space)) && (onGround)){state = jump;dy = -0.4;onGround = false;}}float getPlayerPosX(){return x;}float getPlayerPosY(){return y;}void collision(float dX, float dY)/*{for (int i = y/16; i < (y+heroDimensionY)/16; i++)for (int j = x/16; j < (x + heroDimensionX)/16; j++){if ((TileMap[i][j] == 'W')){if (dX > 0){x = 16*j - heroDimensionX;dx = 0;}if (dX < 0){x = 16 * j + 16;dx = 0;}if (dY < 0){y = 16 * i + 16;dy = 0.1;}if (dY > 0){y = 16 * i - heroDimensionY;dy = 0;onGround = true;}}if ((TileMap[i][j] == 'P')){if (dX > 0){x = 16 * j - heroDimensionX;dx = 0;}if (dX < 0){x = 16 * j + 16;dx = 0;}if (dY < 0){y = 16 * i + 16;dy = 0.1;}if (dY > 0){y = 16 * i - heroDimensionY;dy = 0;onGround = true;}TileMap[i][j] = 'A';}}}*/{for (int i = 0; i < obj.size();i++)if (getRect().intersects(obj[i].rect)){if (obj[i].name == "solid"){if (dY > 0) { y = obj[i].rect.top - h; dy = 0; onGround = true; }if (dY < 0) { y = obj[i].rect.top + obj[i].rect.height; dy = 0; }if (dX > 0) { x = obj[i].rect.left - w; }if (dX < 0) { x = obj[i].rect.left + obj[i].rect.width; }}}}};class ENEMY : public CHARACTER{public:float currentFrame;ENEMY(Texture &image, float X, float Y, float Weight, float Height, String charName,Level &lvl1) : CHARACTER(image,X,Y,Weight,Height,charName){obj = lvl1.GetObjects("solid");if (name == "mushroom"){currentFrame = 0;sprite.setTextureRect(IntRect(0,16,w,h));dx = 0.2;}}void collisionWithMap(float dX, float dY)/*{for (int i = y / 16; i < (y + heroDimensionY) / 16; i++)for (int j = x / 16; j < (x + heroDimensionX) / 16; j++){if ((TileMap[i][j] == 'W')){if (dX > 0){x = 16 * j - heroDimensionX;dx = -0.2;}if (dX < 0){x = 16 * j + 16;dx = 0.2;}if (dY < 0){y = 16 * i + 16;dy = 0.1;}if (dY > 0){y = 16 * i - heroDimensionY;dy = 0;onGround = true;}}}}*/{for (int i = 0; i < obj.size(); i++)if (getRect().intersects(obj[i].rect)){if (dY > 0) { y = obj[i].rect.top - h; dy = 0; onGround = true; }if (dY < 0) { y = obj[i].rect.top + obj[i].rect.height; dy = 0; }if (dX > 0) { x = obj[i].rect.left - w; }if (dX < 0) { x = obj[i].rect.left + obj[i].rect.width; }}}void update(float time){if (name == "mushroom"){currentFrame += 0.01 * time;if (currentFrame > 2) currentFrame = 0;x += dx * time;collisionWithMap(dx, 0);y += dy * time;collisionWithMap(0,dy);sprite.setTextureRect(IntRect(16 * int(currentFrame),16,w,h));sprite.setPosition(x,y);if (health <= 0){life = false;}dy += 0.0015*time; collisionWithMap(0, dy);}}};int main(){RenderWindow window(VideoMode(1600,720),"Test");bool isPressed = false;window.setFramerateLimit(120);Level lvl;lvl.LoadFromFile("2423423g23423");//создаем обьект игрока и враговObject playerObj = lvl.GetObject("player");Object mushroomEnemyObj = lvl.GetObject("mushroomEnemy");Font font;font.loadFromFile("bin/fonts/CyrilicOld.TTF");Image herrr;herrr.loadFromFile("bin/images/hero.png");herrr.createMaskFromColor(Color(0,0,255));Texture heroimage;heroimage.loadFromImage(herrr);Texture enemyTexture;enemyTexture.loadFromFile("bin/images/Enemies.png");Clock clock;Clock playTime;//Классы обьектов и логики игрыPLAYER player(heroimage,/*playerObj.rect.left*/25, /*playerObj.rect.top*/25, heroDimensionX, heroDimensionY,"Player",lvl);ENEMY mushroom(enemyTexture, mushroomEnemyObj.rect.left, mushroomEnemyObj.rect.top, heroDimensionX, heroDimensionY, "mushroom", lvl);float CurrentFrame = 0;int frameWithWeapon = 1;int timeOfplay;int timeForGenerator = 0;float tempX = 0;float tempY = 0;float distance = 0;float dx;float dy;while (window.isOpen()){float time = clock.getElapsedTime().asMicroseconds();clock.restart();time = time / 1000;if (player.life){timeOfplay = playTime.getElapsedTime().asSeconds();}Event event;while (window.pollEvent(event)){if (event.type == Event::Closed)window.close();}player.update(time);mushroom.update(time);window.clear();lvl.Draw(window);window.draw(player.sprite);window.draw(mushroom.sprite);window.display();}return 0;}Разобрался, функция LoadFromFile(“map.tmx”); не воспринимает путь целиком,пришлось закидывать карту и тайлсет в папку с прогой…
-
АвторСообщения
Для ответа в этой теме необходимо авторизоваться.