在主循环里无法调用Player::Move() 可是生成没有错误啊?为什么?

后来测试了一下
不是无法调用函数的问题
下面上代码
Player.h
----------
#pragma once
#include "BasicInclude.h"

namespace Player {
static float Player_x;
static float Player_y;
static float Player_z;
void Move();
}
----------
Player.cpp
----------
#include "Player.h"

using namespace Player;

void Player::Move() {
if (glfwGetKey(MainWindow, GLFW_KEY_W)) { Player::Player_z += 0.01; }
else if (glfwGetKey(MainWindow, GLFW_KEY_S)) { Player::Player_z -= 0.01; }
else if (glfwGetKey(MainWindow, GLFW_KEY_A)) { Player::Player_x += 0.01; }
else if (glfwGetKey(MainWindow, GLFW_KEY_D)) { Player::Player_x -= 0.01; }
else if (glfwGetKey(MainWindow, GLFW_KEY_SPACE)) { Player::Player_y += 0.01; }
else if (glfwGetKey(MainWindow, GLFW_KEY_LEFT_SHIFT)) { Player::Player_y -= 0.01; }
else if (glfwGetKey(MainWindow, GLFW_KEY_RIGHT_SHIFT)) { Player::Player_y -= 0.01; }
}
----------

MineCubeWorld.cpp
----------
int main() {
glfwInit();
MainWindow = glfwCreateWindow(WinWidth, WinHeight, WinTitle.c_str(), NULL, NULL);
glfwMakeContextCurrent(MainWindow);
do {
Player::Move();
printf("X: %f Y: %f Z: %f \n", Player::Player_x, Player::Player_y, Player::Player_z);
glfwPollEvents();
if (glfwGetKey(MainWindow, GLFW_KEY_ESCAPE)) { exit(EXIT_SUCCESS); }
} while (!glfwWindowShouldClose(MainWindow));
glfwTerminate();
return 0;
}
问题描述:

如果把Player::Move()函数的代码放在主循环里 就可以调用与执行

但是 如果在主循环里调用Player::Move()函数的话 Move()函数可以被调用与执行 但是不能给Player::Player_x Player::Player_y Player::Player_z 赋值
这三个数据都是float的

慕侠2389804
浏览 116回答 1
1回答

守候你守候我

用法有问题,namespace里面的定义, 变量定义放在cpp中,看你定义static,既然不打算变量给外部用,那头文件中就连声明都不需要了。里面的函数也要用namespace括起来
打开App,查看更多内容
随时随地看视频慕课网APP