我卡住了,太烦人了……我有发送鼠标坐标的窗口消息,我有一个可以看到这些坐标的游戏循环,但是当我调用一个查看鼠标坐标的类时,它不能看不到鼠标坐标,它只是创建自己的鼠标版本,而不管我在使用它的 .cpp 文件上定义 global.h 和引用 extern:
Mouse.h
#pragma once
class Mouse
{
public:
int x;
int y;
void MouseMove( int x, int y );
Mouse();
};
Global.h
#include "Mouse.h"
static Mouse mouse;
Game.cpp//代码片段//
Game::Game( HWND hWnd, Mouse &mouse )
:
gfx( hWnd ),
mouse( mouse )
{
...
if( scenes[ a ]->interactiveObjects[ b ]->CheckCollision( mouse.x, mouse.y ) )
{
.... // Game is looping if no messages stored, the windows loop updates my mouse coordinates by calling a routine in my Mouse.cpp. My windows loop sends the mouse through as a parameter to my game object which I no longer want to use... I want to use my global mouse as the mouse reference.
“InteractiveObject.cpp”它包含“Global.h”并引用其中声明的鼠标……对吗?那么为什么我的检查碰撞没有看到 mouse.x 和 mouse.y(我必须从我的游戏对象传递坐标作为参数 mouseX 和 mouseY :(
#include "Global.h"
extern Mouse mouse;
#include "InteractiveObject.h"
InteractiveObject::InteractiveObject( int id_, string title_, Image* theImage_, int type_, int x_, int y_, int z_ )
:
id( id_ ),
title( title_ ),
theImage( theImage_ ),
type( type_ ),
x( x_ ),
y( y_ ),
z( z_ )
{
pos.x = x_;
pos.y = y_;
pos.z = z_;
}
bool InteractiveObject::CheckCollision( int mouseX, int mouseY )
{
if(
mouse.x > x &&
mouse.x < x + theImage->width &&
mouse.y > y &&
mouse.y < y + theImage->height
)
/*if(
mouseX > x &&
mouseX < x + theImage->width &&
mouseY > y &&
mouseY < y + theImage->height
)*/
{
return TRUE;
}
return FALSE;
}
最佳答案
您的 global.h 正在扩展到您包含它的每个文件中。因此,对于您包含它的每个文件,都有一个全局的静态(意味着仅此文件)实例化。尝试使用单例模式或将变量声明为extern 在标题中,然后在 mouse.cpp 中一次
关于C++ 和我的鼠标类全局变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15753562/