SFML Sprite 在第二次滴答时剧烈向下移动

发布于 2024-10-04 17:57:00 字数 3776 浏览 4 评论 0原文

我正在开发一款 SFML 游戏,由于某种原因,在生成玩家后,玩家会被传送下来。在第一个刻度上,他的位置正确,但之后,他被向下移动。关于如何诊断这个问题有什么想法吗?我在移动机制中设置了断点,这是玩家坐标发生变化的唯一地方,而且它似乎在函数结束后立即发生。这是主要函数:

int main(){
App.ShowMouseCursor(false);
mainch.mainchinventory.read();
while (App.IsOpened())
{
    sf::Event Event;
    while (App.GetEvent(Event))
    {
        if (Event.Type == sf::Event::Closed)
            App.Close();
    }

    float time = App.GetFrameTime();
    mainch.move(time);
    App.Clear();
    drawall();
    App.Display();

}
return EXIT_SUCCESS;
}

Mainch.move(t):

void cmainchar::move(float t){
if (App.GetInput().IsKeyDown(sf::Key::S)) mainchinventory.save();

if (App.GetInput().IsKeyDown(sf::Key::R)) mainchinventory.read();

if (App.GetInput().IsKeyDown(sf::Key::A)) A = true;
else A = false;

if (App.GetInput().IsKeyDown(sf::Key::D)) D = true;
else D = false;

if(grounded)
    if (App.GetInput().IsKeyDown(sf::Key::W)) first = true;

if ((App.GetInput().IsKeyDown(sf::Key::I)) && (keyreleased)){
    if (mainchinventory.drawmain){
        mainchinventory.drawmain = false;
        mainchinventory.press(mainchinventory.selectionx, 3);
    }

    else{
        mainchinventory.drawmain = true;
    }

keyreleased = false;
}

else if (!App.GetInput().IsKeyDown(sf::Key::I))
    keyreleased = true;



if(!mainchinventory.drawmain){
    if(App.GetInput().IsKeyDown(sf::Key::Num1)) mainchinventory.press(0, 3);
    else if(App.GetInput().IsKeyDown(sf::Key::Num2)) mainchinventory.press(1, 3);
    else if(App.GetInput().IsKeyDown(sf::Key::Num3)) mainchinventory.press(2, 3);
    else if(App.GetInput().IsKeyDown(sf::Key::Num4)) mainchinventory.press(3, 3);
    else if(App.GetInput().IsKeyDown(sf::Key::Num5)) mainchinventory.press(4, 3);
    else if(App.GetInput().IsKeyDown(sf::Key::Num6)) mainchinventory.press(5, 3);
    else if(App.GetInput().IsKeyDown(sf::Key::Num7)) mainchinventory.press(6, 3);
    else if(App.GetInput().IsKeyDown(sf::Key::Num8)) mainchinventory.press(7, 3);
    else if(App.GetInput().IsKeyDown(sf::Key::Num9)) mainchinventory.press(8, 3);
    else if(App.GetInput().IsKeyDown(sf::Key::Num0)) mainchinventory.press(9, 3);

    if(App.GetInput().IsMouseButtonDown(sf::Mouse::Button::Left)){
        mainchinventory.dockitems[mainchinventory.selectionx].use();
        spells.push_back(cspell());
    }
}

else if ((App.GetInput().IsMouseButtonDown(sf::Mouse::Button::Left)) && (mainchinventory.drawmain) && (buttonreleased)){
    mainchinventory.checkmouse();
    buttonreleased = false;
}


else if (!App.GetInput().IsMouseButtonDown(sf::Mouse::Button::Left))
    buttonreleased = true;

int xmap = (View.GetCenter().x - 320) / 40;
int ymap = (View.GetCenter().y - 240) / 40;

if ((xmap != xmapold) || (ymap != ymapold))
    maps.read();

xmapold = xmap;

ymapold = ymap;

collisions();

for(std::list<cspell>::iterator i = mainch.spells.begin(); i != mainch.spells.end(); i++) 
    if (i->move(t))
        spells.erase(i);

if (A && left){
    animate(2, t);
    you.Move(-160 * t, 0);
}
if (D && right){
    animate(1, t);
    you.Move(160 * t, 0);
}

if (!D && !A)
    animate(0, t);

if (up){
    if(grounded && first){
        jump = 1.25;
        first = false;
    }
    if (jump > 0){
        you.Move (0,-250 * t * jump);
        jump = jump - 1 * t;
    }
    if (jump <= 0){
        jump = 0.f;
    }
}

else{
    first = false;
    jump = 0.f;
}


if (down){
    fall = fall + 10 * t;
    you.Move(0, 25 * fall * t);
    grounded = false;
}

else{
    fall = 0.f;
    grounded = true;
}

if(teleport){
    mainchinventory.spawn = true;
    fall = 0.f;
    jump = 0.f;
    maps.changemap(maps.nxtmap);
    teleport = false;
}

moveview();
}

I am working on an SFML game and for some reason after spawning the player, the player gets teleported down. On the first tick he is correctly positioned, but after wards, he is moved down. Any idea on how to diagnose this problem? I set up breakpoints in the move mechanism, the only place where the player's co-ordinates change, and it seems to happen right after the function ends. This is the main function:

int main(){
App.ShowMouseCursor(false);
mainch.mainchinventory.read();
while (App.IsOpened())
{
    sf::Event Event;
    while (App.GetEvent(Event))
    {
        if (Event.Type == sf::Event::Closed)
            App.Close();
    }

    float time = App.GetFrameTime();
    mainch.move(time);
    App.Clear();
    drawall();
    App.Display();

}
return EXIT_SUCCESS;
}

Mainch.move(t):

void cmainchar::move(float t){
if (App.GetInput().IsKeyDown(sf::Key::S)) mainchinventory.save();

if (App.GetInput().IsKeyDown(sf::Key::R)) mainchinventory.read();

if (App.GetInput().IsKeyDown(sf::Key::A)) A = true;
else A = false;

if (App.GetInput().IsKeyDown(sf::Key::D)) D = true;
else D = false;

if(grounded)
    if (App.GetInput().IsKeyDown(sf::Key::W)) first = true;

if ((App.GetInput().IsKeyDown(sf::Key::I)) && (keyreleased)){
    if (mainchinventory.drawmain){
        mainchinventory.drawmain = false;
        mainchinventory.press(mainchinventory.selectionx, 3);
    }

    else{
        mainchinventory.drawmain = true;
    }

keyreleased = false;
}

else if (!App.GetInput().IsKeyDown(sf::Key::I))
    keyreleased = true;



if(!mainchinventory.drawmain){
    if(App.GetInput().IsKeyDown(sf::Key::Num1)) mainchinventory.press(0, 3);
    else if(App.GetInput().IsKeyDown(sf::Key::Num2)) mainchinventory.press(1, 3);
    else if(App.GetInput().IsKeyDown(sf::Key::Num3)) mainchinventory.press(2, 3);
    else if(App.GetInput().IsKeyDown(sf::Key::Num4)) mainchinventory.press(3, 3);
    else if(App.GetInput().IsKeyDown(sf::Key::Num5)) mainchinventory.press(4, 3);
    else if(App.GetInput().IsKeyDown(sf::Key::Num6)) mainchinventory.press(5, 3);
    else if(App.GetInput().IsKeyDown(sf::Key::Num7)) mainchinventory.press(6, 3);
    else if(App.GetInput().IsKeyDown(sf::Key::Num8)) mainchinventory.press(7, 3);
    else if(App.GetInput().IsKeyDown(sf::Key::Num9)) mainchinventory.press(8, 3);
    else if(App.GetInput().IsKeyDown(sf::Key::Num0)) mainchinventory.press(9, 3);

    if(App.GetInput().IsMouseButtonDown(sf::Mouse::Button::Left)){
        mainchinventory.dockitems[mainchinventory.selectionx].use();
        spells.push_back(cspell());
    }
}

else if ((App.GetInput().IsMouseButtonDown(sf::Mouse::Button::Left)) && (mainchinventory.drawmain) && (buttonreleased)){
    mainchinventory.checkmouse();
    buttonreleased = false;
}


else if (!App.GetInput().IsMouseButtonDown(sf::Mouse::Button::Left))
    buttonreleased = true;

int xmap = (View.GetCenter().x - 320) / 40;
int ymap = (View.GetCenter().y - 240) / 40;

if ((xmap != xmapold) || (ymap != ymapold))
    maps.read();

xmapold = xmap;

ymapold = ymap;

collisions();

for(std::list<cspell>::iterator i = mainch.spells.begin(); i != mainch.spells.end(); i++) 
    if (i->move(t))
        spells.erase(i);

if (A && left){
    animate(2, t);
    you.Move(-160 * t, 0);
}
if (D && right){
    animate(1, t);
    you.Move(160 * t, 0);
}

if (!D && !A)
    animate(0, t);

if (up){
    if(grounded && first){
        jump = 1.25;
        first = false;
    }
    if (jump > 0){
        you.Move (0,-250 * t * jump);
        jump = jump - 1 * t;
    }
    if (jump <= 0){
        jump = 0.f;
    }
}

else{
    first = false;
    jump = 0.f;
}


if (down){
    fall = fall + 10 * t;
    you.Move(0, 25 * fall * t);
    grounded = false;
}

else{
    fall = 0.f;
    grounded = true;
}

if(teleport){
    mainchinventory.spawn = true;
    fall = 0.f;
    jump = 0.f;
    maps.changemap(maps.nxtmap);
    teleport = false;
}

moveview();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

就像说晚安 2024-10-11 17:57:00

您没有显示操作 sf::Sprite 对象的代码,所以我(或任何其他人,真的)不能肯定地说,但是......也就是说,我有一个强烈的猜测是什么正在发生。

我最近也在熟悉SFML,我自己也遇到了这个问题。我认为正在发生的事情是您调用 sf::Sprite::Move(x,y) 将精灵移动到位置 (x,y)。这是不正确的;你应该调用的是 sf::Sprite::SetPosition(x,y)。 (顺便说一句,这两个函数都将采用二维向量作为参数)。

我假设您使用的是 SFML 1.6,是吗?看起来是这样...2.0 版本对 API 做了一些改变,所以如果您使用的是 Sprite::Move() 则变为 Sprite::move(),而 Sprite::SetPosition() 则变为 Sprite::setPosition()。

无论如何,总结一下:Move(x,y) 和 SetPosition(x,y) 之间的区别在于 Move 调整精灵的位置相对于其当前位置,而 SetPosition 将精灵移动到新的位置,无论之前在哪里。

再说一次,这是一个盲目的猜测,因为没有包含相关代码......那么我在黑暗中的拍摄是否正确?

You don't show the code where you're manipulating the sf::Sprite object, so I (or anyone else, really) can't say for certain, but...that said, I have a strong guess as to what's happening.

I've been familiarizing myself with SFML recently, too, and I encountered this issue myself. What I think is happening is you're calling sf::Sprite::Move(x,y) to move the sprite to position (x,y). This is incorrect; what you should be calling is sf::Sprite::SetPosition(x,y). (Both these functions take will take a 2d vector as an argument instead, btw).

I'm operating under the assumption you're using SFML 1.6, yes? Looks that way...version 2.0 changes the API somewhat, so in case you're using that Sprite::Move() becomes Sprite::move() and Sprite::SetPosition() becomes Sprite::setPosition().

Anyways, to wrap this up: the difference between Move(x,y) and SetPosition(x,y) is that Move adjusts the sprite's position relative to its current position, whereas SetPosition moves the sprite to a new position regardless of where it was before.

Again, this is a blind guess since the relevant code was not included...so was my shot in the dark correct?

葬花如无物 2024-10-11 17:57:00

不幸的是,您没有提供足够的代码来进行简单的诊断。然而,根据您提供的内容,我最好的猜测是您的 fall 变量尚未初始化。如果尚未初始化,则其值可能是完全随机的,可能比预期大得多。这可以解释你的“传送”行为。

Unfortunately, you haven't provided enough code for a straightforward diagnosis. With what you provided however, my best guess is that your fall variable hasn't been initialized. If it hasn't been initialized, its values can be completely random, likely being much larger than expected. This would explain your 'teleporting' behavior.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文