-1

CheckCollision() 中的 LeftCollision 为真,当 Object1 与 Object2(从屏幕右向左滚动的正方形)左侧发生碰撞时。但是在 GameObject::Update() 中,Leftcollision 永远不会更新为 True,尽管它在 CheckCollision 部分中更改为 true!

我想要的是,只要 LeftCollision 为真,更新就应该停止,直到它再次变为假!

下面是头文件和CPP文件的代码!!

问题在于 LeftCollision 更新!为什么值没有反映在 Update if 条件中!!

    #pragma once

#include <iostream>
#include <allegro5/allegro5.h>
#include <allegro5/allegro_primitives.h>
#include "Globals.h"

class GameObject
{
private :
    int ID;
    bool alive;
    bool collidable;

protected :


    float velX;
    float velY;

    int dirX;
    int dirY;

    int boundX;
    int boundY;

    int maxFrame;
    int curFrame;
    int frameCount;
    int frameDelay;
    int frameWidth;
    int frameHeight;
    int animationColumns;
    int animationDirection;

    ALLEGRO_BITMAP *image;

public :
    float x;
    float y;
    bool LeftCollision;
    bool pause;

    GameObject();
    void virtual Destroy();

    void Init(float x, float y, float velX, float velY, int dirX, int dirY, int boundX, int boundY);
    void virtual Update();
    void virtual Render();

    float GetX() {return x;}
    float GetY() {return y;}

    void SetX(float x) {GameObject ::x = x;}
    void SetY(float y) {GameObject ::y = y;}

    int GetBoundX() {return boundX;}
    int GetBoundY() {return boundY;}

    int GetID() {return ID;}
    void SetID(int ID) {GameObject::ID = ID;}

    bool GetAlive() {return alive;}
    void SetAlive(bool alive) {GameObject :: alive = alive;}

    bool GetCollidable() {return collidable;}
    void SetCollidable(bool collidable) {GameObject :: collidable = collidable;}

    bool CheckCollisions(GameObject *otherObject);
    void virtual Collided(int objectID);
    bool Collidable();

};




 #include "GameObject.h"

GameObject :: GameObject()
{
    x = 0;
    y = 0;

    velX = 0;
    velY = 0;

    dirX = 0;
    dirY = 0;

    boundX = 0;
    boundY = 0;

    LeftCollision = false;


    maxFrame = 0;
    curFrame = 0;
    frameCount = 0;
    frameDelay = 0;
    frameWidth = 0;
    frameHeight = 0;
    animationColumns = 0;
    animationDirection = 0;

    image = NULL;

    alive = true;
    collidable = true;
}

void GameObject :: Destroy()
{

}

void GameObject ::  Init(float x, float y, float velX, float velY, int dirX, int dirY, int boundX, int boundY)
{
    GameObject :: x = x;
    GameObject :: y = y;

    GameObject :: velX = velX;
    GameObject :: velY = velY;

    GameObject :: dirX = dirX;
    GameObject :: dirY = dirY;

    GameObject :: boundX = boundX;
    GameObject :: boundY = boundY;
}
void GameObject :: Update()
{
    if(LeftCollision == false)
    {
        x += velX * dirX;
        y += velY * dirY;
    }
}
void GameObject :: Render()
{
}

bool GameObject :: CheckCollisions(GameObject *otherObject)
{
    float oX = otherObject->GetX();
    float oY = otherObject->GetY();

    int obX = otherObject->GetBoundX();
    int obY = otherObject->GetBoundY();


    if( x + boundX > oX - obX &&
        x - boundX < oX + obX &&
        y + boundY > oY - obY &&
        y - boundY < oY + obY && otherObject->GetID() == TRIANGLE)
    {
        return true;
    }
    else if(((oX < x + boundX + 14)&&(oX+ 40 >x - boundX))&&!((oY < y + boundY)&&(oY+40 > y - boundY)) 
        && otherObject->GetID() == SQUARE)
    {
        y = oY - boundX - 10;
        //x = oX + 40;
        return true;
    }
    else if((oX < x + boundX + 14) && (oX > x - boundX) && otherObject->GetID() == SQUARE)
    {
        LeftCollision = true;
        return false;
    }
    else
    {
        return false;
        LeftCollision = false;
    }
}
void GameObject :: Collided(int objectID)
{

}
bool GameObject :: Collidable()
{
    return alive && collidable;
}
4

2 回答 2

2

这是您的问题的一部分吗 - 在不设置值的情况下返回

    else
    {
        return false;
        LeftCollision = false;
    }
于 2012-03-01T20:07:20.377 回答
0

唯一想到的是,你有不止一个对象在移动,而且你并不总是使用你期望的那个。尝试放置类似的东西:

printf("here(%s:%d) this=%p\n", __FILE__, __LINE__, this);

在每个方法的开始,以确保this重点是你认为的。问题可能在于您没有显示的调用代码。

于 2012-03-01T20:02:25.897 回答