【发布时间】:2011-04-22 22:15:32
【问题描述】:
所以我遇到了一个问题,当我请求位置时,我似乎总是得到最后一个球的位置。好像在我声明对象时没有正确分配对象中的值。
这是我目前拥有的
ball TestBall[2];
在方法中声明变量
void ball::Placeball() {
//TEMP TEST
TestBall[1].DrawBall(5,0.15,10,3,10);
//TEMP TEST
TestBall[2].DrawBall(5,0.15,10,3,30);
}
我的抽球方法和变量是如何传递的
void ball::DrawBall(float radius, float mass, float x, float y, float z) {
xPos = x;
yPos = y;
zPos = z;
GLUquadricObj *quadratic;
quadratic = gluNewQuadric();
glPolygonMode(GL_FRONT,GL_FILL);
glPolygonMode(GL_BACK,GL_FILL);
gluQuadricNormals(quadratic,GLU_SMOOTH);
glTranslatef(x,y,z);
gluSphere(quadratic,radius,20,20);
glTranslatef(-x,-y,-z);
}
我无法工作的传递变量
float ball::zPos
以及它是如何被检索到的
float ball::GetZ() const {
return zPos;
}
然后我只是想获得价值
cout << TestBall[1].GetZ() << endl; //should be 10
cout << TestBall[2].GetZ() << endl; //should be 30
ball.cpp:
float ball::xPos = 0;
float ball::yPos = 0;
float ball::zPos = 0;
ball::ball() { };
void ball::DrawBall(float radius, float mass, float x, float y, float z) {
xPos = x;
yPos = y;
zPos = z;
GLUquadricObj *quadratic;
quadratic = gluNewQuadric();
glPolygonMode(GL_FRONT,GL_FILL);
glPolygonMode(GL_BACK,GL_FILL);
gluQuadricNormals(quadratic,GLU_SMOOTH);
glTranslatef(x,y,z);
gluSphere(quadratic,radius,20,20);
glTranslatef(-x,-y,-z);
}
float ball::GetX() const {
return xPos;
}
float ball::GetY() const {
return yPos;
}
float ball::GetZ() const {
return zPos;
}
ball.h:
#pragma once
class ball
{
private:
static float xPos;
static float yPos;
static float zPos;
public:
ball();
ball(float radius, float mass, float x, float y, float z){};
~ball(){};
static void DrawBall(float radius, float mass, float x, float y, float z);
static void UpdateBall(float speedx, float speedz);
static void Placeball();
float GetX() const;
float GetY() const;
float GetZ() const;
};
两个值都是 30,如果我增加对象数组的大小,这也是同样的问题。
我很可能错过了一些简单的东西。
感谢您的宝贵时间。
【问题讨论】: