[英]SFML Place Sprite on center
I have been trying to fix this problem for about an hour or more... But couldn't find any usefull answers. 我一直在尝试解决这个问题大约一个小时或更长时间...但是找不到任何有用的答案。 I am trying to place a sprite on the center of the window, but it shows up on TOP_LEFT.
我正在尝试将一个精灵放置在窗口的中心,但是它显示在TOP_LEFT上。 Here is the constructor for my class and as you can see I am dividing surface.width and surface.height by 2
这是我的类的构造函数,您可以看到我将surface.width和surface.height除以2
Spaceship::Spaceship(sf::RenderWindow& game_window){
auto surface = game_window.getSize();
signed int ss_x = surface.x/2;
signed int ss_y = surface.y/2;
int ss_width = 128;
int ss_height = 128;
int ss_radius = ss_width/2;
}
///////////////////////////////////////////
// For displaying the sprite on window //
///////////////////////////////////////////
void Spaceship::drawsprite(sf::RenderWindow& game_window){
sf::Texture ship;
if (!ship.loadFromFile(resourcePath() + "space-shuttle-64.png")) {
return EXIT_FAILURE;
}
sf::Sprite ss_sprite(ship);
ss_sprite.setPosition(ss_x, ss_y);
game_window.draw(ss_sprite);
}
I have also tried with: 我也尝试过:
auto surface = game_window.RenderWindow::getSize();
signed int ss_x = surface.x/2;
signed int ss_y = surface.y/2;
but that didn't help either. 但这也无济于事。
I have tried to print the variables which are defined inside the constructor and I got 0 on all of them. 我试图打印在构造函数内部定义的变量,但所有变量都为0。 So my problems seems to be the accessing problem.
所以我的问题似乎是访问问题。 but there were no errors or warnings that have told me that.
但是没有任何错误或警告告诉我。
this is the header file: 这是头文件:
#ifndef Spaceship_hpp
#define Spaceship_hpp
#include <iostream>
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include <stdio.h>
using namespace std;
class Spaceship {
public:
Spaceship();
Spaceship(sf::RenderWindow&);
~Spaceship();
void moveship(char);
void drawsprite(sf::RenderWindow&);
private:
signed int ss_x, ss_y;
unsigned int ss_speed;
int ss_width, ss_height, ss_radius;
};
#endif /* Spaceship_hpp */
You are not initialising your attributes in your constructor correctly. 您没有在构造函数中正确初始化属性。
Spaceship::Spaceship(sf::RenderWindow& game_window){
auto surface = sf::VideoMode::getDesktopMode();
signed int ss_x = surface.width/2;
signed int ss_y = surface.height/2;
int ss_width = 128;
int ss_height = 128;
int ss_radius = ss_width/2;
}
should be 应该
Spaceship::Spaceship(sf::RenderWindow& game_window){
auto surface = sf::VideoMode::getDesktopMode();
ss_x = surface.width/2;
ss_y = surface.height/2;
ss_width = 128;
ss_height = 128;
ss_radius = ss_width/2;
}
Declaring variables in the body of the class means they are seen globally by the class, if you redeclare a variable in the constructor, it will take over the role from the global variable. 在类的主体中声明变量意味着它们在类中全局可见,如果在构造函数中重新声明变量,它将从全局变量中代替角色。 This is called variable shadowing .
这称为可变阴影 。 All modifications to the variable will work, but once you leave the scope of your constructor/function/method, then you lose the information since your attribute variable wasn't modified.
对变量的所有修改都可以使用,但是一旦离开构造函数/函数/方法的范围,由于未修改属性变量,您将丢失信息。
More on scopes: http://en.cppreference.com/w/cpp/language/scope 有关范围的更多信息: http : //en.cppreference.com/w/cpp/language/scope
More on variable shadowing: https://en.wikipedia.org/wiki/Variable_shadowing?oldformat=true 有关可变阴影的更多信息: https : //en.wikipedia.org/wiki/Variable_shadowing? oldformat =true
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.