猫和鼠标SFML项目更新鼠标精灵位置的问题

时间:2017-04-21 17:18:16

标签: c++ visual-c++ visual-studio-2015 sfml

这是我一直在研究的S​​FML项目。我创建了一个鼠标并将其放置在特定位置。当我尝试使用键盘事件更新其位置时,它会移动但稍后会回到原来的位置。此外,当我尝试向右或向下移动鼠标时,它会消失,然后再次出现。似乎还有一个问题是我无法在四个方向上超过1个瓷砖。

Moving Left

Moving Right

任何帮助都将不胜感激。

#include <SFML/Graphics.hpp>
#include <iostream>
using namespace std;

int main()
{
    sf::Vector2f screenDimensions(800, 800);
    sf::RenderWindow Window;
    Window.create(sf::VideoMode(screenDimensions.x, screenDimensions.y), "Tile Maps Demo");

    int TILE_SIZE = 64;            // Size of the tile for both width and height
    const int MAP_WIDTH = 10;      // Elements in columns
    const int MAP_HEIGHT = 10;     // Elements in rows
    int counter = 0;               // Counts which element its on in each row



    int map[MAP_HEIGHT][MAP_WIDTH] = {
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
        { 0, 1, 3, 1, 1, 1, 1, 1, 0, 0 },
        { 0, 1, 1, 1, 1, 1, 1, 1, 0, 0 },
        { 0, 1, 1, 1, 4, 1, 1, 1, 2, 2 },
        { 0, 1, 1, 1, 1, 1, 1, 1, 2, 2 },
        { 0, 1, 1, 1, 1, 1, 1, 1, 0, 0 },
        { 0, 1, 1, 1, 1, 1, 1, 1, 0, 0 },
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } };


    sf::Sprite water;
    sf::Sprite land;
    sf::Sprite bridge;
    sf::Sprite cat;
    sf::Sprite mouse;

    sf::Texture wTexture;
    sf::Texture lTexture;
    sf::Texture bTexture;
    sf::Texture cTexture;
    sf::Texture mTexture;

    if (!wTexture.loadFromFile("tex_Water2.jpg"))
        cout << "Error! image 1 didn't load" << endl;

    if (!lTexture.loadFromFile("Sand4.jpg"))
        cout << "Error! image 2 didn't load" << endl;

    if (!bTexture.loadFromFile("Wood2.jpg"))
        cout << "Error! image 3 didn't load" << endl;

    if (!cTexture.loadFromFile("TomSand2.jpg"))
        cout << "Error! Image 4 didn't load" << endl;

    if (!mTexture.loadFromFile("JerrySand3.jpg"))
        cout << "Error! Image 5 didn't load" << endl;


    water.setTexture(wTexture);

    land.setTexture(lTexture);

    bridge.setTexture(bTexture);

    cat.setTexture(cTexture);

    mouse.setTexture(mTexture);
    mouse.setPosition(256,256);


    while (Window.isOpen())
    {

        sf::Event Event;

        while (Window.pollEvent(Event))
        {
            switch (Event.type)
            {
            case sf::Event::Closed:
                Window.close();
                break;

            case sf::Event::KeyPressed:
                if (Event.key.code == sf::Keyboard::Escape)
                    Window.close();
                break;

            }

        }
        Window.clear();
        // Index on the first row
        for (int i = 0; i < MAP_HEIGHT; i++)
        {
            // Index the first column

            for (int j = 0; j < MAP_WIDTH; j++)
            {
                // count each element
                counter++;


                if (counter >= MAP_WIDTH)
                {
                    //resets itself to zero when end of a row is reached
                    counter = 0;
                }



             if (map[i][j] == 4)
                {
                    // Else if the number is 4, then place this image at that position then draw
                    //mouse.setPosition(j * TILE_SIZE, i * TILE_SIZE);
                    {
                        if (Event.type)
                        {
                            int m = i;
                            int n = j;
                            if ((Event.type == sf::Event::KeyReleased && Event.key.code == sf::Keyboard::A))
                            {
                                // Move left
                                mouse.setPosition((n-1) * TILE_SIZE, m * TILE_SIZE);
                                //mouse.move(sf::Vector2f(-64,0));
                                //Window.draw(mouse);
                                //land.move(sf::Vector2f(64, 0));
                                land.setPosition(n * TILE_SIZE, m * TILE_SIZE);
                                //Window.draw(land);



                            }
                            else if ((Event.type == sf::Event::KeyReleased && Event.key.code == sf::Keyboard::D))
                            {
                                // Move right
                                mouse.setPosition((n + 1) * TILE_SIZE, m * TILE_SIZE);
                                Window.draw(mouse);
                                land.setPosition(n * TILE_SIZE, m * TILE_SIZE);
                                Window.draw(land);
                                //mouse.move(sf::Vector2f(64, 0));

                            }
                            else if ((Event.type == sf::Event::KeyReleased && Event.key.code == sf::Keyboard::W))
                            {
                                // Move up
                                mouse.setPosition(n  * TILE_SIZE, (m-1) * TILE_SIZE);
                                Window.draw(mouse);
                                land.setPosition(n * TILE_SIZE, m * TILE_SIZE);
                                Window.draw(land);
                                //mouse.move(sf::Vector2f(0, -64));

                            }
                            else if ((Event.type == sf::Event::KeyReleased && Event.key.code == sf::Keyboard::S))
                            {
                                // Move down
                                mouse.setPosition(n * TILE_SIZE, (m+1) * TILE_SIZE);
                                Window.draw(mouse);
                                land.setPosition(n * TILE_SIZE, m * TILE_SIZE);
                                Window.draw(land);
                                //mouse.move(sf::Vector2f(0,64));

                            }
                        }
                    }
                    Window.draw(mouse);

                }
                else if (map[i][j] == 0)
                {
                    // If the number is 0, then place the image at this position then draw
                    water.setPosition(j * TILE_SIZE, i * TILE_SIZE);
                    Window.draw(water);

                }
                else if (map[i][j] == 1)
                {
                    // Else if the number is 1, then place this image at that position then draw
                    land.setPosition(j * TILE_SIZE, i * TILE_SIZE);
                    Window.draw(land);
                }
                else if (map[i][j] == 2)
                {
                    // Else if the number is 2, then place this image at that position then draw
                    bridge.setPosition(j * TILE_SIZE, i * TILE_SIZE);
                    Window.draw(bridge);
                }
                else if (map[i][j] == 3)
                {
                    // Else if the number is 3, then place this image at that position then draw
                    cat.setPosition(j * TILE_SIZE, i * TILE_SIZE);
                    Window.draw(cat);
                }


            }

        }

        Window.display();
    }

        return 0;
    }

0 个答案:

没有答案