您现在的位置是:首页 > 电脑 > 

处理鼠标事件SFML(Handle mouse events SFML)

2025-07-14 11:49:21
处理鼠标事件SFML(Handle mouse events SFML) 我在SFML中的事件有些麻烦。 我正在制作一个基于转弯的游戏,当鼠标移动或点击鼠标左键时我检查它是否转动它然后在该物体位置产生一个射弹,当射弹与地形或对手相撞时它会被摧毁并转向改变了。 这种行为不是我所期待的。 当点击射击时,弹丸有时根本不会产生(并且它会立即改变转弯)。 我已经禁用
处理鼠标事件SFML(Handle mouse events SFML)

我在SFML中的事件有些麻烦。 我正在制作一个基于转弯的游戏,当鼠标移动或点击鼠标左键时我检查它是否转动它然后在该物体位置产生一个射弹,当射弹与地形或对手相撞时它会被摧毁并转向改变了。

这种行为不是我所期待的。 当点击射击时,弹丸有时根本不会产生(并且它会立即改变转弯)。 我已经禁用了所有碰撞,所以它不能这样。 我90%肯定问题是我如何处理事件,所以id真的很好地投入了如何让它变得更好。

我学到的是你不应该在while poll事件中执行函数,它只用于记录最近发生的事情,所以我把它们放在外面。 虽然这不能解决我的问题......

sf::Event event; sf::Vector2i mousePos; while (_window.pollEvent(event)) { if ( == sf::Event::Closed) { _(); } if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) { _(); } if( == sf::Event::MouseMoved) { // <- this is how you check to see if an event mousePos = sf::Mouse::getPosition(); moved = true; } if( == sf::Event::MouseButtonPressed) { // <- this is how you check to see if an event mousePressed = true; } } if (mousePressed && tank1Turn) { sf::Vector2f spawnPos = _t1->getPos() + sf::Vector2f(0, -150); sf::Vector2f initVel = _t1->getInitVel(); cout << endl; _p = new Projectile(spawnPos, initVel); tank1Turn = false; tank1Firing = true; mousePressed = false; } if (mousePressed && tank2Turn) { sf::Vector2f spawnPos = _t2->getPos()+sf::Vector2f(0,-150); sf::Vector2f initVel = _t2->getInitVel(); _p = new Projectile(spawnPos, initVel); tank2Turn = false; tank2Firing = true; mousePressed = false; } if (tank1Turn && moved) { _t1->aim(mousePos); moved = false; mousePressed = false; } if (tank2Turn && moved) { _t2->aim(mousePos); moved = false; mousePressed = false; } }

I have some trouble with events in SFML. I am making a turnbased game, when the mouse has moved or left mouse button is clicked i check whos turn it is and then spawn a projectile at that objects position, when the projectile has collided with either terrain or opponent it gets destroyed and the turn is changed.

The behaviour is not what i am expecting though. When clicking shoot the projectile sometimes wont spawn at all (and it changes the turn immedeately). I have disabled all collisi so it cant be that. Im 90% sure that the issue is with how i handle events, so id really appretiate input on how i can make it better.

Of what ive learned is that you should not execute the functi in the while poll event, its only for registering what has happend most recently,so i put them outside instead. This does not solve my problem though...

sf::Event event; sf::Vector2i mousePos; while (_window.pollEvent(event)) { if ( == sf::Event::Closed) { _(); } if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) { _(); } if( == sf::Event::MouseMoved) { // <- this is how you check to see if an event mousePos = sf::Mouse::getPosition(); moved = true; } if( == sf::Event::MouseButtonPressed) { // <- this is how you check to see if an event mousePressed = true; } } if (mousePressed && tank1Turn) { sf::Vector2f spawnPos = _t1->getPos() + sf::Vector2f(0, -150); sf::Vector2f initVel = _t1->getInitVel(); cout << endl; _p = new Projectile(spawnPos, initVel); tank1Turn = false; tank1Firing = true; mousePressed = false; } if (mousePressed && tank2Turn) { sf::Vector2f spawnPos = _t2->getPos()+sf::Vector2f(0,-150); sf::Vector2f initVel = _t2->getInitVel(); _p = new Projectile(spawnPos, initVel); tank2Turn = false; tank2Firing = true; mousePressed = false; } if (tank1Turn && moved) { _t1->aim(mousePos); moved = false; mousePressed = false; } if (tank2Turn && moved) { _t2->aim(mousePos); moved = false; mousePressed = false; } }

最满意答案

考虑一下这个评论。 这不是一个答案 (我没有足够的声誉来评论,所以我必须做出这个答案)但这里有一些我注意到的事情:

您应该使用if-else s替换事件循环中的if-else ,或者使用开关

sf::Keyboard::isKeyPressed用于实时输入(将其放入游戏更新循环中)。 在你的事件循环中,它应该是sf::Event::KeyPressed并检查它与evt.关键

您没有检查按下了哪个鼠标按钮。 使用button ( sf::Mouse::Button::Left for left mouse)

我学到的是你不应该在while poll事件中执行函数,它只用于记录最近发生的事情,所以我把它们放在外面。

你在哪里读到的? 我认为这不是真的

在事件循环下面不应该只有一个if数组。 代码应该更好地构建

Cider this a comment. This isn't an answer (I don't have enough reputation to comment so I have to make this an answer) but here are some things I noticed:

You should either replace the ifs in your event loop with if-elses, or use a switch

sf::Keyboard::isKeyPressed is for real time input (put it inside your game update loop). In your event loop it should be sf::Event::KeyPressed and check which key it was with evt.

You aren't checking which mouse button was pressed. Do it with button (sf::Mouse::Button::Left for the left mouse)

Of what ive learned is that you should not execute the functi in the while poll event, its only for registering what has happend most recently,so i put them outside instead.

Where did you read that? I don't think that's true

There shouldn't just be an array of ifs below the event loop. Code should be structured better

#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格

本文地址:http://www.dnpztj.cn/diannao/61874.html

相关标签:无
上传时间: 2023-04-21 00:31:37
留言与评论(共有 12 条评论)
本站网友 怎么瘦咬肌
10分钟前 发表
本站网友 金融街融景城
3分钟前 发表
Event
本站网友 蜂花粉的作用
15分钟前 发表
Event
本站网友 右脑音乐
29分钟前 发表
本站网友 郑州装修招标
22分钟前 发表
本站网友 上安大厦
25分钟前 发表
本站网友 脂肪填充苹果机
16分钟前 发表
Event
本站网友 婴儿打嗝
2分钟前 发表
Event event; sf
本站网友 长城润滑油
18分钟前 发表
本站网友 韩谈
10分钟前 发表
MouseMoved) { // <- this is how you check to see if an event mousePos = sf
本站网友 北京挂号网
25分钟前 发表