-1

CheckCollision() の LeftCollision は、Object1 が Object2 (画面の右から左にスクロールしている正方形) の左側と衝突するたびに true になります。しかし、GameObject::Update() では、CheckCollision セクションで true に変更されますが、Leftcollision が True に更新されることはありません!!

私が欲しいのは、LeftCollision が true のときはいつでも、Update が再び false になるまで停止する必要があるということです!!

以下はヘッダーファイルと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 に答える