提示コードの///////部で囲っているofs2を2行コメントしてあるところですがpos->に値が上手く行ってないのではと思いデバッグコマンドとしてテキストファイルに出力したらうまく動きバグが解決しました。これはメモリが勝手に書き換わるバグなのでしょうか?全く関係ない部分にコードを書き込んだだけで修正というコード修正は一切しておりません、C++における勝手にメモリが書き換わる原因や現象について知りたいので質問しました。 コードが断片的です。
#include "DxLib.h" #include <vector> #include <math.h> #include <fstream> #include "string.h" #include <iostream> #include <optional> //#include <memory> #include "Frame.h" #include "keyboard.h" #include "Position.h" #include "Player.h" Player::Player(){ /*計算*/ // pos = std::make_unique<Position>(); // speed = std::make_unique<Position>(); // Direction = std::make_unique<Position>(); pos = new Position(); speed = new Position(); Direction = new Position(); /*初期座標*/ pos->x = (6 * CELL); pos->y = (5 * CELL); State = new keyState; State_y = new keyState; isJump = false; isGround = false; /*描画*/ if (LoadDivGraph("Mario/resource/Mario_64px.png", 7, 7, 1, 64, 64, player_graph) == -1) { exit(1); } } /*Yの符号を逆にする関数*/ float Player::rev_y(float t) { return t * -1.0f; } /*等速直線運動のY 落下*/ void Player::gravity_force() { } std::ofstream ofs2("Log.txt"); /*移動処理*/ void Player::Move(keyState key,keyState action) { if (key == keyState::Left) { Direction->x = -1; speed->x = 4 * Direction->x; } else if(key == keyState::Right) { Direction->x = 1; speed->x = 4 * Direction->x; } else if (key == keyState::Invalid) { Direction->x = 0; speed->x = 0; } if (action == keyState::Up) { speed->y = -4; } else if (action == keyState::Down) { speed->y = 4; } else { speed->y = 0; } pos->x += speed->x; pos->y += speed->y; ////////////////////////////////////////////////////////////////// ofs2 << "speed->x: " << speed->x << std::endl; ofs2<<"speed->y: "<<speed->y<<std::endl; ////////////////////////////////////////////////////////////////// } /*-----------------------------計算----------------------------*/ void const Player::Update() { //DrawFormatString(0, 0, GetColor(255, 255, 255), "Pos: %.2f , %.2f", pos->x, pos->y, true); DrawFormatString(0, 0, GetColor(255, 255, 255), "Pos: %.2f , %.2f", pos->x, pos->y, true); //DrawFormatString(0, 32, GetColor(255, 255, 255), "speed: %.2f , %.2f", speed->x, speed->y, true); DrawFormatString(0, 32, GetColor(255, 255, 255), "speed: %.2f , %.2f", speed->x, speed->y, true); //DrawFormatString(200, 200, GetColor(255, 255, 255), "block: %.2f , %.2f", block.x, block.y, true); // DrawFormatString(200, 200, GetColor(255, 255, 255), "block: %.2f, %.2f", block.x, block.y, true); //DrawFormatString(400, 400, GetColor(255, 255, 255), "%d", block.x, block.y, true); // DrawFormatString(200,0,GetColor(255,255,255),"Frame: %d",Fps::now(),true); // DrawFormatString(300, 0, GetColor(255, 255, 255), "speed.x: %.2f", speed->x, true); // DrawFormatString(450, 0, GetColor(255, 255, 255), "speed.y: %.2f", speed->y, true); DrawFormatString(450, 100, GetColor(255, 255, 255), "isGround: %d", isGround, true); DrawFormatString(450, 120, GetColor(255, 255, 255), "isJump: %d", isJump, true); //DrawFormatString(100, 100, GetColor(255, 255, 255), "Right", true); // DrawFormatString(100, 100, GetColor(255, 255, 255), "%d",dd, true); } /*-------------------------------------------------------------------------------------------------*/ /*----------------------------描画-----------------------------*/ void const Player::Draw_Update() { DrawGraph((int)pos->x, (int)pos->y, player_graph[0], true); } /*------------------------------------------------------------*/

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2020/01/29 13:33
2020/01/29 13:45 編集
退会済みユーザー
2020/01/30 11:26
2020/01/31 01:39
退会済みユーザー
2020/01/31 02:10
2020/01/31 05:31
退会済みユーザー
2020/01/31 13:19
2020/01/31 14:06 編集
2020/01/31 14:07
2020/01/31 14:26
退会済みユーザー
2020/01/31 14:29