SFML и C++ Уроки \ Разработка игр › Форумы › SFML Graphics › Проблема с взаимодействием с картой. SFML. › Ответ в теме: Проблема с взаимодействием с картой. SFML.
Значит переписал код твоего 21 урока под свои цели и вылезла новая ошибка – не инециализирована переменная i в цикле Collision для Ball
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
#include <SFML/Graphics.hpp> #include "map.h" //#include "Ball.h" using namespace sf; float offsetX=0, offsetY=0; class Entity{ public: float dx, dy, x, y, speed,moveTimer;//добавили переменную таймер для будущих целей int w,h; //bool life, isMove, onGround; Texture texture; Sprite sprite; String name; Entity(Image &image, float X,float Y,int W,int H,String Name){ x=X; y=Y; w=W; h=H; name = Name; moveTimer = 0; speed = 0; dx=0;dy=0; texture.loadFromImage(image); sprite.setTexture(texture); sprite.setOrigin(w / 2, h / 2); } }; class Palka:public Entity{ public: enum{left,right,stay} state; Palka(Image &image, float X, float Y,int W,int H,String Name):Entity(image,X,Y,W,H,Name){ state=stay; if( name == "Plotik"){ sprite.setTextureRect(IntRect(4,19,w,h)); } } void control(){ if(Keyboard::isKeyPressed){ if (Keyboard::isKeyPressed(Keyboard::Left)) { state=left;speed = 0.0006; } if (Keyboard::isKeyPressed(Keyboard::Right)) { state = right; speed = 0.0006; } } } void Collision(float Dx,float Dy) { for (int i=y/32; i<(y+h)/32; i++) for (int j=x/32; j<(x+w)/32; j++) { if((TileMap[i][j] == 'B')||(TileMap[i][j] == 'C')) { if (Dx>0){ x = j * 32 - w; } if (Dx<0){ x = j * 32 + 32; } } } } void update(float time) { control(); switch (state) { case right:dx = speed; break; case left:dx = -speed;break; case stay: break; } x+=dx*time; Collision(dx, 0); sprite.setPosition(x+w/2, y + h / 2); } }; class Ball:public Entity { public: Ball(Image &image, float X, float Y,int W,int H,String Name):Entity(image,X,Y,W,H,Name){ if(name == "Ball") { sprite.setTextureRect(IntRect(0,0,w,h)); dx = 0.0003; } } void Collision(float Dx,float Dy){ for(int i;i<(y+h)/32;i++) for(int j;j<(x+w)/32;j++) { if(TileMap[i][j] =='B'){ if (Dy>0){ y = i * 32 - h; }//по Y вниз=>идем в пол(стоим на месте) или падаем. В этот момент надо вытолкнуть персонажа и поставить его на землю, при этом говорим что мы на земле тем самым снова можем прыгать if (Dy<0){ y = i * 32 + 32; }//столкновение с верхними краями карты(может и не пригодиться) if (Dx>0){ x = j * 32 - w; dx = -0.1; sprite.scale(-1, 1); }//с правым краем карты if (Dx<0){ x = j * 32 + 32; dx = 0.1; sprite.scale(-1, 1); }// с левым краем карты } } } void update(float time) { if(name=="Ball") { Collision(dx,0); x+=dx*time; sprite.setPosition(x + w / 2, y + h / 2); } } }; int main() { RenderWindow window( VideoMode(1200, 800), "Test!"); Image t; t.loadFromFile("fang.png"); Image map_image; map_image.loadFromFile("map.png"); Texture map; map.loadFromImage(map_image); Sprite s_map; s_map.setTexture(map); Image l; l.loadFromFile("ball.png"); Palka p(t,0, 0,40,30,"Plotik"); Ball Ball(l, 600,140,200,97,"Ball"); Clock clock; //RectangleShape rectangle( Vector2f(32,32)); while (window.isOpen()) { float time = clock.getElapsedTime().asMicroseconds(); clock.restart(); Event event; while(window.pollEvent(event)) { if(event.type == Event::Closed) window.close(); } p.update(time); Ball.update(time); window.clear(Color::White); for (int i=0; i<H; i++) for (int j=0; j<W ; j++) { if (TileMap[i][j]=='B') s_map.setTextureRect(IntRect(0, 0, 32, 32)); if (TileMap[i][j]=='A') s_map.setTextureRect(IntRect(64, 0, 32, 32)); if (TileMap[i][j]==' ') continue; s_map.setPosition(j*32,i*32) ; window.draw(s_map); } window.draw(p.sprite); window.draw(Ball.sprite); window.display(); } return 0; } |