我试图创造形状,使他们反弹的边缘,但我的if循环似乎不工作,我不知道为什么。这条线移动形状:
line1.x += line1.xvelocity;
line1.y += line1.yvelocity;
line1.x2 += line1.xvelocity;
line1.y2 += line1.yvelocity;我想保持640x480的形状,所以我写了
if (line1.x >> 640) line1.xvelocity *= (-1);
if (line1.x << 0) line1.xvelocity *= (-1);
if (line1.y >> 480) line1.yvelocity *= (-1);
if (line1.y << 0) line1.yvelocity *= (-1);
if (line1.x2 >> 640) line1.xvelocity *= (-1);
if (line1.x2 << 0) line1.xvelocity *= (-1);
if (line1.y2 >> 480) line1.yvelocity *= (-1);
if (line1.y2 << 0) line1.yvelocity *= (-1);我尝试使用||,或者仅仅使用x,y或x2,y2坐标。有什么帮助吗?谢谢。
class Line: public GenericShape
{
public:
int x2, y2;
Line();
Line(int x_in, int y_in, int color_in, int xvel, int yvel, int x2_in, int y2_in)
: GenericShape(x_in, y_in, color_in, xvel, yvel),
x2(x2_in),
y2(y2_in)
{}
void draw() const;
};
Line line1(50, 150, 4, 2, -3, 180, 60); // xvelocity=2 yvelocity =-3发布于 2013-11-28 18:34:50
条件在
if (line1.x >> 640)总是为零,因为'>>‘是按位向右移位的操作,而不是比较。它相当于第1.x行除以2^640。
把它改成
if(line1.x >= 640)在其他条件下,“<<”改为“<”,“>>”也改为“>=”。
https://stackoverflow.com/questions/20272691
复制相似问题