提示コードですがelse if(key == KEY_RIGHT)
つまり右キーを押した瞬間だけを取得したのですがbool型変数を使って判定処理を記述してもなぜか毎フレーム走ってしまいます。これはなぜでしょうか?
キー入力は押している間if文の中が実行されます。
確認したいと
プロジェクト全体で別の場所で同じ関数が実行されていないあ確認
変数の型を確認
キーを押している間if文の中が通るという処理を確認 getch()関数の挙動を確認
debug.log
3 ああああ 3 ああああ 3 ああああ 3 ああああ 3 ああああ 3 ああああ 3 ああああ 3 ああああ
Main.cpp
cpp
1#include <iostream> 2#include "Screen.hpp" 3#include <memory> 4//#include <Magick++.h> 5#include "../lib/src/Console.hpp" 6 7 8int main() 9{ 10 Console::Init(); 11 Console::Init_ErrorLog("log/error.log"); 12 Console::Init_Log("log/log.log"); 13 nodelay(stdscr,true); 14 Console::RenderBuffer(NULL); 15 16 17 std::unique_ptr<Screen> main = std::make_unique<Screen>(); 18 19 while(true) 20 { 21 Console::ClearBuffer(stdscr); 22 23 main->Loop(); 24 25 if (main->getExit() == true) 26 { 27 break; 28 } 29 30 31 Console::RenderBuffer(stdscr); 32 33 } 34 35 Console::Finalize(); 36 37 return 0; 38}
利用部
cpp
1 2void Screen::Update() 3{ 4 //pull.Update(); 5 6 if(Console::GetKeyDown() == Console::KeyCode::Right) 7 { 8 Console::Debug::WriteLog("ああああ\n"); 9 } 10 11 12 // 13 KeyInput(); 14} 15
Input.hpp
cpp
1#ifndef ___IPNUT_HPP___ 2#define ___IPNUT_HPP___ 3 4#include "Data.hpp" 5 6namespace Console 7{ 8 9 class Input 10 { 11 public: 12 static void InputKey(); 13 14 static KeyCode GetKeyCode(); 15 static KeyCode GetKeyDown(); 16 static void ResetKey(); 17 18 private: 19 20 static KeyCode keyInput; 21 static KeyCode prevKeyInput; 22 static bool flag; 23 Input(); 24 ~Input(); 25 26 27 }; 28 29 30}; 31 32 33#endif 34
Input.cpp
cpp
1 2#include "Input.hpp" 3#include <ncurses.h> 4#include "Debug.hpp" 5 6Console::KeyCode Console::Input::keyInput = Console::KeyCode::None; 7Console::KeyCode Console::Input::prevKeyInput = Console::KeyCode::None; 8bool Console::Input::flag = false; 9 10Console::KeyCode Console::Input::GetKeyCode() 11{ 12 return keyInput; 13} 14 15Console::KeyCode Console::Input::GetKeyDown() 16{ 17 if(flag == true) 18 { 19 flag = false; 20 21 Debug::WriteLog(" %d\n",keyInput); 22 23 return keyInput; 24 } 25 else 26 { 27 return KeyCode::None; 28 } 29} 30 31void Console::Input::InputKey() 32{ 33 34 int key = getch(); 35 36 if(key == KEY_UP) 37 { 38 keyInput = KeyCode::Up; 39 } 40 else if(key == KEY_RIGHT) 41 { 42 keyInput = KeyCode::Right; 43 44 if(keyInput != prevKeyInput) 45 { 46 flag = true; 47 } 48 49 } 50 else if(key == KEY_DOWN) 51 { 52 keyInput = KeyCode::Down; 53 } 54 else if(key == KEY_LEFT) 55 { 56 keyInput = KeyCode::Left; 57 } 58 else if(key == KEY_ESC) 59 { 60 keyInput = KeyCode::Escape; 61 } 62 else if(key == KEY_RETURN) 63 { 64 keyInput = KeyCode::Return; 65 } 66 else 67 { 68 flag = false; 69 keyInput = KeyCode::None; 70 prevKeyInput = KeyCode::None; 71 } 72 73 74 75 76 prevKeyInput = keyInput; 77} 78 79 80 81 82void Console::Input::ResetKey() 83{ 84 //keyInput = KeyCode::None; 85} 86
InputKey()を呼び出す箇所が提示されていないので私の妄想の域をでないことを前提です
#なんか怪しい箇所があるが
InputKey()が、質問者さんの脳内で想定している以上に呼ばれていて
flagがfalseに戻されているのは?
まずは
flag = false;
にブレイクポイントでも置くか(ブレイク機能をもつ環境かどうかわからないけど)、ログを書き出す処理を加えて
通過していないか確認してみてはいかがでしょうか?

回答2件
あなたの回答
tips
プレビュー