• Advertisement
Sign in to follow this  

Moving saucer threw the use of mouse input.

This topic is 4247 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

Hi all, Ive been working on an exercise from Programming Games in 24Hrs from Michael Morrison in which I have to try and move the flying saucer threw the use of my mouse. I'm getting it as far as it moves with the mouse whenever I move it, but, it should only move if the left mouse button is pressed. I have tried several things but can't seem to get it working. I was wondering if any of you could help me out with this. Ive include the functions in which I think I should be able to change the code:
void MouseButtonDown(int x, int y, BOOL bLeft)
{
	if (bLeft)
	{
		MouseMove(x, y);
	}
	else
	{
		giSpeedX = 0;
		giSpeedY = 0;
	}
}

void MouseButtonUp(int x, int y, BOOL bLeft) {}

void MouseMove(int x, int y) 
{
	//Set the saucer position to the mouse position
	giSaucerX = x - (gpSaucer->GetWidth() / 2);
	giSaucerY = y - (gpSaucer->GetHeight() / 2);		
}

Share this post


Link to post
Share on other sites
Advertisement
You're almost there - look carefully.

Where does the saucer position get set? In MouseMove(). Where is MouseMove() called from? MouseButtonDown(). Under what conditions will MouseButtonDown() call MouseMove()? Are those the conditions you want?

Share this post


Link to post
Share on other sites
Hi Apoch,

Thanks for the helping hand, I know (or think) I'm close, it's just that last bit that get's me.

Well, the condition should be that, when the left mouse button is pressed, no matter where it is, the saucer should go to that place and move along with the mouse aslong as the left mouse button is pressed.

Darn, so close but yet, so far away LOL

Share this post


Link to post
Share on other sites
Ahh, I read your question backwards initially.

What you need to do is look for other places where MouseMove() might be getting called - I'm not familiar with the book in question, but I imagine that (according to standard conventions) MouseMove() gets called automatically whenever the mouse moves. This means that it will get called regardless of the button up/down state - and then it will get called again from MouseButtonDown() where you call it by hand.

Share this post


Link to post
Share on other sites
Got it!

//-------------------------------------------------------------------------------------------------------------------------
// UFO Application
// C++ Header - UFO.h
//-------------------------------------------------------------------------------------------------------------------------

#pragma once

//-------------------------------------------------------------------------------------------------------------------------
// Include Files
//-------------------------------------------------------------------------------------------------------------------------
#include <windows.h>
#include "Resource.h"
#include "GameEngine.h"
#include "Bitmap.h"

//-------------------------------------------------------------------------------------------------------------------------
// Global Variables
//-------------------------------------------------------------------------------------------------------------------------
HINSTANCE ghInstance;
GameEngine* pGame;
const int giMAXSPEED = 2;
Bitmap* gpBackground;
Bitmap* gpSaucer;
BOOL gbDragging; //to determine whether the mouse is being dragged!!!
int giSaucerX, giSaucerY;
int giSpeedX, giSpeedY;



void MouseButtonDown(int x, int y, BOOL bLeft)
{
if (bLeft && !gbDragging)
{
gbDragging = TRUE;
MouseMove(x, y);
}
else
{
giSpeedX = 0;
giSpeedY = 0;
}
}

void MouseButtonUp(int x, int y, BOOL bLeft)
{
//Release mouse
ReleaseCapture();

//Stop dragging
gbDragging = FALSE;
}

void MouseMove(int x, int y)
{
if (gbDragging)
{
//Set the saucer position to the mouse position
giSaucerX = x - (gpSaucer->GetWidth() / 2);
giSaucerY = y - (gpSaucer->GetHeight() / 2);
}

//Force a repaint to redraw the saucer
InvalidateRect(pGame->GetWindow(), NULL, FALSE);
}





Thanks for the tips Apoch, I started thinking abit about it and while I knew I had to use bLeft as a condition, it was not all, I had to make sure that dragging the mouse would make the saucer follow it, hence the use of gbDragging.

[Edited by - J0Be on July 9, 2006 5:16:23 AM]

Share this post


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

  • Advertisement