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

Pages: 1 [2] 3
16
Audio / Re: How can I know if the song ended or not?
« on: June 08, 2014, 02:44:07 am »
Thanks, dude! All that I had to do was change Music::Stopped to SoundSource::Stopped. Thanks!

17
Audio / How can I know if the song ended or not?
« on: June 08, 2014, 01:15:58 am »
Let's say I have a Music object and I load a certain song to it:

Code: [Select]
sf::Music song;
song.openFromFile("path.wav");
song.play();

I want to know if a song ended playing or not. If it did I want to load another song. I tried doing something like this:

Code: [Select]
if (song.getStatus() == sf::Music::Status::Stopped) {
   song.openFromFile("anothersong.wav");
   song.play();
}

But it doesn't work and the problem seems to be with the condition. This one doesn't say me if the song ended. When the song ends status should be set to stopped, right? I thought so and I thought that my code would work, but it doesn't. I tried several things (I thought about checking song duration, and counting time) and I checked if getStatus() method returns the right value (when I press "S" the song stops playing and its status is set to Stopped, when I press "P" the song starts playing and its status is set to Playing - that works fine, but seems that when the song ends status is still set to Playing). How can I check if song ended playing?

18
Graphics / Re: Weird crash when drawing sprite
« on: November 27, 2013, 11:59:46 pm »
Yeah, sorry, I meant destructor, not deconstructor :)

I meant that Run method should be void and later called in main like this:

Code: [Select]
test.Run();
And main ends with return 0 or return EXIT_SUCCESS... But if it's ok, then ok.

19
Graphics / Re: Weird crash when drawing sprite
« on: November 27, 2013, 11:20:36 pm »
Code: [Select]
this->sprite = Sprite(tex);
This seems very strange to me. Though I'm not very good at SFML, I think it shoud look something like this:

Code: [Select]
sprite.setTexture(tex);
Probably you don't need to use "this" here. And

Code: [Select]
Sprite sprite;
should go to private section of class. Also, why do you do this?:

Code: [Select]
return test.Run();
Shouldn't this method be void, not int?

I assume you have a constructor, deconstructor...?

20
Graphics / Re: Sprite's texture doesn't change properly
« on: November 27, 2013, 08:33:15 pm »
My understanding of this code is:

We take a node (w[]). First, let's check, if user clicked on this node. If yes, then let's check, if there already is a "clicked" node somewhere - if yes, then change its "clicked" value to "false". Then we can set "clicked" value of our clicked node to "true".
Now, let's check, if our node (w[]) is "clicked", and if yes, then change its texture:
Code: [Select]
if (w[i].clicked)
            w[i].tex=CLICKED;
if it's not "clicked", then just check, if user clicked on this node earlier:
Code: [Select]
else {
            if (w[i].visited)
                w[i].tex=VISITED;
            else
                w[i].tex=NOTVISITED;
        }

I tried to add:
Code: [Select]
w[j].tex=VISITED;here:
Code: [Select]
for (int j=0; j<size; j++) {
                if (w[j].clicked) { w[j].clicked=false; w[j].tex=VISITED; break; }
            }
But this didn't help. Maybe I should change textures somewhere else?

21
Graphics / Sprite's texture doesn't change properly
« on: November 27, 2013, 07:27:15 pm »
Hi,

I have two classes - Graph and Node. In Graph class I declare an array of Nodes. In the Update method of class Graph I have a loop for checking each node... So, I have 25 nodes. At the beginning, each node has a texture "NOTVISITED". If I click on a node it should change its texture to "CLICKED". When I click on another node, my first node changes its texture to "VISITED" and the now-clicked one changes texture to "CLICKED". That works ok, but there's one problem. Namely, if there's a node with a texture "CLICKED" and I click on another node, first node changes texture to "VISITED", but just after I do some mouse movement or I click again... And it should change its texture to "VISITED" right after clicking on another node. I'm just learning coding 2d apps and I have no idea how to fix it. Could someone help me?

Here's the code of Graph::Update:

Code: [Select]
for (int i=0; i<size; i++) {
        if (gm.MouseClicked(w[i].getSpr(), win, ev)) { //win - window, ev - event
            for (int j=0; j<size; j++) {
                if (w[j].clicked) { w[j].clicked=false; break; }
            }
            if (!w[i].visited) w[i].visited=true;

            w[i].clicked=true;
        }
        if (w[i].clicked)
            w[i].tex=CLICKED;
        else {
            if (w[i].visited)
                w[i].tex=VISITED;
            else
                w[i].tex=NOTVISITED;
        }
        w[i].Update(win, gm, ev); //gm - gameobject
    }

w[] is my array of Nodes

22
Graphics / Re: Collision detection question
« on: August 19, 2013, 03:38:41 pm »
Yeah, it looks good ;) I'll do some more testing to be sure it works ok, but thanks again!

23
Graphics / Re: Collision detection question
« on: August 19, 2013, 03:25:47 pm »
So, right after
Code: [Select]
if (bound.intersects(sprbound))I should add
Code: [Select]
spr.setPosition(spr.getPosition().x, pad.getsprite().getPosition().y - spr.getTextureRect().height);Is that right? Seems to work fine :) I already tried this, but later decided to write own collision detection system... Thanks!

24
Graphics / Re: Collision detection question
« on: August 19, 2013, 01:03:40 pm »
OK, I had a problem with collision detection... And I still have this problem. I wrote this simple collision detection:

Code: [Select]
sf::FloatRect bound = pad.getsprite().getGlobalBounds();

sf::FloatRect sprbound = spr.getGlobalBounds();
if (bound.intersects(sprbound)) {
                //bounce
vec = 180.0f - vec;
yspeed = -yspeed;
}

Where "spr" is my ball sprite and "pad" is a reference to the Paddle object.

But the problem is that sometimes when ball hits the very top of the paddle, the ball starts to behave "jittery". I think it shouldn't behave like that, I tried many ways to fix this (including own collision detection system), but nothing seems to work. What's wrong?

25
Graphics / Re: Collision detection question
« on: August 18, 2013, 05:56:23 pm »
3 rectangles ;) I rather don't need fourth rectangle for the bottom of the paddle.

Height of intersection - you mean this: mysprite.getGlobalBounds().height?

26
Graphics / Collision detection question
« on: August 18, 2013, 05:30:11 pm »
Hi,

I'm writing a simple arkanoid game with SFML 2.1. I have a question about collision detection. How to detect which side of the paddle (top, left, right) the ball hit?

27
System / Re: Propes use of Clock and Time for smooth movement
« on: August 12, 2013, 04:17:51 pm »
OK, I added clock.restart() right after the game loop start. Now the movement is much more smooth. Thanks!

28
System / Re: Propes use of Clock and Time for smooth movement
« on: August 12, 2013, 04:01:36 pm »
I get it...

Code: [Select]
float elapsed = time.asSeconds();
Paddle pad((w-100)/2,(h-20-5),100,20,1000);

Instead of:

Code: [Select]
float elapsed = time.asMicroseconds();
Paddle pad((w-100)/2,(h-20-5),100,20,1);

Right? But that doesn't solve the problem...

29
System / Re: Propes use of Clock and Time for smooth movement
« on: August 12, 2013, 03:45:50 pm »
You mean something like this?

In the move method:
Code: [Select]
speed += 2.0

30
System / Re: Propes use of Clock and Time for smooth movement
« on: August 12, 2013, 03:08:26 pm »
Unfortunately, it doesn't solve the problem. I used Seconds, but I couldn't even move the paddle with it, that's why I used Microseconds.

Entire code? Here it is.

shapes.h
Code: [Select]
#ifndef shapes_h
#define shapes_h

#include <SFML/Graphics.hpp>

class Paddle
{
private:
int x, y;
int width, height;
float speed;
sf::RectangleShape rect;

protected:
sf::RectangleShape& get();

public:
Paddle(int nx, int ny, int nwidth, int nheight, float nspeed);
~Paddle();
void init();
void draw(sf::RenderWindow& win);
void move(float);
};

#endif

shapes.cpp
Code: [Select]
#include "shapes.h"

Paddle::Paddle(int nx, int ny, int nwidth, int nheight, float nspeed) { x=nx; y=ny; width=nwidth; height=nheight, speed=nspeed; }

Paddle::~Paddle() { }

sf::RectangleShape& Paddle::get()
{
return rect;
}

void Paddle::init()
{
rect.setPosition(x, y);
rect.setSize(sf::Vector2f(width,height));
rect.setFillColor(sf::Color(255,255,255));
}

void Paddle::draw(sf::RenderWindow& win)
{
win.draw(rect);
}

void Paddle::move(float elapsed)
{
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) get().move(speed * elapsed * -1, 0);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) get().move(speed * elapsed, 0);
}

main.cpp
Code: [Select]
#include <SFML\Graphics.hpp>
#include "shapes.h"

using namespace sf;

int main()
{
int w=800, h=600;
RenderWindow window(VideoMode(w,h), "Breakout");
window.setVerticalSyncEnabled(true);

Clock clock;
Time time = clock.getElapsedTime();
float elapsed = time.asMicroseconds();

Paddle pad((w-100)/2,(h-20-5),100,20,5);
pad.init();

while (window.isOpen()) {
Event evt;
while (window.pollEvent(evt)) {
if ((Keyboard::isKeyPressed(Keyboard::Escape)) || (evt.type == Event::Closed)) { window.close(); }
pad.move(elapsed);
clock.restart();
}

window.clear(Color::Black);
pad.draw(window);
window.display();
}

return 0;
}

Pages: 1 [2] 3