在学习了C++的基础知识后,我目前开始使用SFML.我已经了解了Arrays,References以及它之前的所有内容,但却努力掌握使用类的概念.
在SFML中,我创建了一个简单的sprite移动程序,但是,我想将这些信息移到一个类中(假设它将被称为"Player").我搞砸了很多但是我无法让它发挥作用.
我已经尝试在类中创建一个函数来检查播放器输入,但我无法访问我在main中创建的精灵.我想将与玩家相关的所有内容移到Player类中,但需要一些建议.
这样做的正确方法是什么?(请不要说回去学习课程,这是我想要了解的地方!)
main.cpp中
#include <SFML/Graphics.hpp>
#include <string>
#include <iostream>
int main()
{
//character position
enum Direction{ Down, Left, Right, Up };
sf::Vector2i source(1, Down);
//window
sf::RenderWindow window(sf::VideoMode(1200, 700), "Testing");
window.setKeyRepeatEnabled(false);
//player character
sf::Texture pTexture;
sf::Sprite pSprite;
if(!pTexture.loadFromFile("image/playerSprite.png"))
std::cout << "Texture Error" << std::endl;
pSprite.setTexture(pTexture);
pSprite.setScale(1.5f, 1.5f);
//game loop
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) //move up
{
source.y = Up;
pSprite.move(0, -0.2);
//animation
source.x++;
if(source.x * 32 …Run Code Online (Sandbox Code Playgroud)