• Create Account

## Detect single mouseclick - move sprite

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

10 replies to this topic

### #1mollekake  Members

143
Like
0Likes
Like

Posted 25 February 2013 - 04:40 AM

Hello everybody!

I'm a bit new to this whole game programming, and pretty new to xna/c# aswell.

I've been doing some hobby-programming with c#, but nothing large.

I know some java, and have made a couple plugins for minecraft/bukkit, so hopefully i will grasp the c# language quicker.

I want to make a point-and-click game in 2D.

I will be doing my pixelart myself, and it will be extreamly basic

So here's my problem:

I want the spreite to move along the X axis on the screen when i click once with the mouse.

If i click again, it will move to the new position.

But i can't get it to detect a single click.

I can make the sprite move when i hold down the mouse-button.

i found this one way with google's help, but i don't understand how that should work:

It doesn't anyway.

var currentMouseState = Mouse.GetState();
var lastMouseState = currentMouseState;
...
if (lastMouseState.LeftButton == ButtonState.Released && currentMouseState.LeftButton == ButtonState.Pressed)
{
Console.WriteLine("should move");
Position += direction * 5;
}


i am ofcourse missing a bunch of code, but the console still don't output "should move".

Edited by mollekake, 25 February 2013 - 04:42 AM.

### #2Inuyashakagome16  Members

836
Like
0Likes
Like

Posted 25 February 2013 - 06:38 AM

I usually use > http://rbwhitaker.wikidot.com/xna-tutorials for tutorials for XNA or books but that site has been good so far.

What the code is doing:

line 1 is taking the current mouse state (Mouse.GetState()) and putting that value in currentMouseState.

line 2 is taking the currentMouseState variable we just created and assigning it to "lastMouseState"

the if statement is saying "if the lastMouseState is equal to the current button released and the currentMouseState is equal to the current button release, then console.writeline("should move"); and changing the position.

Have you looked at other tutorials? From state to finish pretty much. Because the one that I mentioned above has a decent page on mouse input ( http://rbwhitaker.wikidot.com/mouse-input

### #3DpakoH  Members

1198
Like
0Likes
Like

Posted 25 February 2013 - 07:13 AM

i think you are doing it wrong.

var currentMouseState = Mouse.GetState();
var lastMouseState = currentMouseState;


this is wrong, should be

var lastMouseState = currentMouseState;
var currentMouseState = Mouse.GetState();

if (lastMouseState.LeftButton == ButtonState.Pressed && currentMouseState.LeftButton == ButtonState.Released){
...
}


### #4Inuyashakagome16  Members

836
Like
0Likes
Like

Posted 25 February 2013 - 07:19 AM

i think you are doing it wrong.

var currentMouseState = Mouse.GetState();
var lastMouseState = currentMouseState;


this is wrong, should be

var lastMouseState = currentMouseState;
var currentMouseState = Mouse.GetState();

if (lastMouseState.LeftButton == ButtonState.Pressed && currentMouseState.LeftButton == ButtonState.Released){
...
}


I don't think that'll work.

var lastMouseState = currentMouseState;
var currentMouseState = Mouse.GetState();


That's saying that you are assigning (and declaring) lastMouseState and assigning it currentMouseState. But.. that's not even declared yet. 2nd line is "var currentMouseState" and assigns that variable "Mouse.GetState();" which means.. you would be assigning a variable that isn't even declared yet to a new variable. So that would most likely throw an error.

Edited by Inuyashakagome16, 25 February 2013 - 07:21 AM.

### #5mollekake  Members

143
Like
0Likes
Like

Posted 25 February 2013 - 07:55 AM

Yeah that's the issue. I can't figure out how to do this. Should be simple though. The tutorial you linked to Inu, is almost the same as the one i used.

But that gives me errors.

Here's the code:

private MouseState oldState;
MouseState newState = Mouse.GetState();

if (newState.LeftButton == ButtonState.Pressed && oldState.LeftButton == ButtonState.Released)
{
// do something here
}

oldState = newState; // this reassigns the old state so that it is ready for next time


And the errors are:

Invalid expression term 'private'

and:

; expected, on the same line

### #6Inuyashakagome16  Members

836
Like
1Likes
Like

Posted 25 February 2013 - 08:06 AM

Yeah that's the issue. I can't figure out how to do this. Should be simple though. The tutorial you linked to Inu, is almost the same as the one i used.

But that gives me errors.

Here's the code:

private MouseState oldState;
MouseState newState = Mouse.GetState();

if (newState.LeftButton == ButtonState.Pressed && oldState.LeftButton == ButtonState.Released)
{
// do something here
}

oldState = newState; // this reassigns the old state so that it is ready for next time


And the errors are:

Invalid expression term 'private'

and:

; expected, on the same line

Go down to the bottom of the page and click "show source code" that will show you where the "private MouseState oldState;" should be. It should be up with your spritebatch variable just inside of the class, not inside of any methods. (Initialize, Update, Draw, etc)

### #7mollekake  Members

143
Like
0Likes
Like

Posted 25 February 2013 - 08:16 AM

Holy crap, i've been thinking completely wrong

Thanks! Ofcourse it goes outside the Update method.

Forgot that i was working inside the update method. It's quite different to do game stuff than other stuff

### #8Inuyashakagome16  Members

836
Like
0Likes
Like

Posted 25 February 2013 - 08:18 AM

Holy crap, i've been thinking completely wrong

Thanks! Ofcourse it goes outside the Update method.

Forgot that i was working inside the update method. It's quite different to do game stuff than other stuff

it really is. Well good, and you're welcome. Just look through these forums about XNA and just general game development and observe, or comment if you want. That's part of how I learned. That and actually coding it and reading through books.

Good luck!

### #9mollekake  Members

143
Like
1Likes
Like

Posted 25 February 2013 - 08:21 AM

Yeah. I learn the best by trying, so i will be testing a lot

Thanks for the help so far

### #10DpakoH  Members

1198
Like
0Likes
Like

Posted 25 February 2013 - 08:47 AM

i think you are doing it wrong.

var currentMouseState = Mouse.GetState();
var lastMouseState = currentMouseState;


this is wrong, should be

var lastMouseState = currentMouseState;
var currentMouseState = Mouse.GetState();

if (lastMouseState.LeftButton == ButtonState.Pressed && currentMouseState.LeftButton == ButtonState.Released){
...
}


I don't think that'll work.

var lastMouseState = currentMouseState;
var currentMouseState = Mouse.GetState();


That's saying that you are assigning (and declaring) lastMouseState and assigning it currentMouseState. But.. that's not even declared yet. 2nd line is "var currentMouseState" and assigns that variable "Mouse.GetState();" which means.. you would be assigning a variable that isn't even declared yet to a new variable. So that would most likely throw an error.

currentMouseState is initialized when it is declared, as follows:

protected MouseState lastMouseState;
protected MouseState currentMouseState = Mouse.GetState();


and the code i posted is placed in the Update method. furthermore this statement

if (newState.LeftButton == ButtonState.Pressed && oldState.LeftButton == ButtonState.Released)


is wrong, at least the logic is wrong.

### #11mollekake  Members

143
Like
0Likes
Like

Posted 25 February 2013 - 08:56 AM

Well it is kinda wrong, that if line detects when the mouse is clicked and the last state was unclicked.

i changed it to this to be correct:

if (newState.LeftButton == ButtonState.Released && oldState.LeftButton == ButtonState.Pressed)


Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.