增强了循环不工作

时间:2017-05-04 02:54:14

标签: c++ sfml

主要问题是,为什么常规for循环工作和增强的for循环不起作用。无效增强for循环的描述如下。

解决!!!我将把我的描述留给下面的问题,但我做了一些代码,并发现你不能通过增强的for循环移动sprite。你需要一个实际的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

1 个答案:

答案 0 :(得分:1)

你绝对可以使用基于范围的for sprite,你只是错误地使用它们,通过修改循环中的临时副本而不是原始精灵。请尝试使用引用:

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