Hello, I got windows 10, vis. studio 2013, sfml 2.4.2: so, I have just made a test map using massives (the code is bellow). I've made some interaction with map in class Player and the thing is that I want to play a sound when the player touches an object, but the problem with that is I make a sound variable in the Int main, and the interaction with map is above the int main. I tried to creat sound variable right there, in class player, but this didn't work. Then the other thing I would like to understand is how to play sound when I press a keyboard button, for example when the player walks I want him to say smth, but the sound only starts and starts because the keyboard key is pressed, and I want the sound to play once the key WAS pressed, plz help!
I want to put sound here:
if (TileMap[i][j] == '6') {x = 120; y = 920; [b]teleport.play();[/b] TileMap[i][j] == ' '}
and here:
if (Keyboard::isKeyPressed(Keyboard::A)) {p.dir = 0; p.speed = 2;teleport.play();}
P.s. i got three source file: view.h, map.h, main.cpp:
main.cpp#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include "map.h"
#include "view.h"
using namespace sf;
class Player {
private: float x, y;
public:
float w, h, dx, dy, speed = 0;
int dir = 0;
String File;
Image image;
Texture texture;
Sprite sprite;
Player(String F, int X, int Y, float W, float H) {
File = F;
w = W; h = H;
image.loadFromFile("images/" + File);
image.createMaskFromColor(Color(41, 33, 59));
texture.loadFromImage(image);
sprite.setTexture(texture);
x = X; y = Y;
sprite.setTextureRect(IntRect(w, h, w, h));
}
void update(float time)
{
switch (dir)
{
case 0: dx = speed; dy = 0; break;
case 1: dx = -speed; dy = 0; break;
case 2: dx = 0; dy = speed; break;
case 3: dx = 0; dy = -speed; break;
}
x += dx*time;
y += dy*time;
speed = 0;
sprite.setPosition(x, y);
interactionWithMap();
}
float getplayercoordinateX(){
return x;
}
float getplayercoordinateY(){
return y;
}
void interactionWithMap()
{
for (int i = y / 32; i < (y + h) / 32; i++)
for (int j = x / 32; j < (x + w) / 32; j++)
{
if (TileMap[i][j] == '3')
{
if (dy>0)
{
y = i * 32 - h;
}
if (dy < 0)
{
y = i * 32 + 32;
}
if (dx>0)
{
x = j * 32 - w;
}
if (dx < 0)
{
x = j * 32 + 32;
}
}
if (TileMap[i][j] == '1') {
x = 100; y = 675;
sprite.setScale(Vector2f(4, 4));
y = 392;
view.setSize(1380, 820);
TileMap[i][j] = ' ';
}
}
for (int i = y / 32; i < (y + h) / 32; i++)
for (int j = x / 32; j < (x + w) / 32; j++)
{
if (TileMap[i][j] == '3')
{
if (dy>0)
{
y = i * 32 - h;
}
if (dy < 0)
{
y = i * 32 + 32;
}
if (dx>0)
{
x = j * 32 - w;
}
if (dx < 0)
{
x = j * 32 + 32;
}
}
if (TileMap[i][j] == '6') {
x = 120; y = 920;
TileMap[i][j] = ' ';
}
}
}
};
int main()
{
sf::RenderWindow window(sf::VideoMode(1980, 1080), "Glinskii Adventures");
view.reset(sf::FloatRect(0, 0, 640, 400));
Image map_image;
map_image.loadFromFile("images/map.png");
Texture map;
map.loadFromImage(map_image);
Sprite map_sprite;
map_sprite.setTexture(map);
Image house_image;
house_image.loadFromFile("images/house.png");
house_image.createMaskFromColor(Color(255, 255, 255));
Texture house;
house.loadFromImage(house_image);
Sprite house_sprite;
house_sprite.setTexture(house);
house_sprite.setPosition(100, 539);
SoundBuffer teleportBuffer;
teleportBuffer.loadFromFile("sounds/teleport.ogg");
Sound teleport(teleportBuffer);
SoundBuffer claySongBuffer;
claySongBuffer.loadFromFile("sounds/claySong.ogg");
Sound claySong(claySongBuffer);
claySong.play();
Player p("hero.png", 100, 675, 94.0, 96.0);
float CurrentFrame = 0;
Clock clock;
while (window.isOpen())
{
float time = clock.getElapsedTime().asMicroseconds();
clock.restart();
time = time / 800;
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
if (Keyboard::isKeyPressed(Keyboard::D)) {
p.dir = 0; p.speed = 2;
CurrentFrame += 0.005*time;
if (CurrentFrame > 3) CurrentFrame -= 3;
p.sprite.setTextureRect(IntRect(97 * int(CurrentFrame), 192, 94, 96));
getplayercoordinateforview(p.getplayercoordinateX(), p.getplayercoordinateY());
}
if (Keyboard::isKeyPressed(Keyboard::A)) {
p.dir = 1; p.speed = 2;
CurrentFrame += 0.005*time;
if (CurrentFrame > 3) CurrentFrame -= 3;
p.sprite.setTextureRect(IntRect(96 * int(CurrentFrame), 96, 94, 96));
getplayercoordinateforview(p.getplayercoordinateX(), p.getplayercoordinateY());
}
p.update(time);
viewmap(time);
changeview();
window.setView(view);
window.clear();
for (int i = 0; i < HEIGHT_MAP; i++)
for (int j = 0; j < WIDTH_MAP; j++)
{
if (TileMap[i][j] == '3') map_sprite.setTextureRect(IntRect(96, 0, 32, 32));
if (TileMap[i][j] == '0') map_sprite.setTextureRect(IntRect(128, 0, 32, 32));
if (TileMap[i][j] == '2') map_sprite.setTextureRect(IntRect(160, 0, 32, 32));
if (TileMap[i][j] == ' ') map_sprite.setTextureRect(IntRect(192, 0, 32, 32));
if (TileMap[i][j] == '1') map_sprite.setTextureRect(IntRect(0, 0, 32, 32));
if (TileMap[i][j] == 'g') map_sprite.setTextureRect(IntRect(64, 0, 32, 32));
if (TileMap[i][j] == '6') map_sprite.setTextureRect(IntRect(0, 0, 32, 32));
if (TileMap[i][j] == '7') map_sprite.setTextureRect(IntRect(0, 0, 32, 32));
map_sprite.setPosition(j * 32, i * 32);
window.draw(map_sprite);
}
window.draw(p.sprite);
window.draw(house_sprite);
window.display();
}
return 0;
}
[b]map.h[/b]
#include <SFML\Graphics.hpp>
const int HEIGHT_MAP = 33;
const int WIDTH_MAP = 194;
sf::String TileMap[HEIGHT_MAP] = {
" 0",
" 7777 7 777 7 7 3",
" 7 7 7 7 7 3",
" 7 7 7 7 77 3",
" 7 7 7 7 77 7 3",
" 77 7 777 777 7 7 3",
" 3",
" 7 777 7 7 3",
" 7 7 7 7 7 7 3",
" 777 7 7 7 7 3",
" 7 7 7 7 7 7 3",
" 7 7 777 7 7 3",
" 3",
" 3",
" 3",
"3 3",
"3 3",
"3 3",
"3 3",
"3 3",
"3 3",
"3 3",
"3 3",
"3 6 3",
"22222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222",
"gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg",
"gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg",
"gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg",
"3 3 ",
"3 3 ",
"3 3 ",
"3 1 3 ",
"33333333333333333333333333333333333333333333333333 ",
};