【发布时间】:2023-03-09 21:25:01
【问题描述】:
主要问题是,为什么常规 for 循环有效而增强型 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>
播放器.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() {
}
引擎.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
【问题讨论】:
-
即使您的问题已经解决,您也应该考虑编辑您的问题。我这么说是因为在我看来问题标题实际上非常有用(伙计们不要恨我,XY 不工作 是推入谷歌的第一件事)但问题本身并不是很有帮助.如果您将其简化为实际问题:不工作的增强循环与工作正常循环,没有所有周围的代码,它可以为其他有类似问题的人提供一个很好的起点。旁注:我没有 -1 这个,但就目前而言 -1 可能是应得的。