C ++,Allegro中的更新不正确

时间:2012-03-01 18:12:27

标签: c++ collision allegro5

当Check1与Object2(屏幕从右向左滚动的方块)左侧碰撞时,CheckCollision()中的LeftCollision成立。但是在GameObject :: Update()中 Leftcollision永远不会更新为True,虽然它在CheckCollision部分中变为true !!

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

以下是Header文件和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;
}

2 个答案:

答案 0 :(得分:2)

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

    else
    {
        return false;
        LeftCollision = false;
    }

答案 1 :(得分:0)

唯一想到的是,你有多个物体四处走动而你并不总是使用你想要的物体。尝试使用类似的东西:

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

在每个方法的开头确保this点符合您的想法。问题可能在于您未显示的调用代码。