-1

LeftCollision in CheckCollision() goes true, when ever Object1 collides with Object2(square which is scrolling from right to left of screen) left side. But in the GameObject::Update() Leftcollision never updates to True, though it changes to true in the CheckCollision section!!

What i want is, whenever the LeftCollision is true, Update should stop, until it becomes false again!!

Below is the code for Header file and CPP file!!

The problem is with LeftCollision updation!! Why is the value not reflected in the Update if condition!!

    #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;
}
Varun Parakh
  • 221
  • 1
  • 4
  • 10
  • This looks suspicious: `GameObject :: x = x;` Please show your header file, too. – Greg Hewgill Mar 01 '12 at 18:15
  • Done, but the problem is with the If condition in the GameObject :: Update() section, it always remains false!! – Varun Parakh Mar 01 '12 at 18:19
  • 1
    Pedantic grammar note: You don't need two exclamation points at the end of your sentences to make a point. A period would work just as well. Please stop using "!!"; if you truly need an exclamation, just use *one*. – Nicol Bolas Mar 01 '12 at 18:31
  • If the answer was more directed towards the question i have put in, it would have been appreciable. – Varun Parakh Mar 01 '12 at 18:38

2 Answers2

2

Is this part of your problem - returning without setting the value

    else
    {
        return false;
        LeftCollision = false;
    }
Bo Persson
  • 90,663
  • 31
  • 146
  • 203
0

The only thing that jumps to mind is that you've got more than one object going around and you're not always using the one you expect. Try putting something like:

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

at the start of each method to make sure that the this point is what you think it is. The problem may lie in the calling code that you haven't shown.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285