Jump to content
  • Advertisement
Sign in to follow this  
SSJCORY

Move not working!

This topic is 4867 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am getting an access violation when i Make calls to getX or setX or getY or setY in the following source code. I cannot figure out why that is. I did narrow it down to those methods. ANyone got any ideas.
#include<windows.h>
#include<vector>
using namespace std;
class cMap;
class cPlayer;
class cMap{
private:
	int width;
	int height;
	vector<cPlayer> players;
	int currentplayer;
public:
	int getwidth(){
		return width;
	}
	int getheight(){
		return height;
	}
	cMap(){
		width = 500;
		height = 500;
	}
	cMap(int newwidth,int newheight){
		width = newwidth;
		height = newheight;
		currentplayer = 1;
	}
	void moveplayer(int direction);
	void addplayer(cPlayer newplayer);
	void display(HWND hwnd);
};
class cPlayer{
private:
	int x;
	int y;
	HBITMAP visual;
public:
	int getX(){
		return x;
	}
	int getY(){
		return y;
	}
	HBITMAP getVisual(){
		return visual;
	}
	void setX(int newx){
		x = newx;
	}
	void setY(int newy){
		y = newy;
	}
	void setVisual(HBITMAP newbitmap){
		visual = newbitmap;
	}
	cPlayer(){
		visual = (HBITMAP)LoadImage(0,"defaultplayer.bmp",IMAGE_BITMAP,50,50,LR_LOADFROMFILE);
		x = 0;
		y = 0;
	}
	cPlayer(int startx,int starty){
		visual = (HBITMAP)LoadImage(0,"defaultplayer.bmp",IMAGE_BITMAP,50,50,LR_LOADFROMFILE);
		x = startx;
		y = starty;
	}
	cPlayer(HBITMAP hbitmap){
		visual = hbitmap;
		x = 0;
		y = 0;
	}
	cPlayer(HBITMAP hbitmap,int startx,int starty){
		visual = hbitmap;
		x = startx;
		y = starty;
	}
	void display(HWND hwnd){
		HDC hdc = GetDC(hwnd);
		HDC idc = CreateCompatibleDC(hdc);
		SelectObject(idc,visual);
		BitBlt(hdc,x,y,50,50,idc,0,0,SRCCOPY);// WHY DOESNT THIS WORK?
		DeleteDC(idc);
	}
	
};
bool done = false;
LRESULT CALLBACK WndProc(HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM lParam){
	cMap cmap;
	cPlayer player;
	cPlayer player2(100,100);
	cPlayer player3(200,200);
	cmap.addplayer(player);
	cmap.addplayer(player2);
	cmap.addplayer(player3);
	switch(iMsg){
	case WM_DESTROY:
		PostQuitMessage(0);
		done = true;
		return 0;
		break;
	case WM_KEYDOWN:
		switch(wParam){
		case VK_SPACE:
			cmap.display(hwnd);
			break;
		case VK_LEFT:
			cmap.moveplayer(1);
			break;
		case VK_RIGHT:
			cmap.moveplayer(2);
			break;
		case VK_UP:
			cmap.moveplayer(3);
			break;
		case VK_DOWN:
			cmap.moveplayer(4);
			break;
		}
		break;
	}
	return DefWindowProc(hwnd,iMsg,wParam,lParam);
}
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hpInstance,PSTR cmdLine,int iCmd){
	HWND hwnd;
	MSG msg;
	WNDCLASS wc;
	wc.cbClsExtra = NULL;
	wc.cbWndExtra = NULL;
	wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	wc.hCursor = LoadCursor(NULL,IDC_ARROW);
	wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);
	wc.hInstance = hInstance;
	wc.lpfnWndProc = WndProc;
	wc.lpszClassName = "cory";
	wc.lpszMenuName = NULL;
	wc.style = CS_VREDRAW|CS_HREDRAW;
	RegisterClass(&wc);
	hwnd = CreateWindow("cory","window",WS_OVERLAPPEDWINDOW,0,0,800,600,NULL,NULL,hInstance,NULL);
	ShowWindow(hwnd,iCmd);
	UpdateWindow(hwnd);
	while(!done){
		if(PeekMessage(&msg,NULL,0,0,PM_REMOVE)){
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		else{

		}
	}
	UnregisterClass("cory",hInstance);
	return 0;
}
void cMap::display(HWND hwnd){
	int countdown = players.size();
	while(countdown >= 0){
		players[countdown].display(hwnd);
		countdown--;
	}
}
void cMap::addplayer(cPlayer newplayer){
	players.push_back(newplayer);
}
void cMap::moveplayer(int direction){
	if(direction == 1){
		MessageBox(NULL,"CUTOFF","CUTOFF",MB_OK);
		players[currentplayer].setX(250);
	}
	if(direction == 2){
		players[currentplayer].setX(players[currentplayer].getX()+50);
	}
	if(direction == 3){
		players[currentplayer].setY(players[currentplayer].getY()-50);
	}
	if(direction == 4){
		players[currentplayer].setY(players[currentplayer].getY()+50);
	}
	if(currentplayer > players.size()){
		currentplayer = 1;
	}
	else{
		currentplayer++;
	}
}


Whenever i press left right up or down it doesnt do what it shuld. i tried both calling setx(number) and getx in place of the number neither work. Thanks, Cory [Edited by - SSJCORY on June 17, 2005 10:15:46 PM]

Share this post


Link to post
Share on other sites
Advertisement
Not sure where your other post went, but guess what! Same problem, only this time it's more evident since you're explicitly using the wrong index.

Share this post


Link to post
Share on other sites
Ok i fixed it i wasnt lookin i forgot in one of my map constructors. I pressed left and right and stuff and now no access violation but when i press space to redraw the map it nothing is different.
Any ideas why?
-Cory

Share this post


Link to post
Share on other sites
Okay i set it up starting at 0. But still they dont move when i press an arrow key then press space to redraw it redraws the same... I dont understand :'(
-Cory

Share this post


Link to post
Share on other sites
You could always take a look with your debugger. You might have a peculiar, non-compliant compiler. Either way, a debugger would've shown you right away where the problem was.

Share this post


Link to post
Share on other sites
Quote:
Original post by SSJCORY
I have msvc++ 6.0
What do i do with the debugger to find the problem?
-COry


Well, let's see, it's been a little while since I've used MS' debugger...

First, put the cursor into one of the keystroke areas, the line after VK_LEFT: should be good. Then select debug>run to cursor. In one little window should be all of the local variables [or auto-variables if auto is set]. You can change that tab to see more or less.

Then step into [F11] each line. You should see the variables change as you go. Make sure the variables are set to what they should be, and change as they should change. There are a few other things under debug [like quickwatch] that should help you examine things.

Share this post


Link to post
Share on other sites
Okay i got the debugger now for some reason it highlights x in red when it gets changed to -50. Why does it do that? When i am calling setx(getx-50) it doest that right so why is it in red? Does that mean it is only a local change and does not affect the variable on a global basis?
Thanks
-CORY
EDIT also i notice for some reason the currentplayer is not getting changed at all.........

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!