Colors 前景碰撞

Colors 前景碰撞,colors,collision,sfml,foreground,intersect,Colors,Collision,Sfml,Foreground,Intersect,我一直在尝试在SFML上做一个RPG游戏,现在我在碰撞中挣扎。我的问题很简单,我有两层,背景和前景。背景只是作为背景图像,前景png图像应该作为碰撞,让角色应该完全自由行走的部分(透明)并保持其余结构用作碰撞(这里的背景图像:我使地板透明,同时保持其余部分)。我试着用getGlobalBounds().intesect来处理角色sprite,但没有成功。我想让它尽可能简单,以下是我到目前为止所拥有的: #包括 #包括“Piso1.h” 使用名称空间std; Piso1::Piso1(){ };

我一直在尝试在SFML上做一个RPG游戏,现在我在碰撞中挣扎。我的问题很简单,我有两层,背景和前景。背景只是作为背景图像,前景png图像应该作为碰撞,让角色应该完全自由行走的部分(透明)并保持其余结构用作碰撞(这里的背景图像:我使地板透明,同时保持其余部分)。我试着用getGlobalBounds().intesect来处理角色sprite,但没有成功。我想让它尽可能简单,以下是我到目前为止所拥有的:


#包括
#包括“Piso1.h”
使用名称空间std;
Piso1::Piso1(){
};
int Piso1::绘制(渲染窗口和窗口、事件和事件)
{
srand(时间(nullptr));
Soundtrack.openFromFile(“../Scenes/Piso1/Sounds/Neon District.wav”);
配乐设置循环(真);
配乐播放();
纹理;
BGTexture.loadFromFile(“../Scenes/Piso1/Graphics/piso1background.png”);
雪碧背景;
背景。setTexture(BGTexture);
背景.设置刻度(8,7.5);
背景。设置位置(BackX,BackY);
纹理;
FGTexture.loadFromFile(“../Scenes/Piso1/Graphics/piso1foreground.png”);
雪碧前景;
前台设置纹理(FGTexture);
前景.设置刻度(8,7.5);
前景。设置位置(BackX,BackY);
纹理纹理;
loadFromFile(“../Scenes/Piso1/Graphics/pSprite.png”);
IntRect-SpriteBx(0,0,34,47);
精灵主角(ProtateXtra,SpriteBx);
progationa.setPosition((window.getSize().x)/2.35,(window.getSize().y)/3);
主角a.设置刻度(3,3);
while(window.isOpen()){
while(window.pollEvent(evento)){
开关(事件类型){
案例事件::已结束:
window.close();
打破
案例事件::按键:
EncounterValue=rand()%1000;
如果(遇到值>5){
if(evento.key.code==键盘::向下){
BackY-=10;
背景。移动(0,-10);
移动(0,-10);
//这是我失败的尝试
if(protagona.getLocalBounds().intersects(Foreground.getLocalBounds()))
{
openFromFile(“../Scenes/Piso1/Sounds/oof.ogg”);
碰撞。播放();
BackY+=10;
背景。移动(0,10);
前景。移动(0,10);
}
如果(时钟1.getElapsedTime().asmillseconds()>64){
SpriteBx.top=0;
如果(SpriteBx.left==0)
SpriteBx.left=34;
else if(SpriteBx.left==34)
SpriteBx.left=68;
else if(SpriteBx.left==68)
SpriteBx.left=102;
其他的
SpriteBx.left=0;
主角a.setTextureRect(SpriteBx);
clock1.restart();
}
打破
}
else if(evento.key.code==键盘::向上){
BackY+=10;
背景。移动(0,10);
前景移动(0,10);
如果(时钟1.getElapsedTime().asmillseconds()>64)
{
SpriteBx.top=152;
如果(SpriteBx.left==0)
SpriteBx.left=34;
else if(SpriteBx.left==34)
SpriteBx.left=68;
else if(SpriteBx.left==68)
SpriteBx.left=102;
其他的
SpriteBx.left=0;
主角a.setTextureRect(SpriteBx);
clock1.restart();
}
打破
}
else if(evento.key.code==键盘::左){
BackX+=10;
背景。移动(10,0);
前景移动(10,0);
如果(时钟1.getElapsedTime().asmillseconds()>64)
{
SpriteBx.top=53;
如果(SpriteBx.left==0)
SpriteBx.left=34;
else if(SpriteBx.left==34)
SpriteBx.left=68;
else if(SpriteBx.left==68)
SpriteBx.left=102;
其他的
SpriteBx.left=0;
主角a.setTextureRect(SpriteBx);
clock1.restart();
}
打破
}
else if(evento.key.code==键盘::右){
BackX-=10;
背景移动(-10,0);
前景移动(-10,0);
如果(时钟1.getElapsedTime().asmillseconds()>64)
{
SpriteBx.top=104;
如果(SpriteBx.left==0)
SpriteBx.left=34;
else if(SpriteBx.left==34)
SpriteBx.left=68;
else if(SpriteBx.left==68)
SpriteBx.left=102;
其他的
#include <iostream>
#include "Piso1.h"

using namespace std;
Piso1::Piso1(){

};

int Piso1::Draw(RenderWindow &window, Event &evento)
{
srand(time(nullptr));
Soundtrack.openFromFile("../Scenes/Piso1/Sounds/Neon District.wav");
    Soundtrack.setLoop(true);
    Soundtrack.play();

Texture BGTexture;
    BGTexture.loadFromFile("../Scenes/Piso1/Graphics/piso1background.png");
Sprite Background;
    Background.setTexture(BGTexture);
    Background.setScale(8,7.5);
    Background.setPosition(BackX,BackY);


Texture FGTexture;
    FGTexture.loadFromFile("../Scenes/Piso1/Graphics/piso1foreground.png");
Sprite Foreground;
    Foreground.setTexture(FGTexture);
    Foreground.setScale(8,7.5);
    Foreground.setPosition(BackX,BackY);

Texture ProtaTextura;
    ProtaTextura.loadFromFile("../Scenes/Piso1/Graphics/pSprite.png");
    IntRect SpriteBx(0,0,34,47);
Sprite Protagonista(ProtaTextura,SpriteBx);
    Protagonista.setPosition((window.getSize().x)/2.35,(window.getSize().y)/3);
    Protagonista.setScale(3,3);

while (window.isOpen()) {
    while (window.pollEvent(evento)) {
        switch (evento.type) {
            case Event::Closed:
                window.close();
                break;
            case Event::KeyPressed:
                EncounterValue = rand()%1000;
                if(EncounterValue > 5){
                    if(evento.key.code == Keyboard::Down) {
                        BackY -= 10;
                        Background.move(0,-10);
                        Foreground.move(0,-10);
                        //this is my failed attempt
                        if(Protagonista.getLocalBounds().intersects(Foreground.getLocalBounds()))
                        {
                            Collision.openFromFile("../Scenes/Piso1/Sounds/oof.ogg");
                            Collision.play();
                            BackY += 10;
                            Background.move(0, 10);
                            Foreground.move(0, 10);
                        }

                        if(clock1.getElapsedTime().asMilliseconds()>64){
                            SpriteBx.top = 0;
                            if (SpriteBx.left == 0)
                                SpriteBx.left = 34;
                            else if (SpriteBx.left==34)
                                SpriteBx.left= 68;
                            else if (SpriteBx.left== 68)
                                SpriteBx.left= 102;
                            else
                                SpriteBx.left=0;
                            Protagonista.setTextureRect(SpriteBx);
                            clock1.restart();
                        }
                        break;
                    }
                    else if (evento.key.code == Keyboard::Up) {
                        BackY += 10;
                        Background.move(0,10);
                        Foreground.move(0,10);
                        if (clock1.getElapsedTime().asMilliseconds()>64)
                        {
                            SpriteBx.top = 152;
                            if (SpriteBx.left == 0)
                                SpriteBx.left = 34;
                            else if (SpriteBx.left==34)
                                SpriteBx.left= 68;
                            else if (SpriteBx.left== 68)
                                SpriteBx.left= 102;
                            else
                                SpriteBx.left=0;
                            Protagonista.setTextureRect(SpriteBx);
                            clock1.restart();
                        }
                        break;
                    }
                    else if(evento.key.code == Keyboard::Left) {
                        BackX += 10;
                        Background.move(10,0);
                        Foreground.move(10,0);
                        if (clock1.getElapsedTime().asMilliseconds()>64)
                        {
                            SpriteBx.top = 53;
                            if (SpriteBx.left == 0)
                                SpriteBx.left = 34;
                            else if (SpriteBx.left==34)
                                SpriteBx.left= 68;
                            else if (SpriteBx.left== 68)
                                SpriteBx.left= 102;
                            else
                                SpriteBx.left=0;
                            Protagonista.setTextureRect(SpriteBx);
                            clock1.restart();
                        }
                        break;
                    }
                    else if(evento.key.code == Keyboard::Right){
                        BackX -= 10;
                        Background.move(-10,0);
                        Foreground.move(-10,0);
                        if (clock1.getElapsedTime().asMilliseconds()>64)
                        {
                            SpriteBx.top = 104;
                            if (SpriteBx.left == 0)
                                SpriteBx.left = 34;
                            else if (SpriteBx.left==34)
                                SpriteBx.left= 68;
                            else if (SpriteBx.left== 68)
                                SpriteBx.left= 102;
                            else
                                SpriteBx.left=0;
                            Protagonista.setTextureRect(SpriteBx);
                            clock1.restart();
                        }
                        break;
                    }
                    else if(evento.key.code == Keyboard::C){
                        Soundtrack.stop();
                        return 1;
                    }
                }
                else{
                    Soundtrack.stop();
                    return 0;
                }
        }
        window.clear();
        window.draw(Foreground);
        window.draw(Background);
        window.draw(Protagonista);
        window.display();
    }
}
}
if(velocity.x > 0 && player.getLocalBounds().intersect(wall.getLocalBounds()) && player.getLocalBounds().left > wall.getLocalBounds().left)