Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - The Illusionist Mirage

Pages: 1 2 [3] 4 5 ... 8
31
General / Re: Sprite not moving
« on: October 18, 2013, 08:41:56 am »
I don't see any drawing code (window.clear(), window.draw(player), window.display(), etc.) in your code.

Actually, I have it properly in place, but my problem lies in update only so i didn't post full code here ;)

My main is something like this:
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <iostream>
#include "Constants.h"
#include "Game.h"
#include "TileMap.h"
#include "Player.h"

int main()
{
        //Game m_game;
        //m_game.Run();

        sf::RenderWindow m_gameWindow(sf::VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT,
                WINDOW_BPP), WINDOW_TITLE, sf::Style::Close);

        sf::Event m_gameEvent;

        TileMap m_tileMap;
        Player m_player;
       
        float t_i = 0.0f, t_f = 0.0f, dt = 0.0f;
        sf::Clock m_timer;

        while(m_gameWindow.isOpen())
        {
                t_f = m_timer.getElapsedTime().asSeconds();
                dt = t_f - t_i;
                t_i = t_f;

                while(m_gameWindow.pollEvent(m_gameEvent))
                {
                        if(m_gameEvent.type == sf::Event::Closed
                                || m_gameEvent.key.code == sf::Keyboard::Escape)
                        {
                                m_gameWindow.close();
                        }
                }

                m_gameWindow.clear();

                //m_tileMap.RenderTileMaps(m_gameWindow);

                m_player.Draw(m_gameWindow);

                m_player.Update(dt);

                m_gameWindow.display();
        }
       
        return EXIT_SUCCESS;
}
 

32
General / Re: Sprite not moving
« on: October 18, 2013, 06:36:00 am »
What values do you have for the player speed?  I want to say that maybe your DELTA_TIME may be so small that you are only moving by like0.000x a pixel, which would look like you aren't moving at all.  Since you're cout's are all being hit as they should maybe you should print out your players current x/y coordinate to like 6 decimal places, i bet the sprite is only moving a teeny tiny bit.

I already tried to remove DELTA_TIME and made the player move like m_playerSprite.move(m_PlayerSpeed.x , 0); and m_playerSprite.move(0, m_PlayerSpeed.x); (where m_playerSpeed is a sf::Vector2f and has a value 250 for both x and y). But that doesn't move my sprite either.

33
General / [SOLVED]Sprite not moving
« on: October 17, 2013, 08:21:00 pm »
Hello

I have a player class which contains a member function called Update() to get the key the user is pressing(if any) and then move the sprite accordingly using the move(offsetX, offsetY) function. But that's not working in my case. Here's how I move my sprite:

void Player::Update(float DELTA_TIME)
{
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
        {
                m_playerSource.y = PlayerDirection::UP;
                m_isKeyPressed = true;
                m_currentKeyState = KeyState::UP_PRESSED;
                m_playerSprite.move(0, - m_PlayerSpeed.y * DELTA_TIME);
                std::cout << "Up" << std::endl;
        }

        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
        {
                m_playerSource.y = PlayerDirection::DOWN;
                m_isKeyPressed = true;
                m_currentKeyState = KeyState::DOWN_PRESSED;
                m_playerSprite.move(0, m_PlayerSpeed.y * DELTA_TIME);
                std::cout << "Down" << std::endl;
        }

        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
        {
                m_playerSource.y = PlayerDirection::LEFT;
                m_isKeyPressed = true;
                m_currentKeyState = KeyState::LEFT_PRESSED;
                m_playerSprite.move(- m_PlayerSpeed.x * DELTA_TIME, 0);
                std::cout << "Left" << std::endl;
        }

        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
        {
                m_playerSource.y = PlayerDirection::RIGHT;
                m_isKeyPressed = true;
                m_currentKeyState = KeyState::RIGHT_PRESSED;
                m_playerSprite.move(m_PlayerSpeed.x * DELTA_TIME, 0);
                std::cout << "Right" << std::endl;
        }

        else
        {
                m_isKeyPressed = false;
                m_currentKeyState = KeyState::NONE_PRESSED;
        }

        m_frameCounter += m_frameSpeed * m_frameTimer.restart().asSeconds();

        if(m_frameCounter >= m_switchFrame)
        {
                m_frameCounter = 0.0f;

                if(m_isKeyPressed)
                {
                        m_playerSource.x++;
                }

                if(m_playerSource.x * 32 >= m_playerTexture.getSize().x)
                {
                        m_playerSource.x = 0;
                }
        }

        m_playerSprite.setTextureRect(sf::IntRect(m_playerSource.x * 32, m_playerSource.y * 32, 32, 32));
}
 

Even the cout statement gets printed if I press a key but the sprite's not moving. This function is called directly in the main game loop as follows:
while(myWindow.isOpen())
{
    t_f = m_timer.getElapsedTime().asSeconds();
    dt = t_f - t_i;
    t_i = t_f;

    sf::Event m_gameEvent;
    while(myWindow.pollEvent(m_gameEvent))
    {
         if(m_gameEvent.type == sf::Event::Closed
                || m_gameEvent.key.code == sf::Keyboard::Escape)
        {
                m_gameWindow.close();
        }
    }

    m_player.Update(dt);      
}
 

Where am I going wrong?

Thanks

34
General / Re: Loading from this tileset
« on: October 15, 2013, 04:25:57 pm »
OK, I've figured out my problems from one of my friends(Geheim). Thanks to all for helping!

35
General / Re: Loading from this tileset
« on: October 15, 2013, 02:02:22 pm »
Ok. Here's how I load my tiles:
for(int i = 0; i < H; i++)
                {
                        for(int j = 0; j < W; j++)
                        {
                                if(tileMap[i][j] == '0')// || tileMap[i][j] == ' ')
                                        tile_ground.setTextureRect(sf::IntRect(256, 64, 32, 32));

                                if(tileMap[i][j] == 'P')
                                        tile_ground.setTextureRect(sf::IntRect(512, 0, 32, 32));

                                tile_ground.setPosition(j * 32, i * 32);
                                window.draw(tile_ground);
                        }
                }
 

And here's how I do the collision checking:
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
                {
                        source.y = Up;
                        isKeyPressed = true;
                        if(!heroSprite.getLocalBounds().intersects(tile_ground.getLocalBounds()))               // I check if the player sprite and the obstacle intersect     
                                heroSprite.move(0, - heroSpeed.y * deltaTime);
                }
               
                else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
                {
                        source.y = Down;
                        isKeyPressed = true;
                        if(!heroSprite.getLocalBounds().intersects(tile_ground.getLocalBounds()))               // I check if the player sprite and the obstacle intersect
                                heroSprite.move(0, heroSpeed.y * deltaTime);
                }

                else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                {
                        source.y = Left;
                        isKeyPressed = true;
                        if(!heroSprite.getLocalBounds().intersects(tile_ground.getLocalBounds()))               // I check if the player sprite and the obstacle intersect
                                heroSprite.move(- heroSpeed.x * deltaTime, 0);
                }

                else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                {
                        source.y = Right;
                        isKeyPressed = true;
                        if(!heroSprite.getLocalBounds().intersects(tile_ground.getLocalBounds()))               // I check if the player sprite and the obstacle intersect
                                heroSprite.move(heroSpeed.x * deltaTime, 0);
                }
 

But my sprite is stuck in only one tile and can't move in any direction. How do I prevent this?

36
General / Re: Loading from this tileset
« on: October 15, 2013, 05:30:23 am »
I am still unable to figure out what's wrong. :(

37
General / Re: Loading from this tileset
« on: October 14, 2013, 06:18:57 pm »
Hi

my code is not producing the desired ouput. Here's my code:
/* Platformer try */

#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>

float offsetX = 0, offsetY = 0;

const int H = 17;
const int W = 150;

sf::String tileMap[H] =
{
        "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
        "0                                                                                                                                                    0",
        "0                                                                                                                                                    0",
        "0                                                                                                                                                    0",
        "0                                                                                                                                                    0",
        "0                   w                                                                                                                                0",
        "0                                                                                                                                                    0",
        "0                                                                                                                                                    0",
        "0                      c                                                                                                                             0",
        "0                                                                                                                                                    0",
        "0                                                                                                                                                    0",
        "0               c    kckck                                                                                                                           0",
        "0G                                                                                                                                                   0",
        "0           d    g       d                                                                                                                           0",
        "PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP",
        "PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP",
        "PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP"
};

// Classes

/* Player */

class PLAYER
{
        public:
                float dy, dx;
                sf::FloatRect rect;
                bool onGround;
                sf::Sprite sprite;
                float currentFrame;

                PLAYER(sf::Texture &image);
                void update(float time);
                void Collision(int num);
                ~PLAYER();
};

PLAYER::PLAYER(sf::Texture &image)
{
        sprite.setTexture(image);
        rect = sf::FloatRect(100, 180, 16, 16);

        dx = dy = 0.1;
        currentFrame = 0;
}

void PLAYER::update(float time)
{
        rect.left += dx * time;
        Collision(0);

        if(!onGround)
                dy = dy + 0.0005 * time;

        rect.top += dy * time;
        onGround = false;
        Collision(1);

        currentFrame += time * 0.005;
       
        if(currentFrame > 3)
                currentFrame -= 3;

        if(dx > 0)
                sprite.setTextureRect(sf::IntRect(112 + 31 * int(currentFrame), 144, 16, 16));
        if(dx < 0)
                sprite.setTextureRect(sf::IntRect(112 + 31 * int(currentFrame) + 16, 144, -16, 16));

        sprite.setPosition(rect.left - offsetX, rect.top - offsetY);

        dx = 0;
}

void PLAYER::Collision(int num)
{
        for(int i = rect.top / 16; i < (rect.top + rect.height) / 16; i++)
        {
                for(int j = rect.left / 16; j < (rect.left + rect.width) / 16; j++)
                {
                        if(tileMap[i][j] == 'P' || tileMap[i][j] == 'k' || tileMap[i][j] == '0' || tileMap[i][j] == 'r' || tileMap[i][j] == 't')
                        {
                                if(dy > 0 && num == 1)
                                {
                                        rect.top = i * 16 - rect.height;
                                        dy = 0;
                                        onGround = true;
                                }

                                if(dy < 0 && num == 1)
                                {
                                        rect.top = i * 16 + 16;
                                        dy = 0;
                                }

                                if(dx > 0 && num == 0)
                                {
                                        rect.left = j * 16 - rect.width;
                                }

                                if(dx < 0 && num == 0)
                                {
                                        rect.left = j * 16 + 16;
                                }
                        }
                }
        }
}

PLAYER::~PLAYER()
{
        // dctor
}

class ENEMY
{
        public:
                float dx, dy;
                sf::FloatRect rect;
                sf::Sprite sprite;
                float currentFrame;
                bool onGround;
                bool life;

                void set(sf::Texture &image, int x, int y);
                void update(float time);
                void Collision();
                ~ENEMY();
};

void ENEMY::set(sf::Texture &image, int x, int y)
{
        sprite.setTexture(image);
        rect = sf::FloatRect(x, y, 16, 16);

        dx = 0.05;
        currentFrame = 0;
        life = true;
}

void ENEMY::update(float time)
{
        rect.left += dx * time;

        Collision();
       
        currentFrame  += time * 0.005;

        if(currentFrame > 2)
                currentFrame -= 2;
       
        sprite.setTextureRect(sf::IntRect(18 * int(currentFrame), 0, 16, 16));

        if(!life)
                sprite.setTextureRect(sf::IntRect(58, 0, 16, 16));

        sprite.setPosition(rect.left - offsetX, rect.top - offsetY);
}

void ENEMY::Collision()
{
        for(int i = rect.top / 16; i < (rect.top + rect.height); i++)
        {
                for(int j = rect.left / 16; j < (rect.left + rect.width); j++)
                {
                        if(tileMap[i][j] == 'p' || tileMap[i][j] == '0')
                        {
                                if(dx > 0)
                                {
                                        rect.left = j * 16 - rect.width;
                                        dx *= -1;
                                }

                                else if(dx < 0)
                                {
                                        rect.left = j * 16 + 16;
                                        dx *=- 1;
                                }
                        }
                }
        }
}

ENEMY::~ENEMY()
{
        // dctor
}

int main()
{
    sf::RenderWindow window(sf::VideoMode(400, 250, 32), "Platformer_try[mario]");

        sf::Texture tileSet;
        tileSet.loadFromFile("Mario_tileset.png");

        PLAYER Mario(tileSet);

        ENEMY Mushy;
        Mushy.set(tileSet, 48 * 16, 13 * 16);

        sf::Sprite tiles;
        tiles.setTexture(tileSet);

        sf::SoundBuffer buffer;
        buffer.loadFromFile("Jump.ogg");
       
        sf::Sound sound(buffer);

        sf::Music music;
        music.openFromFile("Mario_Theme.ogg");
        //music.play();

        sf::Clock clock;

    while (window.isOpen())
    {
                float time = clock.getElapsedTime().asMicroseconds();
                clock.restart();

                time = time / 500;

                if(time > 20)
                        time = 20;
               
                sf::Event event;
        while(window.pollEvent(event))
        {
                        if(event.type == sf::Event::Closed || event.key.code == sf::Keyboard::Escape)
                window.close();
        }

                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                {
                        Mario.dx =- 0.1f;
                }

                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                {
                        Mario.dx = 0.1f;
                }

                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
                {
                        if(Mario.onGround)
                        {
                                Mario.dy =- 0.27f;
                                Mario.onGround = false;
                                //sound.play();
                        }
                }

                Mario.update(time);
                //Mushy.update(time);

                if(Mario.rect.intersects(Mushy.rect))
                {
                        if(Mushy.life)
                        {
                                if(Mario.dy > 0)
                                {
                                        Mushy.dy = 0;
                                        Mario.dy = -0.2;
                                        Mushy.life = false;
                                }

                                else
                                {
                                        Mario.sprite.setColor(sf::Color::Red);
                                }
                        }
                }
               
                if(Mario.rect.left > 200)
                        offsetX = Mario.rect.left - 200;


        window.clear(sf::Color(107, 140, 225));

                for(int i = 0; i < H; i++)
                {
                        for(int j = 0; j < W; j++)
                        {
                                if(tileMap[i][j] == 'P')
                                        tiles.setTextureRect(sf::IntRect(143 - 16 * 3, 112, 16, 16));

                                if(tileMap[i][j] == 'k')
                                        tiles.setTextureRect(sf::IntRect(143, 112, 16, 16));

                                if(tileMap[i][j] == 'c')
                                        tiles.setTextureRect(sf::IntRect(143 - 16, 112, 16, 16));

                                if(tileMap[i][j] == 't')
                                        tiles.setTextureRect(sf::IntRect(0, 47, 32, 95 - 47));

                                if(tileMap[i][j] == 'g')
                                        tiles.setTextureRect(sf::IntRect(0, 16 * 9 - 5, 3 * 16, 16 * 2 + 5));

                                if(tileMap[i][j] == 'G')
                                        tiles.setTextureRect(sf::IntRect(145, 222, 222 - 145, 255 - 222));

                                if(tileMap[i][j] == 'd')
                                        tiles.setTextureRect(sf::IntRect(0, 106, 74, 127 - 106));

                                if(tileMap[i][j] == 'w')
                                        tiles.setTextureRect(sf::IntRect(99, 224, 140 - 99, 255 - 224));

                                if(tileMap[i][j] == 'r')
                                        tiles.setTextureRect(sf::IntRect(143 - 32, 112, 16, 16));

                                if(tileMap[i][j] == '0' || tileMap[i][j] == ' ')
                                        continue;

                                tiles.setPosition(sf::Vector2f(j * 16 - offsetY, i * 16 - offsetX));
                                window.draw(tiles);
                        }
                }

                window.draw(Mario.sprite);
                //window.draw(Mushy.sprite);
        window.display();
    }

    return 0;
}
 

My mario goes correctly upto some extent but then suddenly, he starts to move in the positive y-direction.







But I can still jump and move around as if I was moving normally(I can even jump), but only the movement is actually in y-direction.

I was following code from here which I found on this forum a few days ago(this code works correctly):
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>

float offsetX = 0, offsetY = 0;

const int H = 17;
const int W = 150;

sf::String TileMap[H] =
{
    "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
    "0                                                                                                                                                    0",
    "0                                                                                    w                                                               0",
    "0                   w                                  w                   w                                                                         0",
    "0                                      w                                       kk                                                                    0",
    "0                                                                             k  k    k    k                                                         0",
    "0                      c                                                      k      kkk  kkk  w                                                     0",
    "0                                                                       r     k       k    k                                                         0",
    "0                                                                      rr     k  k                                                                   0",
    "0                                                                     rrr      kk                                                                    0",
    "0               c    kckck                                           rrrr                                                                            0",
    "0                                      t0                           rrrrr                                                                            0",
    "0G                                     00              t0          rrrrrr            G                                                               0",
    "0           d    g       d             00              00         rrrrrrr                                                                            0",
    "PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP",
    "PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP",
    "PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP",
};

class PLAYER
{
    public:

    float dx,dy;
    sf::FloatRect rect;
    bool onGround;
    sf::Sprite sprite;
    float currentFrame;

    PLAYER(sf::Texture &image)
    {
        sprite.setTexture(image);
        rect = sf::FloatRect(100, 180, 16, 16);

        dx = dy = 0.1;
        currentFrame = 0;
    }

    void update(float time)
    {
        rect.left += dx * time;
        Collision(0);

        if(!onGround)
            dy = dy + 0.0005 * time;

        rect.top += dy * time;
        onGround = false;
        Collision(1);

        currentFrame += time * 0.005;
        if(currentFrame > 3)
            currentFrame -= 3;

        if(dx > 0)
            sprite.setTextureRect(sf::IntRect(112 + 31 * int(currentFrame), 144, 16, 16));
        if(dx < 0)
            sprite.setTextureRect(sf::IntRect(112 + 31 * int(currentFrame) + 16, 144, -16, 16));

        sprite.setPosition(rect.left - offsetX, rect.top - offsetY);

        dx = 0;
    }

    void Collision(int num)
    {
        for(int i = rect.top / 16; i < (rect.top + rect.height) / 16; i++)
            for (int j = rect.left / 16; j < (rect.left + rect.width) / 16; j++)
                        {
                if((TileMap[i][j] == 'P')
                    || (TileMap[i][j] == 'k')
                        || (TileMap[i][j] == '0')
                            || (TileMap[i][j] == 'r')
                                || (TileMap[i][j] == 't'))
                {
                    if(dy > 0 && num == 1)
                    {
                        rect.top = i * 16 - rect.height;
                        dy = 0;
                        onGround = true;
                    }

                    if(dy < 0 && num == 1)
                    {
                        rect.top = i * 16 + 16;
                        dy = 0;
                    }

                    if(dx > 0 && num == 0)
                    {
                        rect.left =  j * 16 -  rect.width;
                    }

                    if(dx < 0 && num == 0)
                    {
                        rect.left =  j * 16 + 16;
                    }
                }

                if(TileMap[i][j] == 'c')
                {
                     //TileMap[i][j]=' ';
                }
                         }

    }

};

class ENEMY
{
    public:
        float dx,dy;
        sf::FloatRect rect;
        sf::Sprite sprite;
        float currentFrame;
        bool life;

        void set(sf::Texture &image, int x, int y)
        {
            sprite.setTexture(image);
            rect = sf::FloatRect(x, y, 16, 16);

            dx = 0.05;
            currentFrame = 0;
            life = true;
        }

        void update(float time)
        {
            rect.left += dx * time;

            Collision();

            currentFrame += time * 0.005;
            if(currentFrame > 2)
                currentFrame -= 2;

            sprite.setTextureRect(sf::IntRect(18 * int(currentFrame), 0, 16, 16));

            if(!life)
                sprite.setTextureRect(sf::IntRect(58, 0, 16, 16));

            sprite.setPosition(rect.left - offsetX, rect.top - offsetY);

        }

        void Collision()
        {
            for (int i = rect.top / 16; i < (rect.top + rect.height) / 16; i++)
                for(int j = rect.left / 16; j < (rect.left + rect.width) / 16; j++)
                    if((TileMap[i][j] == 'P') || (TileMap[i][j] == '0'))
                    {
                        if(dx > 0)
                        {
                            rect.left = j * 16 - rect.width;
                            dx *= -1;
                        }

                                                else if(dx < 0)
                        {
                            rect.left = j * 16 + 16;
                            dx *=- 1;
                        }
                    }
        }
};

int main()
{

    sf::RenderWindow window(sf::VideoMode(400, 250), "Super Mario");

        sf::Texture tileSet;
        tileSet.loadFromFile("Mario_Tileset.png");

        PLAYER Mario(tileSet);
        ENEMY  enemy, enemy2;

        enemy.set(tileSet, 48*16, 13*16);

        sf::Sprite tile(tileSet);

        sf::SoundBuffer buffer;
        buffer.loadFromFile("Jump.ogg");
        sf::Sound sound(buffer);

    sf::Music music;
    music.openFromFile("Mario_Theme.ogg");
    //music.play();

        sf::Clock clock;

    while(window.isOpen())
    {
                float time = clock.getElapsedTime().asMicroseconds();
                clock.restart();

                time = time / 500;  // &#1079;&#1076;&#1077;&#1089;&#1100; &#1088;&#1077;&#1075;&#1091;&#1083;&#1080;&#1088;&#1091;&#1077;&#1084; &#1089;&#1082;&#1086;&#1088;&#1086;&#1089;&#1090;&#1100; &#1080;&#1075;&#1088;&#1099;

                if(time > 20)
            time = 20;

        sf::Event event;
        while(window.pollEvent(event))
        {
            if(event.type == sf::Event::Closed)
                window.close();
                }

                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
            Mario.dx =- 0.1;

            if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
            Mario.dx = 0.1;

            if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
            if(Mario.onGround)
            {
                Mario.dy =- 0.27;
                Mario.onGround = false;
                //sound.play();
            }

                 Mario.update(time);
                 enemy.update(time);

                 if(Mario.rect.intersects(enemy.rect))
                 {
             if(enemy.life)
             {
                 if(Mario.dy > 0)
                 {
                     enemy.dx = 0;
                     Mario.dy =- 0.2;
                     enemy.life = false;
                 }
                 else Mario.sprite.setColor(sf::Color::Red);
                         }
                 }

                 if(Mario.rect.left > 200)
            offsetX = Mario.rect.left - 200;           //&#1089;&#1084;&#1077;&#1097;&#1077;&#1085;&#1080;&#1077;

                 window.clear(sf::Color(107, 140, 255));

                 for(int i = 0; i < H; i++)
                         for(int j = 0; j < W; j++)
                          {
                                if(TileMap[i][j] == 'P')
                    tile.setTextureRect(sf::IntRect(143 - 16 * 3, 112, 16, 16));

                                if(TileMap[i][j] == 'k')
                    tile.setTextureRect(sf::IntRect(143, 112, 16, 16));

                            if(TileMap[i][j] == 'c')
                    tile.setTextureRect(sf::IntRect(143 - 16, 112, 16, 16));

                                if(TileMap[i][j] == 't')
                    tile.setTextureRect(sf::IntRect(0, 47, 32, 95 - 47));

                                if(TileMap[i][j] == 'g')
                    tile.setTextureRect(sf::IntRect(0, 16 * 9 - 5, 3 * 16, 16 * 2 + 5));

                                if(TileMap[i][j] == 'G')
                    tile.setTextureRect(sf::IntRect(145, 222, 222 - 145, 255 - 222));

                            if(TileMap[i][j] == 'd')
                    tile.setTextureRect(sf::IntRect(0, 106, 74, 127 - 106));

                                if(TileMap[i][j] == 'w')
                    tile.setTextureRect(sf::IntRect(99, 224, 140 - 99, 255 - 224));

                                if(TileMap[i][j] == 'r')
                    tile.setTextureRect(sf::IntRect(143 - 32, 112, 16, 16));

                                if((TileMap[i][j] == ' ') || (TileMap[i][j] == '0'))
                    continue;

                            tile.setPosition(j * 16 - offsetX, i * 16 - offsetY);
                        window.draw(tile);
                     }

                window.draw(Mario.sprite);
            window.draw(enemy.sprite);
            window.draw(enemy2.sprite);

                window.display();
    }

    return 0;
}
 

How exactly does this collision and update functions work? And what mistakes are therein my code?

Thanks

38
General / Re: Loading from this tileset
« on: October 14, 2013, 11:28:11 am »
Umm...you look at where the player is, what tiles are close to him, what user input there is (if any), and decide what velocity he should have as a result?

It's probably not that complicated, and the exact answer depends entirely on what you want the program to do (like practically everything else in programming).  All we can do is throw out some obvious ideas, eg, if there is no solid tile directly underneath the player, maybe make him move downward.

You mean what this thread here suggests?

I also got this idea from some site whose link I can't find(neither remember or find in browser history):

At any given instant, the player will have a minimum of 4 tiles adjacent to him. I can create a temporary variable and I'll check if the player can land in those coordinates. If he can I'll copy the temporary variable to that of player's position. If not, nothing happens. What do you think about this?

And I've decided that I'll keep the player falling and as soon as he touches those tiles, I'll stop his downward-Y movement(+ve direction Y movement). If he jumps, I'll try to implement gravity.

Thanks for the help!

39
General / Re: Loading from this tileset
« on: October 14, 2013, 09:13:11 am »
Here's a map that I've created:


Here are the tiles:


And here's my code:
#include <SFML/Graphics.hpp>

const int width = 640;
const int height = 480;
const int bpp = 32;
const char* title = "tilemap[test]_1";

const int H = 10;
const int W = 50;

sf::String tileMap[H] =
{
        "00000000000000000000000000000000000000000000000000",
        "0                                                0",
        "0                                                0",
        "0                                                0",
        "0                                                0",
        "0               MMM                              0",
        "0                                                0",
        "0                                                0",
        "GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG",
        "GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG"
};

int main()
{
    sf::RenderWindow window(sf::VideoMode(width, height, bpp), title, sf::Style::Close);

        sf::Texture tileset;
        if(!tileset.loadFromFile("tiles.png"))
        {
                system("pause");
                return EXIT_FAILURE;
        }

        sf::Sprite tile;
        tile.setTexture(tileset);

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed
                                || event.key.code == sf::Keyboard::Escape)
                                        window.close();
        }

        window.clear(sf::Color(107, 140, 255));

                for(int i = 0; i < H; i++)
                {
                        for(int j = 0; j < W; j++)
                        {
                                if(tileMap[i][j] == 'G')
                                        tile.setTextureRect(sf::IntRect(0 * 16, 0 * 16, 16, 16));

                                if(tileMap[i][j] == 'M')
                                        tile.setTextureRect(sf::IntRect(1 * 16, 0 * 16, 16, 16));
                               
                                if(tileMap[i][j] == '0' || tileMap[i][j] == ' ')
                                        continue;

                                tile.setPosition(sf::Vector2f(j * 16, i * 16));
                                window.draw(tile);
                        }
                }

        window.display();
    }

        system("pause");
    return EXIT_SUCCESS;
}
 

So, how do I use this tile map along with my character?

Thanks

40
General / Re: Loading from this tileset
« on: October 14, 2013, 08:44:00 am »
Does that make sense?

Yes! Thanks! Now I could make my character move from a spritesheet within the screen.

Here's my spritesheet:


And here's my code:
#include <SFML/Graphics.hpp>

int main()
{
        sf::RenderWindow window(sf::VideoMode(640, 480), "tilemap_character_test", sf::Style::Close);

        enum Direction { Down, Left, Right, Up };

        sf::Vector2i source(1, Down);

        sf::Texture tex;
        if(!tex.loadFromFile("player2.png"))
        {
                system("pause");
                return EXIT_FAILURE;
        }
   
        sf::Sprite sprite;
        sprite.setTexture(tex);
        sprite.setTextureRect(sf::IntRect(32, 0, 32, 32));

        const float spriteSpeedX = 250.0f;
        const float spriteSpeedY = 250.0f;

        sf::Clock clock;
        float deltaTime = 0.f, currentTime = 0.f, previousTime = 0.f;

        float frameCounter = 0, switchFrame = 75, frameSpeed = 500;
        sf::Clock clock2;

    while (window.isOpen())
    {
                currentTime = clock.getElapsedTime().asSeconds();
                deltaTime = currentTime - previousTime;
                previousTime = currentTime;

                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;
                        }
                       
        }
               
                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
                {
                        source.y = Direction::Up;
                        if(sprite.getPosition().y >= 0)
                                sprite.move(0, - spriteSpeedY * deltaTime);
                }

                else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
                {
                        source.y = Direction::Down;
                        if(sprite.getPosition().y <= 448)
                                sprite.move(0, spriteSpeedY * deltaTime);
                }

                else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                {
                        source.y = Direction::Right;
                        if(sprite.getPosition().x <= 608)
                                sprite.move(spriteSpeedX * deltaTime, 0);
                }

                else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                {
                        source.y = Direction::Left;
                        if(sprite.getPosition().x >= 0)
                                sprite.move(- spriteSpeedY * deltaTime, 0);
                }
               
                frameCounter += frameSpeed * clock2.restart().asSeconds();
                if(frameCounter >= switchFrame)
                {
                        frameCounter = 0;
                        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up)
                                || sf::Keyboard::isKeyPressed(sf::Keyboard::Down)
                                        || sf::Keyboard::isKeyPressed(sf::Keyboard::Left)
                                                || sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                                source.x++;
               
                        if(source.x * 32 >= tex.getSize().x)
                                source.x = 0;
                }

                sprite.setTextureRect(sf::IntRect(source.x * 32, source.y * 32, 32, 32));

        window.clear();
                window.draw(sprite);
        window.display();
    }

        system("pause");
    return EXIT_SUCCESS;
}
 

And here's the output:




So, now how do I load a tilemap in which this sprite can move(and test for collisions etc). I don't need exact codes but only how I should make my approach and the methods involved.

Thanks for your help! :D

41
General / Re: Loading from this tileset
« on: October 13, 2013, 05:31:08 pm »
@Ancurio

This maybe irritating to you but I couldn't still understand what you just said. :(

42
General / Re: Loading from this tileset
« on: October 12, 2013, 04:27:22 pm »
I have tried these threads(thread-1, thread-2) but I am unable to fully understand them. Until now, I had no problem with SFML or C++ but loading tilemaps is seeming pretty confusing to me.

I know this is a vague question but still I am asking it : how exactly is tilemap handling done in SFML and C++ using Tiled!

Can anyone please explain briefly to me the exact methods and algorithms used for loading and displaying tilemaps?

Thanks

43
General / Re: Loading from this tileset
« on: October 11, 2013, 08:10:46 am »
Ok. I came across Tiled and could easily create a tilemap using it. So, how can I use the tilemap in my codes(in C++)? If anyone can link me t a tutorial, it'd be very helpful.

Thanks

44
General / [Solved]Loading from this tileset
« on: October 10, 2013, 05:17:20 pm »
Hello

I was following this tutorial on the SFML site and learnt how to load a tilemap fomr a tileset. But I have one problem - how to access tile from the tileset whose y-coordinate is not zero, ie, 1, 2, etc.

Example, if this is the tileset:


and I want to load the tiles of mario, how do I I do that?

Thanks

P.S.
I followed the tutorial and could understand how the tile class works. But is this tile class universally applicable for animated tiles and all?

45
General / Re: Copy program/game to other machines
« on: October 04, 2013, 12:27:04 pm »
First, update the binary from 2.0 RC to 2.1. That's why you have those missing symbols.

How could you actually find out the reason his code didn't compile was due to older binaries?

Pages: 1 2 [3] 4 5 ... 8
anything