Detect single mouseclick - move sprite

Started by
9 comments, last by mollekake 11 years, 1 month ago

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 tongue.png

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".

Advertisement

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 )

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 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.

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

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. :P It should be up with your spritebatch variable just inside of the class, not inside of any methods. (Initialize, Update, Draw, etc)

Holy crap, i've been thinking completely wrong :P

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 :P

Holy crap, i've been thinking completely wrong tongue.png

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 tongue.png

it really is. :P 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!

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

Thanks for the help so far :)

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.

This topic is closed to new replies.

Advertisement