0

新来的,所以要温柔,我目前正在为我的课程做我的主要项目,我不是要求为我完成家庭作业,我只是无法解决我遇到的一个奇怪问题即使在这里,也无法找到答案。我正在为我的绘图使用 SDL。

我正在使用我的项目或“状态机”进行面向对象编程(相信我,这在新手心中听起来不那么痛苦),并且在我的 Class Game1.cpp 的渲染部分中,我试图调用一个 Draw Function我的播放器类,但由于某些我无法理解的未知原因,它只是完全跳过了这个函数调用。

我没有错误,我什至使用断点来找出发生了什么,但它每次都完全跳过它,它也将屏幕拉黑,没有失败。任何关于它为什么跳过它的帮助将不胜感激。

老实说,我觉得这是一个简单的菜鸟错误,但是欢迎对我的代码进行任何和所有审查,感谢我能做的任何事情来改善自己。

游戏1.cpp:

#include "Game1.h"
#include "PlayerCharacter.h"
Game1::Game1( World * worldObject )
{
    //object setup
    this->worldObject = worldObject;
    setDone (false);
}

Game1::~Game1()
{

}

void Game1::handle_events()
{
    //*******************************************
    //**//////////////Call Input///////////////**
    //*******************************************
    //******Check for Keyboard Input*************

    //******Check Keyboard Logic*****************

    //******Check for Mouse Input****************
    //The mouse offsets
    x = 0, y = 0;

    //If the mouse moved
    if (SDL_PollEvent(&worldObject->event))
    {
        if( worldObject->event.type == SDL_MOUSEMOTION )
        {
            //Get the mouse offsets
            x = worldObject->event.motion.x;
            y = worldObject->event.motion.y;
        }
    }
    //******Check Mouse Logic********************


}

void Game1::logic()
{



    //*******************************************
    //**//////////Collision Detection//////////**
    //*******************************************
    //******Check Player Bullet Collision Loop***
    //Check for collision with enemies

    //Check for collision with bitmap mask (walls)

    //******Check Enemy Bullet Collision Loop****
    //Check for Collision with Player

    //Check for collision with bitmap mask (walls)



}

void Game1::render()
{
    //*******************************************
    //**////////////////Drawing////////////////**
    //*******************************************
    //******Blit Black Background****************
    SDL_FillRect(worldObject->Screen , NULL , 0xff000000);

    //******Blit Bitmap Mask*********************

    //******Blit Flashlight**********************

    //******Blit Map*****************************

    //******Blit Pickups*************************

    //******Blit Bullets*************************

    //******Blit Player**************************
    &PlayerCharacter.Draw; // <----- Skips this line completely, no idea why

    //******Blit Enemies*************************

    //******Blit Blackened Overlay***************

    //******Blit HUD*****************************

    //******Flip Screen**************************
    SDL_Flip(worldObject->Screen);
}

游戏1.h

#ifndef __Game1_H_INLUDED__
#define __Game1_H_INLUDED__

#include "GameState.h"
#include "SDL.h"
#include "ImageLoader.h"
using namespace IMGLoader;
class Game1 : public GameState
{
    private:
    //Menu Image
    World * worldObject;
    SDL_Rect PauseMenu,Item1Tile,Item2Tile,Item3Tile;
    /*bool bPauseMenu, bItem1Tile, bItem2Tile, bItem3Tile;
    int ButtonSpace,ButtonSize;
    float x,y;
    int Alpha1,Alpha2;*/
    //Clipping Window
    //SDL_Rect sclip,dclip;

    public:
    //Loads Menu resources
    Game1 (World * worldObject);
    //Frees Menu resources
    ~Game1();

    //Main loop functions
    void handle_events();
    void logic();
    void render();
};

#endif

玩家角色.cpp

#include "PlayerCharacter.h"

SDL_Rect psclip,pdclip;

PlayerCharacter::PlayerCharacter ( float X, float Y, float dX, float dY, float Angle, float Speed, bool Existance, int Height, int Width, int Health, int Shield, SDL_Surface* Player ):Characters ( X, Y, dX, dY, Angle, Speed, Existance, Height, Width, Health )
{
    this->Player = Player;
    this->Shield = Shield;
    this->Player = load_image("image\Player1.png");
}
void PlayerCharacter::setShield ( int Shield )
{
    this->Shield = Shield;
}
int PlayerCharacter::getShield ( void )
{
    return Shield;
}
void PlayerCharacter::Draw(  ) 
{
    psclip.x = 0; psclip.y = 0; psclip.w = 64; psclip.h = 64;
    pdclip.x = 640; pdclip.y = 318; pdclip.w = 64; pdclip.h = 64;
    SDL_BlitSurface(Player, &psclip, worldObject->Screen, &pdclip);
}

玩家角色.h

#ifndef __PlayerCharacter_H_INCLUDED__
#define __PlayerCharacter_H_INCLUDED__

#include "Characters.h"

class PlayerCharacter : public Characters
{
private:
    int Shield;
    SDL_Surface* Player;
    World *worldObject;
public:
    PlayerCharacter ( float X, float Y, float dX, float dY, float Angle, float Speed, bool Existance, int Height, int Width, int Health, int Shield, SDL_Surface* Player );

    void setShield ( int Shield );
    int getShield ( void );

    void Draw (  );
};

#endif
4

2 回答 2

5

线

&PlayerCharacter.Draw; // <----- Skips this line completely, no idea why

实际上不是函数调用。它是一个表达式,它获取类中Draw函数的地址PlayerCharacter并且对它不做任何事情。

我实际上有点惊讶它编译时没有错误,或者至少有很多警告。

你需要创建一个PlayerCharacter对象,然后调用对象中的函数。

于 2013-05-17T10:58:48.730 回答
1

&PlayerCharacter.Draw不是函数调用。PlayerCharacter::Draw()不是静态类方法,因此您需要一个PlayerCharacter 对象来调用此方法。

您有 a class PlayerCharacter,它定义了 aPlayerCharacter 什么以及可以用它什么。但据我所知,你没有一个PlayerCharacter 对象,即没有玩家角色。如果你有,让我们给他打电话pc,然后你可以画他pc.Draw()。为此,您必须实例化该类,例如 via PlayerCharacter pc( ... ),并...用一些适当的值替换您那里的大量构造函数参数。(你真的想要一个默认构造函数,将所有这些初始化为零或其他适当的“开始”值......)

于 2013-05-17T10:59:55.360 回答