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 - CodingNewbie

Pages: [1]
1
SFML projects / Re: 'Tiled' Tile-map Loader
« on: October 19, 2013, 02:17:55 am »
First of all, thank you creating such a useful tool.  It is much appreciated :).

Alright, now, I've been trying to figure out a way to handle collision with your code, but I'm coming up short.  If I create more than 2 layers on a Tiled map, the layers don't look right when they load.  So, I made 1 .tmx with 2 layers (which is all visable tiles), and then a second .tmx with a single layer with invisible tiles (for collision).  I've been trying every method I could find to load the tiles from the Collision.tmx and not allow the animatedSprite to pass through them, but nothing is working.  Here is my code at the moment:

#include "stdafx.h"
#include "zlib.h"
#include "MapLoader.h"
#include "AnimatedSprite.h"
#include "Animation.h"
#include "zconf.h"
#include <luabind/luabind.hpp>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <string>
#include <iostream>

int main()
{
    sf::Vector2i screenDimensions(720, 420);
    sf::RenderWindow window(sf::VideoMode(screenDimensions.x, screenDimensions.y), "Pokemon Clone");

    // load texture
    sf::Texture texture;
    texture.loadFromFile("Player.png");

        //create map loader and load map
        tmx::MapLoader ml("Maps/");
        tmx::MapLoader mll("Maps/");
        ml.Load("Pallet Town.tmx");
        mll.Load("Collision.tmx");


    // push frames
    Animation walkingAnimationDown;
    walkingAnimationDown.setSpriteSheet(texture);
        walkingAnimationDown.addFrame(sf::IntRect(16, 0, 16, 20));
    walkingAnimationDown.addFrame(sf::IntRect(32, 0, 16, 20));
    walkingAnimationDown.addFrame(sf::IntRect(16, 0, 16, 20));
    walkingAnimationDown.addFrame(sf::IntRect(0, 0, 16, 20));

    Animation walkingAnimationLeft;
    walkingAnimationLeft.setSpriteSheet(texture);
    walkingAnimationLeft.addFrame(sf::IntRect(16, 40, 16, 20));
    walkingAnimationLeft.addFrame(sf::IntRect(32, 40, 16, 20));
    walkingAnimationLeft.addFrame(sf::IntRect(16, 40, 16, 20));
    walkingAnimationLeft.addFrame(sf::IntRect(0, 40, 16, 20));

    Animation walkingAnimationRight;
    walkingAnimationRight.setSpriteSheet(texture);
    walkingAnimationRight.addFrame(sf::IntRect(16, 60, 16, 20));
    walkingAnimationRight.addFrame(sf::IntRect(32, 60, 16, 20));
    walkingAnimationRight.addFrame(sf::IntRect(16, 60, 16, 20));
    walkingAnimationRight.addFrame(sf::IntRect(0, 60, 16, 20));

    Animation walkingAnimationUp;
    walkingAnimationUp.setSpriteSheet(texture);
        walkingAnimationUp.addFrame(sf::IntRect(16, 20, 16, 20));
    walkingAnimationUp.addFrame(sf::IntRect(32, 20, 16, 20));
    walkingAnimationUp.addFrame(sf::IntRect(16, 20, 16, 20));
    walkingAnimationUp.addFrame(sf::IntRect(0, 20, 16, 20));

    // set up AnimatesSprite
    AnimatedSprite animatedSprite(sf::seconds(0.13));
    animatedSprite.setAnimation(walkingAnimationDown);
    animatedSprite.pause();
    animatedSprite.setPosition(700, 400);
       
    sf::Clock frameClock;

        sf::Music music;

        if(!music.openFromFile("Pallet Town Theme.ogg"))
                std::cout << "ERROR: Can't find Pallet Town Theme.ogg" << std::endl;

        music.play();
        music.setLoop(true);

    float speed = 0.045f;
    bool bNoKeyWasPressed = true;

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
            if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape)
                window.close();
                        if (event.type == sf::Event::LostFocus)
                        {
                                music.pause();
                               
                        }
                        if (event.type == sf::Event::GainedFocus)
                                music.play();
        }
               
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
        {
            if(animatedSprite.getAnimation() != &walkingAnimationUp)
            {
                 animatedSprite.setAnimation(walkingAnimationUp);
            }
            animatedSprite.play();
                        animatedSprite.move(0, -speed);
                        animatedSprite.move(0, 0);
            bNoKeyWasPressed = false;
        }
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
        {
            if(animatedSprite.getAnimation() != &walkingAnimationDown)
            {
                animatedSprite.setAnimation(walkingAnimationDown);
            }
            animatedSprite.play();
            animatedSprite.move(0, speed);
            bNoKeyWasPressed = false;
        }
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
        {
            if(animatedSprite.getAnimation() != &walkingAnimationRight)
            {
                animatedSprite.setAnimation(walkingAnimationRight);
            }
            animatedSprite.play();
            animatedSprite.move(speed, 0);
            bNoKeyWasPressed = false;
        }
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
        {
            if(animatedSprite.getAnimation() != &walkingAnimationLeft)
            {
                animatedSprite.setAnimation(walkingAnimationLeft);
            }
            animatedSprite.play();
            animatedSprite.move(-speed, 0);
            bNoKeyWasPressed = false;
        }

        else if(bNoKeyWasPressed)
        {
            animatedSprite.pause();
        }
        bNoKeyWasPressed = true;

        // update AnimatedSprite
        animatedSprite.update(frameClock.restart());

        window.clear();
                window.draw(ml);
                ml.Draw(window, tmx::MapLayer::Back);
                window.draw(animatedSprite);
                ml.Draw(window, tmx::MapLayer::Front);
                mll.Draw(window, tmx::MapLayer::All);

        window.display();
    }

    return 0;
}
 

2
General / Re: Collision Detection Issue
« on: October 19, 2013, 12:28:05 am »
The more I look at this the more it doesn't make sense to me.  Collision will always be true as far as I can tell because it's initially set to be true, and then all the conditions that would change it are inside of those loops, but once the loops cease to run collision will still be true.

Here is some code:
// set up AnimatesSprite
    AnimatedSprite animatedSprite(sf::seconds(0.13));
    animatedSprite.setAnimation(walkingAnimationDown);
    animatedSprite.pause();
    animatedSprite.setPosition(700, 400);
       
        bool collision;
    for(auto layer = mll.GetLayers().begin(); layer != mll.GetLayers().end(); ++layer)
    {
        if(layer->name == "Test")
        {
            for(auto object = layer->objects.begin(); object != layer->objects.end(); ++object)
            {
                                collision = object->Contains(animatedSprite.getPosition());
            }
        }
    }

    sf::Clock frameClock;

        sf::Music music;

        if(!music.openFromFile("Pallet Town Theme.ogg"))
                std::cout << "ERROR: Can't find Pallet Town Theme.ogg" << std::endl;

        music.play();
        music.setLoop(true);

    float speed = 0.045f;
    bool bNoKeyWasPressed = true;

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
            if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape)
                window.close();
                        if (event.type == sf::Event::LostFocus)
                        {
                                music.pause();
                               
                        }
                        if (event.type == sf::Event::GainedFocus)
                                music.play();
        }
               
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
        {
            if(animatedSprite.getAnimation() != &walkingAnimationUp)
            {
                 animatedSprite.setAnimation(walkingAnimationUp);
            }
            animatedSprite.play();
                        if(collision = false)
                                animatedSprite.move(0, -speed);
                        else
                                animatedSprite.move(0, 0);
            bNoKeyWasPressed = false;
        }
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
        {
            if(animatedSprite.getAnimation() != &walkingAnimationDown)
            {
                animatedSprite.setAnimation(walkingAnimationDown);
            }
            animatedSprite.play();
            animatedSprite.move(0, speed);
            bNoKeyWasPressed = false;
        }
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
        {
            if(animatedSprite.getAnimation() != &walkingAnimationRight)
            {
                animatedSprite.setAnimation(walkingAnimationRight);
            }
            animatedSprite.play();
            animatedSprite.move(speed, 0);
            bNoKeyWasPressed = false;
        }
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
        {
            if(animatedSprite.getAnimation() != &walkingAnimationLeft)
            {
                animatedSprite.setAnimation(walkingAnimationLeft);
            }
            animatedSprite.play();
            animatedSprite.move(-speed, 0);
            bNoKeyWasPressed = false;
        }

3
General / Re: Collision Detection Issue
« on: October 19, 2013, 12:22:30 am »
Let me place him outside of the map and test that...be right back.

....nope, still can't move even outside of the map boundaries.

4
General / Collision Detection Issue
« on: October 19, 2013, 12:15:44 am »
Hello everyone.  I am using the TMX Map Loader to load the .tmx files from the Tiled Map Editor.  In the documentation they have a section covering collision detection with this snippet of code:

bool collision;
    for(auto layer = mll.GetLayers().begin(); layer != mll.GetLayers().end(); ++layer)
    {
        if(layer->name == "Test")
        {
            for(auto object = layer->objects.begin(); object != layer->objects.end(); ++object)
            {
                                collision = object->Contains(point);
            }
        }
    }
 

For the line
collision = object->Contains(point);
, am I right to use something along the lines of animatedSprite.getPosition() in place of point?  Also, when I try to use that code to determine whether or not Collision is true or false, it is always true.  For example, if I make a move condition for my sprite stating that they can move if collision is false, else they cannot move, they will never be able to move.

I'm sure this didn't come out as clearly as it sounded in my head.  Any help with this would be greatly appreciated!


5
Graphics / Re: Sprite animation with inconsistant sprite sizes
« on: October 13, 2013, 02:20:40 pm »
Sounds like a plan!  I wasn't sure how complex the logic would have to be.  Much appreciated gentlemen.

6
Graphics / Sprite animation with inconsistant sprite sizes
« on: October 13, 2013, 07:28:29 am »
Hello everyone.  I'm currently trying to animate a sprite with SFML, but I'm running into an issue because all the guides I can find on it assume that every sprite is the same (16x16, 32x32, etc).  Mine are any 3 of these sizes:
 
 - 60x76
 - 56x76
 - 56x72

Any help with this would be greatly appreciated.  In case any of you are wondering, I attached the sprite sheet I'm using (making a Pallet Town clone from Pokemon Fire Red):



 

Pages: [1]