2017-05-04 2 views
-2

Die Hauptfrage ist, warum die reguläre for-Schleife funktioniert und die erweiterte for-Schleife nicht. Die Beschreibung der nicht funktionierenden erweiterten for-Schleife ist unten aufgeführt.Enhanced für Schleife funktioniert nicht

Gelöst !!! Ich werde meine Beschreibung auf das unten stehende Problem beschränken, aber ich habe etwas mit dem Code herumgespielt und herausgefunden, dass man Sprites nicht über erweiterte For-Loops bewegen kann. Sie benötigen eine tatsächliche for-Schleife. Schau dir meine updateSprites Funktion an (was ich gerade implementiert habe) funktioniereUpdateSprites Funktion.

(vorheriges Problem hatte ich vor dir PROBABLY kann es ignorieren) SFML verschiebt mein Sprite überhaupt nicht nach rechts. Ich habe eine updateSprites Funktion, die leere Frames nach rechts bewegt (aus experimentellen Gründen), die von der Player-Update-Funktion aufgerufen wird, und dann von der Engine-Update-Funktion. Die Sprites werden angezeigt, indem ein Sprite an die Haupt-Engine zurückgegeben und einfach gezeichnet wird.

Player.cpp 
<i> 
#include "Player.h" 
#include "Engine.h" 
#include <math.h> 
#include <windows.h> 
#include <string> 
#define _MYDEBUG1 



void Player::updateSprites() {       //Changes x and y variables for EVERY player sprite upon each iteration of the game loop 
    for (Sprite i : idle) 
      i.move(1, 0); 
} 
//idk why the for loop works and the enhanced one doesn't. 
void Player::workingUpdateSprites() {       //Changes x and y variables for EVERY player sprite upon each iteration of the game loop 
    for (int i = 0; i < ARRAYSIZE(idle); i++) 
      idle[i].move(1, 0); 
} 

void Player::update() {          //God method that updates the player class {accessed by main engine} 
    updateVisuals(); 
} 

void Player::updateVisuals() { 
    updateSprites(); 
} 

Sprite Player::getPlayerSprite() {               //return image of player sprite to get printed in the engine file 
    if (movement->getDirection() == 0) {              //also determines which sprite to send and at what frame 
      int amtFrameTimePerSprite = frameIdleMaxCounterVal/ARRAYSIZE(idle);    //gets amount of frame time per sprite 
      if (frameIdleMaxCounterVal - frameIdleCounter > amtFrameTimePerSprite)    //divides frameIdleCount by amtFrameTimePerSprite to get exact index 
       return idle[(int)floor(frameIdleCounter/amtFrameTimePerSprite)]; 
      else 
       return idle[ARRAYSIZE(idle) - 1]; 
    } 
    return idle[0]; 
} 

Player::Player(int x, int y) {    //Constructs superclass(es) and sprites 
    frameIdleMaxCounterVal = 240; 

///////-----------------------------------------------------CONSTRUCION OF ALL PLAYER SPRITES BEGINS----------------------------------------//////////////// 
    //constructs idle sprites into array 
    for (int i = 0; i < ARRAYSIZE(tIdle); i++) { 
      if (i == 3) { 
       if (!tIdle[i].loadFromFile("resources\\player\\playerSass\\playerSass1.png")) { 
        throw "Could not load player idle frames"; 
       } 
      } 
      else { 
       if (!tIdle[i].loadFromFile("resources\\player\\playerSass\\playerSass" + std::to_string(i) + ".png")) 
        throw "could not load player idle frames"; 
      } 
      idle[i].setTexture(tIdle[i]); 
      idle[i].setPosition(x, y); 
    } 
    //constructs movement up sprites into array 
    for (int i = 0; i < ARRAYSIZE(tMvtUp); i++) { 
      if (!tMvtUp[i].loadFromFile("resources\\player\\playerSass\\mvtUp\\playerMvtUp" + std::to_string(++i) + ".png")) 
       throw "could not load player movement up frames"; 
      mvtUp[i].setTexture(tMvtUp[i]); 
      mvtUp[i].setPosition(x, y); 
    } 
    //constructs movement down sprites into array 
    for (int i = 0; i < ARRAYSIZE(tMvtDown); i++) { 
      if (!tMvtDown[i].loadFromFile("resources\\player\\playerSass\\mvtDown\\playerMvtDown" + std::to_string(++i) + ".png")) 
       throw "could not load player movement down frames"; 
      mvtDown[i].setTexture(tMvtDown[i]); 
      mvtDown[i].setPosition(x, y); 
    } 
    //constructs movement left sprites into array 
    for (int i = 0; i < ARRAYSIZE(tMvtLeft); i++) { 
      if (!tMvtLeft[i].loadFromFile("resources\\player\\playerSass\\mvtLeft\\playerMvtLeft" + std::to_string(++i) + ".png")) 
       throw "could not load player movement left frames"; 
      mvtLeft[i].setTexture(tMvtLeft[i]); 
      mvtLeft[i].setPosition(x, y); 
    } 
    //constructs movement down sprites into array 
    for (int i = 0; i < ARRAYSIZE(tMvtRight); i++) { 
      if (!tMvtRight[i].loadFromFile("resources\\player\\playerSass\\mvtRight\\playerMvtRight" + std::to_string(++i) + ".png")) 
       throw "could not load player movement right frames"; 
      mvtRight[i].setTexture(tMvtRight[i]); 
      mvtRight[i].setPosition(x, y); 
    } 
    ///////------------------------------------CONSTRUCTION OF ALL PLAYER ANIMATION FRAMES END---------------------------------------------------////////// 

    //////-------------------------------------CONSTRUCIION OF MOVEMENT----------------------------------------------- 
    movement = new Movement(x, y); 
} 
</i> 

Player.h

#include <SFML\Graphics.hpp> 
#include "Movement.h" 
#ifndef _PLAYER_H 
#define _PLAYER_H 

using namespace sf; 

class Player { 
private: 
    Movement *movement; 
    Sprite idle[4], mvtUp[8], mvtDown[8], mvtLeft[8], mvtRight[8]; 
    Texture tIdle[4], tMvtUp[8], tMvtDown[8], tMvtLeft[8], tMvtRight[8]; 
    int frameIdleCounter = 1, frameIdleMaxCounterVal = 20; 

    void updateVisuals(); 
    void updateCounters(); 
    void updateSprites(); 
public: 
    Sprite getPlayerSprite(); 
    int getX(); 
    int getY(); 

    void update(); 

    Player(int x, int y); 
}; 
#endif 

Engine.cpp

#include "Engine.h" 
#include <SFML\Graphics.hpp> 
#include <iostream> 
#define _PAUSEDISPLAYl 
#define _MYDEBUGf 

bool Engine::init() { 
#ifdef _MYDEBUG 
    freopen("conin$", "r", stdin); 
    freopen("conout$", "w", stdout); 
    freopen("conout$", "w", stderr); 
#endif 
    window = new sf::RenderWindow(sf::VideoMode(800, 600, 32), "RPG", sf::Style::Close | sf::Style::Resize); 
    window->setFramerateLimit(60); 
    player = new Player(50, 50); 
    if (!window) 
      return false; 
    return true; 
} 

void Engine::mainLoop() { 
    //Loop until window is closed 
    while (window->isOpen()) { 
      processInput(); 
      update(); 
      window->clear(sf::Color::Black); 
      renderFrame(); 
      window->display(); 
#ifdef _PAUSEDISPLAY 
      system("pause"); 
#endif 
    } 
} 

void Engine::processInput() { 
    sf::Event evt; 
    //loops through all window events 
    while (window->pollEvent(evt)) { 

      //window events 
      switch (evt.type) { 
      case sf::Event::Closed: 
       window->close(); 
      case sf::Event::Resized: 
       std::cout << "width " << evt.size.width << " height " << evt.size.height; 
       break; 
      } 
    } 
} 

void Engine::update() {    //the actual god method 
    player->update(); 
} 

void Engine::renderFrame() {    //calls object sprites to then be printed/displayed on the screen 
    window->draw(player->getPlayerSprite()); 
} 

void Engine::go() { 
    if (!init()) 
      throw "Initialization of Engine has Failed"; 
    mainLoop(); 
} 
Engine::Engine() { 
} 


Engine::~Engine() { 
} 

engine.h

#ifndef _ENGINE_H 
#define _ENGINE_H 

#include <SFML\Graphics.hpp> 
#include "Player.h" 
#include "Input.h" 

class Engine { 
private: 
    sf::RenderWindow* window; 
    Player *player; 

    bool init(); 

    void mainLoop(); 

    void processInput(); 

    void update(); 

    void renderFrame(); 

public: 
    Engine(); 
    ~Engine(); 

    void go(); 

    Input input[4]; 
}; 

#endif 

+0

Auch wenn Ihr Problem gelöst ist, sollten Sie in Betracht ziehen, Ihre Frage zu bearbeiten. Ich sage das, weil der Fragetitel meiner Meinung nach ziemlich nützlich ist (hasst mich nicht Jungs, * XY funktioniert nicht * ist die erste Sache, die man in Google schieben kann), aber die Frage selbst ist nicht sehr hilfreich. Wenn Sie es auf das eigentliche Problem reduzieren: nicht funktionierende erweiterte Schleife im Vergleich zur normalen Schleife, ohne den umgebenden Code, kann es einen guten Ausgangspunkt für andere mit einem ähnlichen Problem bilden. Randnotiz: Ich habe das nicht gemacht, aber wie es aussieht, ist die -1 wahrscheinlich verdient. – grek40

Antwort

1

Sie absolut können Verwendung bereichsbasierte for-Schleifen mit Sprites, waren Sie gerade sie falsch verwendet wird, durch Ändern einer temporären Kopie in Ihrer Schleife statt das ursprüngliche Sprite. Verwenden Sie stattdessen eine Referenz:

for (Sprite& i : idle) 
     i.move(1, 0);