2016-09-26 10 views
-1

私はSFMLを使って練習用の小さなゲームを作成しています。コンソールの。SFMLスプライトがウィンドウに表示されていませんが、私にエラーを与えていません

私はプレイヤーを描こうとしているときに遭遇したことがあります。私は奇妙なことを言っています。最初は表示されていたので、何か問題はないと思っていますが、敵の戦車の派生クラスを作成して新しいファイルを作成した後、すべてのファイルがtogheter(5ファイル、1 cpp、4ヘッダー)。私はそれを理解した後、私はこの問題に遭遇し、それに対する解決策を見つけることができません。それはその問題の前に働いていましたが、今はもうそれはありません。

テクスチャが読み込まれている場合は、それを設定した位置を確認しましたが、大丈夫です。ここで

は、プレイヤータンク

class tank{ 

protected: 
sf::FloatRect boundingBox; 
sf::Sprite tankSprite; 

bullet *mBullet; 
public : 
tank(); // constructor 

bullet * get_ptr(){return mBullet;} 
sf::FloatRect get_boundingBox(){return boundingBox;} 

void setRotation(float); 
void show(){game.draw(tankSprite);} 
void update(); 
void shoot(){mBullet = new bullet(tankSprite);} 
}; 

ためのクラスであり、これは私が世界を更新し、私の窓の上に

if(!stop) 
    Me.update(); 

if(Me.get_ptr()->isFired()==true) 
    Me.get_ptr()->update(); 

// Output on the window 
game.clear(); 
Me.show(); 
if(Me.get_ptr()->isFired()==true) 
    Me.get_ptr()->show(); 
game.display(); 

サイドノートを描く私のメインのコードです:ゲームですウィンドウをレンダリングし、グローバルに宣言されている(私は悪い習慣であることを知っている、私はこの悪い習慣を捨てることを遅くしている)

main.cpp

#include <SFML/Graphics.hpp> 
#include "Tank.h" 

int main() 
{ 
tank Me; 

init(); 
while (game.isOpen()) 
{ 
    sf::Event event; 
    while (game.pollEvent(event)) 
    { 
     if (event.type == sf::Event::Closed) 
      game.close(); 
     else if (event.type == sf::Event::KeyPressed) 
     { 
      stop=false; 
      switch(event.key.code) 
      { 
       case sf::Keyboard::W: 
       { 
        Me.setRotation(0); 
        break; 
       } 
       case sf::Keyboard::D: 
       { 
        Me.setRotation(90); 
        break; 
       } 
       case sf::Keyboard::S: 
       { 
        Me.setRotation(180); 
        break; 
       } 
       case sf::Keyboard::A: 
       { 
        Me.setRotation(270); 
        break; 
       } 
       case sf::Keyboard::Escape: 
       { 
        game.close(); 
        break; 
       } 
       case sf::Keyboard::Space: 
       { 
        if(Me.get_ptr()->isFired()==false)Me.shoot(); 
        break; 
       } 
       case sf::Keyboard::LControl: 
       { 
        stop=true; 
        break; 
       } 
       default: 
        break; 
      } 
     } 
    } 

    if(!stop) 
     Me.update(); 

    if(Me.get_ptr()->isFired()==true) 
     Me.get_ptr()->update(); 

    // Output on the window 
    game.clear(); 
    Me.show(); 
    if(Me.get_ptr()->isFired()==true) 
     Me.get_ptr()->show(); 
    game.display(); 
} 
return 0; 
} 

#include<iostream> 

bool stop; 

sf::RenderWindow game(sf::VideoMode(400, 400), "SFML"); 

sf::Texture myTankTexture; 
sf::Texture bulletTexture; 

void init(){ 

srand(time(NULL)); 

stop = true; 

game.setVerticalSyncEnabled(true); 
game.setFramerateLimit(60); 

if(!myTankTexture.loadFromFile("tank.jpg")) 
{ 
    std::cout<<"eroare la textura tank"<<std::endl; 
} 
myTankTexture.setSmooth(true); 

if(!bulletTexture.loadFromFile("bullet.jpg")) 
{ 
    std::cout<<"bullet texture error"<<std::endl; 
} 
bulletTexture.setSmooth(true); 
} 

sf::Vector2f rotationToDirection(int rotation){ 

sf::Vector2f dir; 
switch (rotation) 
{ 
    case 0: 
    { 
     dir.x=0.0; 
     dir.y=-1.0; 
     break; 
    } 
    case 90: 
    { 
     dir.x=1.0; 
     dir.y=0.0; 
     break; 
    } 
    case 180: 
    { 
     dir.x=0.0; 
     dir.y=1.0; 
     break; 
    } 
    case 270: 
    { 
     dir.x=-1.0; 
     dir.y=0.0; 
     break; 
    } 
} 
return dir; 
} 

init.h bullet.h

#include "init.h" 

class bullet{ 

protected: 
sf::Sprite bulletSprite; 
sf::FloatRect boundingBox; 
bool isBFired = false; 
public: 
bullet(sf::Sprite); // constructor 
~bullet(){isBFired=false;} 

sf::FloatRect get_boundingBox(){return boundingBox;} 
bool isFired(){if(isBFired)return true;else return false;} 
int collision(); 

void del(){delete this;} 
void update(); 
void show(){game.draw(bulletSprite);} 
}; 

bullet::bullet(sf::Sprite sprite){ 

isBFired = true; 

bulletSprite.setTexture(bulletTexture); 
bulletSprite.setOrigin(2.5,5.0); 
bulletSprite.setRotation(sprite.getRotation()); 
    bulletSprite.setPosition(sprite.getPosition().x+rotationToDirection(bulletSprite.getRotation()).x*5.0,sprite.getPosition().y+rotationToDirection(bulletSprite.getRotation()).y*5.0); 

boundingBox = bulletSprite.getLocalBounds(); 
} 

int bullet::collision(){ 

if(bulletSprite.getPosition().x < 0 
    || bulletSprite.getPosition().x > 400 
    || bulletSprite.getPosition().y > 400 
    || bulletSprite.getPosition().y < 0)return 1; 
else 
    return 0; 
} 

void bullet::update(){ 

      bulletSprite.move(rotationToDirection(bulletSprite.getRotation()).x*6.0,rotationToDirection(bulletSprite.getRotation()).y*6.0); 

if(collision()==1) 
    delete this; 
else 
    boundingBox = bulletSprite.getLocalBounds(); 
} 

あなたはどうかを確認、私の知る限り見るようにテクスチャ矩形を設定されていませんtank.h

#include "bullet.h" 

class tank{ 

protected: 
sf::FloatRect boundingBox; 
sf::Sprite tankSprite; 

bullet *mBullet; 
public : 
tank(); // constructor 

bullet * get_ptr(){return mBullet;} 
sf::FloatRect get_boundingBox(){return boundingBox;} 

void setRotation(float); 
void show(){game.draw(tankSprite);} 
void update(); 
void shoot(){mBullet = new bullet(tankSprite);} 
}; 

tank::tank(){ 

tankSprite.setTexture(myTankTexture); 
tankSprite.setOrigin(20,20); 
tankSprite.setRotation(0); 
tankSprite.setPosition(200,200); 

boundingBox = tankSprite.getLocalBounds(); 
} 

void tank::update(){ 

    tankSprite.move(rotationToDirection(tankSprite.getRotation()).x*3,rotationToDirection(tankSprite.getRotation()).y*3); 

boundingBox = tankSprite.getLocalBounds(); 
} 

void tank::setRotation(float rotation){ 

tankSprite.setRotation(rotation); 
} 
+1

最小限の検証可能な例を示してください。また、あなたのコードは "code smells" *でいっぱいです(例えば、newを使って箇条書きを作成し、それをメンバポインタフィールドに割り当てる、非const ptrなどで箇条書きを返すなど)* –

+0

@VittorioRomeoアプリ私は私が撃つことができるので、私は周りを移動することができますと私は弾丸が意図したように見えるが、私は自分自身を見ることができない、私はどこから始まり、弾丸。私はあなたが最小限の検証可能な例を意味するものを正しく理解しているかどうかはわかりません。 –

+0

@Lアレックス:コピーして、自分のシステムに簡単に貼り付けることができるもの。 –

答えて

0

これはあなたを助けます。あなたは、あなたのタンクのスプライト試して設定した場合:

tankSprite.setTexture(myTankTexture、真の);

trueを渡すと、テクスチャの矩形がスプライトのサイズにリセットされます。詳細については、SFML docsを参照してください。

また、init()を呼び出す前にTankクラスを作成すると、myTankTextureが空になり、タンクコンストラクタがそれを使用し、テクスチャが後で読み込まれるようになります。

あなたの言語で言うように、「multa bafta」:)

+0

残念ながら、それはうまくいきませんでしたが、とにかく感謝しています。 –

+0

私は今問題を見る、ごめんなさい。これらの行を交換してください: tank Me; init(); テクスチャがロードされていないタンクを作成しているので、setTextureは空のテクスチャを設定します。 :) –

+0

ああ、それはうまくいきました、おかげさまで、愚かなことに気付かなかった、何とか私はそれらを交換しました。 –

関連する問題