SFML и C++ Уроки \ Разработка игр › Форумы › SFML Graphics › Задача связанная частично с картой
Помечено: coordinate, map, sound, teleport
В этой теме 0 ответов, 1 участник, последнее обновление spoty76 6 года/лет, 8 мес. назад.
-
АвторСообщения
-
Здравствуйте, я создал звук телепортации после int main:
SoundBuffer teleportBuffer;
teleportBuffer.loadFromFile(“sounds/teleport.ogg”);
Sound teleport(teleportBuffer);и у меня есть в class player в interactionWIthMap:
if (TileMap[i][j] == ‘6’) {
x = 120; y = 920;
TileMap[i][j] = ‘ ‘;я хочу чтобы после перемещения координат у меня проигрывался teleport.play();, но intaraction with map выше находится от sound teleport (первое в class player, второе после int main); так вот как мне сделать чтобы звук играл после if (TileMap[i][j] == ‘6’) {
x = 120; y = 920; ?вот код от main.cpp: (есть view.h, map.h)
#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 = 0.5;
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 = 0.5;
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(224, 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));map_sprite.setPosition(j * 32, i * 32);
window.draw(map_sprite);
}window.draw(p.sprite);
window.draw(house_sprite);
window.display();
}return 0;
} -
АвторСообщения
Для ответа в этой теме необходимо авторизоваться.