2017-05-04 132 views
-2

主要問題是,爲什麼常規循環工作和增強for循環不工作。不工作增強for循環的描述在下面。增強for循環不工作

已解決!我將把我的描述留給下面的問題,但我做了一些周圍的代碼,發現你不能通過增強for循環移動精靈。你需要一個實際的循環。看看我的updateSprites函數(我剛剛實現的)workingUpdateSprites函數。

(之前的問題我以前可能可以忽略它) SFML不會將我的精靈向右移動。我有一個updateSprites函數,將空閒幀移到右側(由於實驗原因),然後由播放器更新函數調用,然後由引擎更新函數調用。通過將精靈返回到主引擎並簡單地繪製精靈來顯示精靈。

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

即使您的問題已解決,您應該考慮編輯您的問題。我這樣說,因爲問題標題實際上在我看來非常有用(不要憎恨我們,* XY不工作*是第一個推入谷歌的),但這個問題本身並不是很有幫助。如果將其減少到實際問題:非工作增強型環路與工作正常環路,沒有所有周圍的代碼,它可以爲其他類似問題的人提供一個很好的起點。旁註:我沒有-1這個,但是因爲它站立-1可能是理所當然的。 – grek40

回答

1

你絕對可以使用範圍爲基礎與精靈的循環,你只是使用它們不當,通過修改您的循環內的臨時副本而不是原始的精靈。嘗試使用參考代替:

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