Game chess sprite not moving across board

#include "Tile.h"  Tile::Tile() : sf::RectangleShape(sf::Vector2f(105, 105)), m_isHighlighted(false)  {     if (!m_cBuffer.loadFromFile("penclick.wav")) {         std::cout << "Failed to load penclick.wav" << std::endl;     } }  bool Tile::isTileHighlighted() const {     return m_isHighlighted; }  void Tile::turnOffHighlight(){     m_clickSound.setBuffer(m_cBuffer);     m_clickSound.setVolume(15);     m_clickSound.play();     setPosition(sf::Vector2f(getPosition().x - 5, getPosition().y - 5));     setSize(sf::Vector2f(getSize().x + 10, getSize().y + 10));     setOutlineThickness(0);     m_isHighlighted = false;  }  void Tile::highlightTile() {      m_clickSound.setBuffer(m_cBuffer);     m_clickSound.setVolume(15);     m_clickSound.play();     setPosition(sf::Vector2f(getPosition().x + 5, getPosition().y + 5));     setSize(sf::Vector2f(95, 95)); //decrease size to be able to render border without clashes     setOutlineThickness(5);     setOutlineColor(sf::Color::Yellow);      m_isHighlighted = true; }  Tile::~Tile(){ }   #include "GamePieces.h"  float GamePieces::m_speed = 52.5;  GamePieces::GamePieces() { }  GamePieces::GamePieces(const std::string& type, const Color& c, const sf::Vector2f& position) : m_type(type), m_col(c), m_position(position) {     std::string filePath = ((c == Color::White) ? "w_" : "b_") + type + ".png";     std::cout << filePath << std::endl;      if (!m_gamePiece.loadFromFile(filePath)) {         std::cout << "Failed" << std::endl;      }     else {         m_gamePieceSprite.setTexture(m_gamePiece);         m_gamePieceSprite.setOrigin(sf::Vector2f(m_gamePiece.getSize().x / 2, m_gamePiece.getSize().y / 2));         m_gamePieceSprite.setPosition(position);     } }  sf::Sprite GamePieces::getPieceSprite() const {         return m_gamePieceSprite; }  bool GamePieces::isWhite() const {     return (m_col==Color::White); }  sf::Vector2f GamePieces::getPosition() const {     return m_position; }  std::string GamePieces::getPieceType() const {     return m_type; }  void GamePieces::movePiece(const Tile& tile, float dt) {     sf::Vector2f tileCoords = sf::Vector2f(tile.getPosition().x + 52.5, tile.getPosition().y + 52.5);     std::cout << tileCoords.y << std::endl;     while (m_gamePieceSprite.getPosition().y >= tileCoords.y) {         m_gamePieceSprite.setPosition(sf::Vector2f(m_gamePieceSprite.getPosition().x, m_gamePieceSprite.getPosition().y - (m_speed*dt)));         std::cout << m_gamePieceSprite.getPosition().y << std::endl;     }     m_position = sf::Vector2f(tileCoords.x, tileCoords.y); }  void GamePieces::setPos(const sf::Vector2f& pos)  {     m_position = pos; }       GamePieces::~GamePieces() { }   void Grid::update(Windows& wind, const sf::Event& event, const Team& w, const Team& b, float dt) {     if (sf::Mouse::isButtonPressed(sf::Mouse::Left)) {          try {             sf::Vector2f mousePosFloat = static_cast<sf::Vector2f>(sf::Mouse::getPosition(*wind.getWindows()));              if ((mousePosFloat.x > (wind.getWindows()->getSize().x) || mousePosFloat.x < 0)) {                 throw 99;             }             if ((mousePosFloat.y > wind.getWindows()->getSize().y || mousePosFloat.y < 0)) {                 throw 99;             }              sf::Vector2f rowCol = windowCoordsToRowCol(mousePosFloat);             Tile& tile = m_tileSet[(int)rowCol.x][(int)rowCol.y];             //std::cout << tile.getPosition().x << ", " << tile.getPosition().y << std::endl;             GamePieces* gamePiece = getGamePieceOnTile(rowCol.x, rowCol.y, w, b);              if (tile.isTileHighlighted()) {                 tile.turnOffHighlight(); //moves square back in place and turns off outline              }                else if (gamePiece != nullptr && gamePiece->isWhite() && !(isOneTileHighlighted(w))) { //only highlight tile if its white and no one in the team is                                                                                                               //highlited                 tile.highlightTile();             }             else if (isOneTileHighlighted(w)&& (gamePiece == nullptr || !gamePiece->isWhite())) {                  if (gamePiece == nullptr) {//space clicked on is empty                     getHighlightedGamePiece(w).movePiece(tile, dt);                  }                 else if (!gamePiece->isWhite()) {                     //TODO move to space and eat opposing gamePiece                 }             }             else {                 if (!ebuffer.loadFromFile("error.wav")) {                     std::cout << "Failed to load error.wav" << std::endl;                 }                 errorSound.setBuffer(ebuffer);                 errorSound.setVolume(8);                 errorSound.play();             }              //TODO find way of deleting the gamePiece pointer         }         catch (int x) {             std::cout << "hello" << std::endl;         }     } 

void Field::update(sf::Event& event) {
float elapsedTime = m_elapsed.getElapsedTime().asSeconds(); if (elapsedTime >= frameTime) { if (m_secondClock.getElapsedTime().asSeconds() >= 1.0f) { elapsedFrames = 0; m_secondClock.restart(); } elapsedFrames++; m_elapsed.restart(); while (m_windows.getWindows()->pollEvent(event)) { // max 60 fps - cant go faster m_windows.Update(event); m_chessBoard.update(m_windows, event, m_wTeam, m_bTeam, elapsedTime); } } }

So for now I'm just trying to select a game piece and then move it to an empty tile, and according to the debugger all of this executes fine as per the flow of the program. I event print out the location of the sprite as it makes its way to the new tile and it seems right. The "dt" time variable in this case is just the time between updates that exceeds 16 ms (so this is a fixed time step). I'm also wondering how I can make it so that it goes exactly to a specific point, while the printed location of the gamepiece sprite's y coord is close, i want it to be exact. My idea is for this gamepiece to move at a rate of 52.5 units per second, so a distance of around 2 tiles away or (105 units) in abt 2 seconds or ideally 120 frames. The counter in my while loop says it runs abt 230 times which is a bit odd as ive printed the frame rate in past...consistently at 59 fps.

Thanks for the help.

Replay

Category: c# Time: 2016-07-31 Views: 10
Tags: sfml

Related post

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development

search

Front-end development

Database

development tools

Open Platform

Javascript development

.NET development

cloud computing

server

Copyright (C) avrocks.com, All Rights Reserved.

processed in 0.219 (s). 12 q(s)