2017-03-03 15 views
-2

Ich versuche ein C++ sfml-Spiel zu machen, aber die Schaltfläche, die ich erstellt habe, scheint nicht zu funktionieren, und ich habe es auf gute Autorität, die es sollte. Bitte hilf mir, der Button ist übrigens, "wenn (mouseposx> 1366/2.0 & & mouseposx < 1083) ...".C++ SFML Button funktioniert nicht:/

Haupt menu.h

#include <SFML/Graphics.hpp> 
#include <iostream> 
using namespace sf; 
using namespace std; 
#pragma once 

class main_menu 
{ 
public: 
void startmenu(RenderWindow &gameDisplay, Font &font1, bool &MainMenu, int &mouseposx, int &mouseposy) 
{ 
    gameDisplay.setMouseCursorVisible(true); 
    RectangleShape start(Vector2f(400, 100)); 
    start.setFillColor(Color(0, 0, 0)); 
    start.setOutlineThickness(20); 
    start.setOutlineColor(Color(255, 255, 255)); 

    FloatRect startRect = start.getLocalBounds(); 
    start.setOrigin(startRect.width/2, startRect.height/2); 
    start.setPosition(Vector2f(1366/2.0f, 768 - 450.0f)); 



    Text starttext; 
    starttext.setCharacterSize(100); 
    starttext.setFont(font1); 
    starttext.setString("DODGE"); 
    starttext.setFillColor(Color(255, 255, 255)); 
    starttext.setStyle(Text::Bold); 

    FloatRect starttextRect = starttext.getLocalBounds(); 
    starttext.setOrigin(starttextRect.width/2, starttextRect.height/2); 
    starttext.setPosition(Vector2f(1366/2.0f - 30, 768 - 700.0f)); 



    while (MainMenu == true) 
    { 
     /* 
     if (Keyboard::isKeyPressed(Keyboard::P)) 
     { 
      MainMenu = false; 

     } 
     */ 

     mouseposx = (Mouse::getPosition(gameDisplay).x); 
     mouseposy = (Mouse::getPosition(gameDisplay).y); 
     cout << "Position of mouse y is" << mouseposy << endl; 
     cout << "Position of mouse x is" << mouseposx << endl; 


      if (mouseposx > 1366/2.0 && mouseposx < 1083) 
       if (mouseposy > 768 - 450 && mouseposy < 868) 
        if (Mouse::isButtonPressed(Mouse::Left)) 
         MainMenu = false; 



     if (Keyboard::isKeyPressed(Keyboard::Q)) 
     { 
      MainMenu == false; 
      gameDisplay.close(); 

     } 

     gameDisplay.draw(start); 
     gameDisplay.draw(starttext); 
     gameDisplay.display(); 
    } 


    } 
}; 

things.h

#include <SFML/Graphics.hpp> 
#include <iostream> 
#include <cstdlib> 
using namespace sf; 
using namespace std; 
#pragma once 
int RandomXpos = 0; 

class Thing 
{ 
public: 
void thingspawn(RenderWindow &gameDisplay, int &enemystartx, int &enemystarty, int &enemywidth, int &enemyheight, RectangleShape &thing, int enemyspeed) 
{ 
    thing.setFillColor(Color(255,255,255)); 
    if (thing.getPosition().y < 800) 
     thing.move(0.0f, 0.4f); 
    if (thing.getPosition().y >= 800) 
    { 
     RandomXpos = rand() % 1000; 
     thing.setPosition(RandomXpos, -200); 
    } 

    } 
}; 

main.cpp

#include <iostream> 
#include <SFML/Graphics.hpp> 
#include "things.h" 
#include "Main Menu.h" 
#include <cstdlib> 
#include <Windows.h> 
using namespace std; 
using namespace sf; 




int main() 
{ 
//FreeConsole(); 
RenderWindow gameDisplay(VideoMode(1366, 768), "Game"); 
gameDisplay.clear(); 
int enemystarty = -200; 
int enemystartx = 300; 
int enemyheight = 30; 
int enemywidth = 300; 
int enemyspeed = 0.3; 
int enemycount = 1; 
int dodged = 0; 
int blockcolor = 0; 
bool MainMenu = true; 

int mouseposx = 0; 
int mouseposy = 0; 

Font font1; 
if (!font1.loadFromFile("OCRAEXT.ttf")) 
{ 
    //error... 
} 

/* 
RectangleShape player(Vector2f(200, 50)); 
player.setFillColor(Color(0, 0, 255)); 
player.setPosition(400, 700); 
*/ 

Texture playertexture; 
if (!playertexture.loadFromFile("whiter_arrow.jpg")) 
{ 
    //error... 
} 

Sprite player; 
player.setTexture(playertexture); 
player.setPosition(300, 600); 

RectangleShape thing(Vector2f(enemywidth, enemyheight)); 
thing.setPosition(300, -200); 

main_menu object; 
object.startmenu(gameDisplay, font1, MainMenu, mouseposx, mouseposy); 


while (gameDisplay.isOpen()) 
{ 
    Event evnt; 
    while (gameDisplay.pollEvent(evnt)) 
    { 
     switch (evnt.type) 
     { 
     case Event::Closed: 
      gameDisplay.close(); 
     case Event::KeyPressed: 
      if (Keyboard::isKeyPressed(Keyboard::Q)) 
       gameDisplay.close(); 

     } 

    } 


    while (MainMenu == false) 
    { 
     //gameDisplay.setMouseCursorVisible(false); 

     if (Keyboard::isKeyPressed(Keyboard::Right)) 
      if (player.getPosition().x < 1000) 
      { 
       player.move(0.3f, 0.0f); 
      } 

     if (Keyboard::isKeyPressed(Keyboard::Left)) 
      if (player.getPosition().x > 50) 
      { 
       player.move(-0.3f, 0.0f); 
      } 
     if (Keyboard::isKeyPressed(Keyboard::Q)) 
      gameDisplay.close(); 


     Thing thingobject; 
     thingobject.thingspawn(gameDisplay, enemystartx, enemystarty, enemywidth, enemyheight, thing, enemyspeed); 


     gameDisplay.clear(); 
     gameDisplay.draw(player); 
     gameDisplay.draw(thing); 
     gameDisplay.display(); 
    } 



    } 
} 

Antwort

0

Sie können versuchen, zu überprüfen, zu setzen, wenn die Taste in Ereignisschleife angeklickt wird