I have a problem with munipulating sprites in classes. Everytime time I try to show, hide, paste or create a sprite in a class function the compiler returns no errors, but after the program launches it's window goes black i get the [name].exe has stopped working message.
I see this pretty often too: LINK : Debug\push to play 2.exe not found or not built by the last incremental link; performing full link
Here are to examples of code that get that error:
#include "DarkGDK.h"
class Player
{
public:
int player;
int id;
int x;
int y;
void left();
void right();
void up();
void down();
void offScreen();
Player(int imageID, int playerNumber);
};
Player::Player(int imageID, int playerNumber)
{
id = playerNumber+1000;
x = playerNumber*200;
y = 1;
dbSprite(id, x, y, imageID);
}
void Player::offScreen()
{
while(this->y > (599 - dbSpriteHeight(this->id)))
this->y = this->y - 1;
while(this->y < 1)
this->y = this->y + 1;
while(this->x > (799 - dbSpriteWidth(this->id)))
this->x = this->x - 1;
while(this->x < 1)
this->x = this->x +1;
}
void Player::up()
{
this->y = this->y - 10;
this->offScreen();
dbPasteSprite(this->id, this->x, this->y);
dbSync();
}
void Player::down()
{
this->y = this->y + 10;
this->offScreen();
dbPasteSprite(this->id, this->x, this->y);
dbSync();
}
void Player::left()
{
this->x = this->x - 10;
this->offScreen();
dbPasteSprite(this->id, this->x, this->y);
dbSync();
}
void Player::right()
{
this->x = this->x + 10;
this->offScreen();
dbPasteSprite(this->id, this->x, this->y);
dbSync();
}
void DarkGDK(void)
{
dbSyncOn();
dbSyncRate(40);
dbLoadImage("guy2.bmp", 2);
Player pl(2, 1);
while(LoopGDK())
{
if(dbLeftKey() == 1)
pl.left();
if(dbRightKey() == 1)
pl.right();
if(dbUpKey() == 1)
pl.up();
if(dbDownKey() == 1)
pl.down();
}
}
and
#pragma once
class Point
{
public:
int x;
int y;
Point(int X, int Y) : x(X), y(Y) {}
Point(void);
void initialize(Point p);
void initialize(int X, int Y);
~Point(void);
};
#include "Point.h"
Point::Point(void)
{
}
Point::~Point(void)
{
}
void Point::initialize(int X, int Y)
{
this->x = X;
this->y = Y;
}
void Point::initialize(Point p)
{
this->initialize(p.x, p.y);
}
#pragma once
#include "Point.h"
#include "DarkGDk.h"
class Space
{
Point high; //high is the top left point
Point low; //low is the bottom right point
public:
bool click();
bool inside(int Player_id);
Point see_high; //all of the Points here are constructed with Point(void)
Point see_low;
Space(Point topLeft, Point bottomRight);
~Space(void);
};
#include "Space.h"
#include "DarkGDK.h"
Space::Space(Point topLeft, Point bottomRight)
{
high.initialize(bottomRight);
low.initialize(topLeft);
see_high.initialize(high);
see_low.initialize(low);
}
bool Space::click()
{
if(dbMouseClick() == 1
&& dbMouseX() > this->low.x
&& dbMouseX() < this->high.x
&& dbMouseY() > this->low.y
&& dbMouseY() < this->high.y) //1 long if statement :)
return true;
else
return false;
}
bool Space::inside(int Player_id)
{
if(dbSpriteX(Player_id) > this->low.x
&& dbSpriteY(Player_id) < this->low.x
&& dbSpriteY(Player_id) > this->low.y
&& dbSpriteY(Player_id) < this->low.y)
return true;
else
return false;
}
Space::~Space(void)
{
}
class Screen
{
public:
vector<int> IDs; //keeps track of all the items on the screen
Screen(string Filename, int backgroundID);
Screen(string Filename, int backgroundID, bool show);
Screen() {}
~Screen(void);
void show();
void hide();
bool showing; //used in add_item
void add_item(int id);
};
#include "Screen.h"
using std::string;
Screen::Screen(string Filename, int backgroundID)
{
char filename[499];// convert string to char*
strcpy(filename, Filename.c_str());
dbLoadImage(filename, backgroundID); //load the background
dbSprite(backgroundID, 0, 0, backgroundID); //put it into a sprite
IDs.push_back(backgroundID); // add it to our IDs
this->hide();
}
Screen::Screen(std::string Filename, int backgroundID, bool show)
{
char filename[499];// convert string to char*
strcpy(filename, Filename.c_str());
dbLoadImage(filename, backgroundID); //load the background
dbSprite(backgroundID, 0, 0, backgroundID); //put it into a sprite
IDs.push_back(backgroundID); // add it to our IDs
if(show)
this->show();
}
void Screen::show()
{
for(int i = 0; i < (this->IDs.size()+1); i++) //run a loop through everthing in IDs hiding them
{
int j = this->IDs[i];
dbShowSprite(j);
}
}
void Screen::hide()
{
for(int i = 0; i < (this->IDs.size()+1); i++) //run a loop through everthing in IDs hiding them
{
int j = this->IDs[i];
dbHideSprite(j);
}
}
void Screen::add_item(int id)
{
IDs.push_back(id); //add it's id to our IDs
if(this->showing)
this->show();
}
Screen::~Screen(void)
{
for(int i = 0; i < (this->IDs.size()+1); i++)
{ int j = IDs[i];
dbDeleteSprite(j);
}
}
#include "DarkGDK.h"
#include "Screen.h"
#include "Space.h"
#include "Point.h"
void DarkGDK(void)
{
dbSyncOn();
dbSyncRate(40);
dbLoadImage("apples.bmp", 3);
dbLoadImage("car.bmp", 4);
Screen ts("title screen.bmp", 2, true);
Screen car; car.add_item(4);
Screen txt("text background.bmp", 5);
Screen apples; apples.add_item(3);
Space show_car(Point(122, 376), Point(297, 470));
Space show_apples(Point(538, 370), Point(754, 458));
Space show_text(Point(114, 100), Point(197, 297));
dbSetTextSize(30);
while(LoopGDK())
{
if(show_car.click())
{car.show(); ts.hide();}
if(show_apples.click())
{apples.show(); ts.hide();}
if(show_text.click())
{car.show(); ts.hide(); dbText(400, 300, "Hi, whoever you are!");}
dbSync();
}
}