C++ 增强循环不工作的原因

C++ 增强循环不工作的原因,c++,sfml,C++,Sfml,主要的问题是,为什么常规for循环有效而增强的for循环无效。下面是对不工作的增强for循环的描述 解决了!!!我将把我的描述留给下面的问题,但是我对代码做了一些修改,发现你不能通过增强for循环移动精灵。您需要一个实际的for循环。看看我的UpdateScrites函数(我刚刚实现的)workingUpdateSprites函数 (你可能可以忽略之前我遇到的问题) SFML根本没有把我的精灵移到右边。我有一个UpdateScrites函数,它将空闲帧向右移动(出于实验原因),然后由播放器更新函

主要的问题是,为什么常规for循环有效而增强的for循环无效。下面是对不工作的增强for循环的描述

解决了!!!我将把我的描述留给下面的问题,但是我对代码做了一些修改,发现你不能通过增强for循环移动精灵。您需要一个实际的for循环。看看我的UpdateScrites函数(我刚刚实现的)workingUpdateSprites函数

(你可能可以忽略之前我遇到的问题) SFML根本没有把我的精灵移到右边。我有一个UpdateScrites函数,它将空闲帧向右移动(出于实验原因),然后由播放器更新函数调用,然后由引擎更新函数调用。精灵通过将精灵返回到主机并简单地绘制来显示

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.cpp
#包括“Player.h”
#包括“发动机.h”
#包括
#包括
#包括
#定义_MYDEBUG1
void Player::updateSprites(){//在游戏循环的每次迭代中更改每个玩家精灵的x和y变量
用于(精灵i:空闲)
i、 移动(1,0);
}
//idk为什么for循环有效而增强循环无效。
void Player::workingUpdateSprites(){//在游戏循环的每次迭代中更改每个玩家精灵的x和y变量
for(int i=0;igetDirection()==0){//还确定要发送哪个精灵以及在哪个帧
int-amtFrameTimePerSprite=frameIdleMaxCounterVal/ARRAYSIZE(idle);//获取每个精灵的帧时间量
if(frameIdleMaxCounterVal-frameIdleCounter>amtFrameTimePerSprite)//将frameIdleCount除以amtFrameTimePerSprite以获得准确的索引
返回空闲[(内部)楼层(frameIdleCounter/amtFrameTimePerSprite)];
其他的
返回空闲[阵列化(空闲)-1];
}
返回空闲[0];
}
Player::Player(intx,inty){//构造超类和精灵
frameIdleMaxCounterVal=240;
///////-----------------------------------------------------开始建造所有玩家精灵----------------------------------------////////////////
//将空闲精灵构造到数组中
for(int i=0;i
Player.h

#包括
#包括“Movement.h”
#ifndef(玩家)
#定义玩家
使用名称空间sf;
职业选手{
私人:
运动*运动;
精灵空闲[4]、mvtUp[8]、mvtDown[8]、mvtLeft[8]、mvtRight[8];
纹理整理[4],tMvtUp[8],tMvtDown[8],tMvtLeft[8],tMvtRight[8];
int frameIdleCounter=1,frameIdleMaxCounterVal=20;
void updateVisuals();
void updateCounters();
void updateSprites();
公众:
Sprite getPlayerSprite();
int getX();
int getY();
无效更新();
玩家(整数x,整数y);
};
#恩迪夫

Engine.cpp

#包括“Engine.h”
#包括
#包括
#定义暂停显示
#定义_MYDEBUGf
bool引擎::init(){
#ifdef_MYDEBUG
freopen(“conin$”,“r”,圣
#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
#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() {
}
#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
for (Sprite& i : idle)
      i.move(1, 0);