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

Pages: [1]
1
General / Re: CircleShape movement not working??
« on: August 10, 2015, 05:21:21 am »
Thank you both and sorry for being such an amateur! I left the libraries out intentionally because I wasn't sure if they'd be annoying. Thank you for all your comments - I've been consolidating and playing with all the suggestions before replying. This is a big help and thank you for the link to the bouncing ball.

A couple of questions:

1) Why do you prefer random to rand?
2) Great point about not allowing a stone to be created less than Radius distance from a wall. Relatedly, I'm now wondering how I could make sure stones don't generate at a point where another stone is (at least in the initial positions)?
3) Is there a way to set up event triggers as a "key last pressed" instead of having to have the key remain pressed?
4) I've tried and failed to add a texture to the balls. From the documentation, it seems like the problem might be the template becomes disassociated with the object upon its return to main. Is writing a copy constructor inside Stone with the template the best solution to this?

2
General / Re: CircleShape movement not working??
« on: August 09, 2015, 11:37:31 pm »
Sorry for my delayed response. I hear you all and have updated the original code to be as minimal as possible and compilable. There's a lot of code re: collision checking and other features that had to be cut out so sorry for the original mistakes.

As is, you can compile and see that the stones aren't displaying in the correct number before the events, and are moving when events are called but very sporadically with only 1-2 circles show at a time. The draw and display ARE outside of the event loop, sorry it was hard to tell. I actually don't want the stones to move or display unless either event is called, so the starting screen should just be the target and the draw/update functions should probably all be inside events. But for purposes of testing I've left them outside of  events here. It may just be my numbers that are screwed up and I've looked at it for too long to tell.

3
General / Re: CircleShape movement not working??
« on: August 07, 2015, 01:38:26 pm »
Ok, have fixed the nested if and removed the display. Same issue - only 1 stone displaying now and no reaction to key presses.

Re: the link, sorry, what should I fix to improve? Length? Location? I didn't think this was related to a specific module since I don't know where the problem originates. Should I move to Window?

4
General / CircleShape movement not working??
« on: August 07, 2015, 01:14:15 pm »
Hello,

I am trying to generate 10 circles at random positions and locations across the screen, and for some reason the movement isn't working (not sure if the use of rand is causing an issue).

The events are supposed to represent 2 different modes, where in one the circles should bounce off the screen bounds, and in the second they should reappear at a random position and velocity. The circles simply flicker and then reappear at random locations, and never more than 5-6 of them.

I've tried both drawing the circles inside and outside of the events thinking I wasn't understanding the process. I've also seen many of you keep track of time for movement, but I've written another fully functional SFML program that works almost exactly the same without any use of time or the clock, the only difference being there's only one circle being moved. I'm a complete beginner and have looked at this so many times that my eyes are glazing over, so sorry if this is absurdly obvious.

using namespace std;

float SIZE = 750.f;

float const stoneRadius = 30.f;

struct Stone
{
    sf::CircleShape st;
    sf::Vector2f velocity;

    Stone(float x, float y, float p1, float p2)
    {

        sf::Vector2f velocity (x,y);
        st.setRadius(stoneRadius);
        st.setFillColor(sf::Color::Black);
        st.setOrigin(stoneRadius, stoneRadius);
        st.setPosition(p1, p2);
    }


    void update_bounce()
    {

        st.move(velocity);

        if (st.getPosition().x - stoneRadius <= 0)
            velocity.x = -velocity.x;
        else if ((st.getPosition().x + stoneRadius) >= SIZE)
            velocity.x = -velocity.x;


        if (st.getPosition().y - stoneRadius <= 0)
            velocity.y = -velocity.y;
        else if (st.getPosition().y + stoneRadius >= SIZE)
            velocity.y = -velocity.y;
    }


    void update()

    {
        srand ( time(NULL) );

        st.move(velocity);

        if ( (st.getPosition().x - stoneRadius) <= 0 || (st.getPosition().x + stoneRadius) <= SIZE
                || (st.getPosition().y - stoneRadius) <= 0 || (st.getPosition().y - stoneRadius >= SIZE) )
        {

            float v = rand() % 4 + (6);
            float p1 = rand () % 1500 + (-750);
            float p2 = rand () % 1500 + (-750);

            velocity.x = v;
            velocity.y = v;
            st.setPosition(p1, p2);
        }

    }
};


int main()
{
    srand ( time(NULL) );

    sf::RenderWindow window(sf::VideoMode(SIZE, SIZE), "SFML Window");

    window.setFramerateLimit(30);


    sf::CircleShape house[4];
    sf::Color colors[4] = {sf::Color::Blue, sf::Color::White, sf::Color::Red, sf::Color::White};
    float sizes[4] = {300, 200, 100, 25};

    for(int i=0; i<4; i++)
    {
        house[i].setPosition(SIZE/2.f, SIZE/2.f);
        house[i].setRadius(sizes[i]/2.f);
        house[i].setFillColor(colors[i]);
        house[i].setOrigin(sizes[i]/2.f, sizes[i]/2.f);
    }

    vector<Stone> gameStones;

    for (int x = 0; x< 10; x++)
    {
        float v =  rand() % 12 + (-6);
        float p1 = rand() % 1500 + (-750);
        float p2 = rand() % 1500 + (-750);
        gameStones.push_back(Stone(v,v,p1,p2));
    }


    while(window.isOpen())
    {

        sf::Event event;

        while (window.pollEvent(event))

        {

            if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Q)

            {

                window.close();
                break;
            }

            else if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::R)

            {

                for (int i=0; i<10; i++)
                    gameStones[i].update();


                }


            else if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::B)

            {

                for (int i=0; i<10; i++)
                    gameStones[i].update_bounce();


            }

        }

        window.clear(sf::Color::White);

        for (auto& gameStone : gameStones)
            window.draw(gameStone.st);

        for (int i=0; i<4; i++)
            window.draw(house[i]);

        window.display();

    }

    return 0;

}
 

Pages: [1]