Sprite1 *test = new Sprite1(450, 450, "enemy.bmp", *screen);
test->DrawJon();
SDL_Delay(1000);
test->MoveJon(20,20);
我在第 2 行遇到运行时错误。它说在 0x0 的访问冲突
Sprite1 是我定义的一个类和类中的 DrawJon() 和 MoneJon()。这种语法在编译器中是可以的,但在运行时失败。
Sprite1.cpp
#include "Sprite1.h"
Sprite1::Sprite1(int posX, int posY, std::string imagePath, SDL_Surface screen) : PosX(posX), PosY(posY), ImagePath(imagePath), Screen(screen)
{
void DrawSprite1Jon( int x, int y, SDL_Surface *sprite, SDL_Surface *screen );
void DrawJon();
void MoveJon(int xDist, int yDist);
}
void Sprite1::DrawSprite1Jon( int x, int y, SDL_Surface *sprite, SDL_Surface *screen )
{
//Make a temporary rectangle to hold the offsets
SDL_Rect offset;
//Give the offsets to the rectangle
offset.x = x;
offset.y = y;
//Blit the surface
SDL_BlitSurface( sprite, NULL, screen, &offset );
SDL_UpdateRect(screen, 0, 0, 0, 0);
}
void Sprite1::DrawJon()
{
#pragma region Char to String Conversion
string ImagePath;
char * writable = new char[ImagePath.size() + 1];
copy(ImagePath.begin(), ImagePath.end(), writable);
writable[ImagePath.size()] = '\0';
#pragma endregion
temp = SDL_LoadBMP(writable);
sprite = SDL_DisplayFormat(temp);
SDL_FreeSurface(temp);
// free the string after using it
delete[] writable;
DrawSprite1Jon(PosX, PosY, sprite, screen);
}
Sprite1.h
#include <string>
#include <SDL.h>
#include "Functions.h"
using namespace std;
class Sprite1
{
private:
int PosX;
int PosY;
int xDist;
int yDist;
string ImagePath;
SDL_Surface Screen;
SDL_Surface *temp, *sprite, *screen;
public:
Sprite1(int PosX, int PosY, string ImagePath, SDL_Surface Screen );
void DrawSprite1Jon( int x, int y, SDL_Surface *sprite, SDL_Surface *screen);
void DrawJon();
void MoveJon(int xDist, int yDist);
};
编辑:
经过进一步调查,是这条线
DrawSprite1Jon(PosX, PosY, sprite, screen);
这在 DrawJon() 中失败了