My method some reason doesnt work I want it so when I click tab it grabs the mouse and moves the player but when clicked again I want it to release the mouse and stop the players rotation I tried putting a print statement into the movePlayerWithMouse method but it just returns true the only thing that does change is the mouse each time I click tab it releases and grabs any help?
Heres the method I use:
boolean movePlayerWithMouse = false;
while(Keyboard.next()) {
if(!Keyboard.getEventKeyState()) {
if(Keyboard.getEventKey() == Keyboard.KEY_TAB) {
movePlayerWithMouse = !movePlayerWithMouse;
Mouse.setGrabbed(!Mouse.isGrabbed());
}
}
}
if(movePlayerWithMouse = !movePlayerWithMouse) {
rotY -= Mouse.getDX() / SENSITIVITY;
}
Related
So for a school project, I have to create a game with a program called 'Processing'.
I am creating a main menu with the switch statement. For that I want to use the buttons 'Start', 'Help', and 'exit'.i would like to use those buttons to change the variables of the switch statement. Therefore I'm using "mousePressed". The problem is that which button I'm pressing, is giving me the same result as the 'exit' button. Could somebody give me tips on how I can structure my menu better or even make my button work? I am using a library on Processing called 'ControlP5' to make my buttons.
here is my code so far:
int mode; // 1: intro screen, 2: game , 3: game over
final int INTRO = 1;
final int PLAY = 2;
final int GAMEOVER = 3;
//==============================================================
void setup(){
size(1920,1080);
mode = 1;
}
//========================================================
void draw(){
if(mode == 1){
introScreen();
}
else if (mode == 2){
gameItself();
}
else if (mode == 3){
gameOver();
}
else println("mode error");{
}
}
void introScreen(){
mode = 1;
static int Page = 0;
import controlP5.*;
ControlP5 cp5;
cp5= new ControlP5(this);
switch(Page){
case 0: // main menu
cp5.addButton("Start").setValue(0).setPosition(1420,250).setSize(400,100);
cp5.addButton("Exit").setValue(0).setPosition(1420,650).setSize(400,100);
cp5.addButton("Help").setValue(0).setPosition(1420,450).setSize(400,100);
break;
case 1: //help menu
cp5.addButton("Back").setValue(0).setPosition(1420,450).setSize(400,100);
break;
}
public void Start(){
if(mousePressed){
mode = 2; // switching to the game itself
}
println("Start");
}
public void Exit(){
if(mousePressed){
exit(); }
println("Exit");
}
public void Help(){
Page = 1;
println("Help");
}
public void Back(){
if(mousePressed){
Page = 0;
}
println("Back");
}
void gameItself(){
// game and stuff
}
void gameOver(){
//gameover
}
Take a look at how the mousePressed event works. You may use this information useful.
To achieve your goal as to change the Page variable by clicking buttons, there are multiple options. First, I'll go with the easier one, the one which doesn't need an import but just check for coordinates. Then I'll do the same thing, but with controlP5.
1. Just checking where the clicks lands
I'll go with the most basic one: detecting the "click" and checking if it's coordinates are inside a button.
First, we'll add the mouseClicked() method. This method is called every time a mouse button is pressed.
// I'm typing this out of IDE si there may be some quirks to fix in the code
void mouseClicked() {
switch(Page) {
case 0:
if (mouseX > 1420 && mouseX < 1420+400 && mouseY > 250 && mouseY < 250+100) {
// You're in the main menu and Start was clicked
}
if (mouseX > 1420 && mouseX < 1420+400 && mouseY > 650 && mouseY < 650+100) {
// You're in the main menu and Exit was clicked
}
if (mouseX > 1420 && mouseX < 1420+400 && mouseY > 450 && mouseY < 450+100) {
// You're in the main menu and Help was clicked
}
// You should use 'else if' instead of 3 different if, but I coded it like that so it would be easier to see the small differences between the coordinates
case 1:
if (mouseX > 1420 && mouseX < 1420+400 && mouseY > 450 && mouseY < 450+100) {
// You're un the help menu and Back was clicked
}
}
}
As you can see, I just used the coordinates and size of your buttons to check if the click was located inside one. That's kind of ninja-ing my way out of this issue. I don't know how far into programming you are, or else I would recommand to build a class to handle user inputs, but this way is easy to manage for small exercises like homework.
2. Designing controlP5 buttons
I'm not a ControlP5 expert, so we'll keep close to the basics.
I'll be blunt, the code you provided is ripe with problems, and it's not so many lines, so instead of pointing where it goes wrong I'll give you some skeleton code which will work and on which you can build some understanding. I'll give you the tools and then you can make your project work.
When you design your buttons, if you design them all in the same object, they'll share some properties. For an example, all your buttons will be visible or invisible at the same time. You don't need to redraw them all the time, because they already handle this, so you need to manage them with another method.
You should design your buttons as global objects (as you did), and add them to the ControlP5 object which makes the most sense. You can have one button per object if you want, or many if they are linked together, for an example all the "menu" buttons which appears at the same time could be owned by the same object. Design your buttons in the setup() method if you design them only one time for the whole program. Of course, if this was more than an homework, you may want to avoid the buttons being globals, but it'll be much easier to keep them in memory for a short project.
The "name" of the button is also the name of the method that it'll try to call if you click on it. Two buttons cannot share the same "name". Buttons can have a set value which will be sent to the method that they call.
You don't need to use Processing's mouse events for the buttons to work. They are self-contained: they have their own events, like being clicked on or detecting when the mouse is over them. Here's the documentation for the full list of the methods included in the ControlP5 buttons.
You don't need to manage the buttons in the draw() loop. They manage themselves.
Here's some skeleton code to demonstrate what I just said. You can copy and paste it in a new Processing project and run it to see what's going on.
ControlP5 cp5;
ControlP5 flipVisibilityButton;
int Page = 0;
void setup() {
size(1920, 800);
textAlign(CENTER, CENTER);
textSize(60);
fill(255);
cp5 = new ControlP5(this); // this is ONE object, which will own buttons.
cp5.addButton("MenuButton0") // this is the name of the button, but also the name of the method it will call
.setValue(0) // this value will be sent to the method it calls
.setPosition(1420, 250)
.setSize(400, 100);
cp5.addButton("MenuButton1")
.setValue(1)
.setPosition(1420, 450)
.setSize(400, 100);
cp5.addButton("MenuButton2")
.setValue(2)
.setPosition(1420, 650)
.setSize(400, 100);
flipVisibilityButton = new ControlP5(this); // this is a different object which own it's own controls (a button in this case)
flipVisibilityButton.addButton("flipVisibility")
.setValue(2)
.setPosition(200, height/2)
.setSize(200, 100);
}
void draw() {
// No button management to see here
background(0);
// showing which button has been pressed while also keeping watch to see if the mouse is over one of the cp5 buttons
text(Page + "\n" + cp5.isMouseOver(), width/2, height/2);
}
void MenuButton0(int value) {
ChangePage(value);
}
void MenuButton1(int value) {
ChangePage(value);
}
void MenuButton2(int value) {
ChangePage(value);
}
void ChangePage(int value) {
Page = value;
}
void flipVisibility(int value) {
// When the buttons are invisible, they are also unclickable
cp5.setVisible(!cp5.isVisible());
}
You should be able to expand on this example to do your project, but if you have difficulties don't hesitate to comment here and ask further questions. Have fun!
I need to write a code that places a picture on the screen, plays a sound once the picture has been clicked, and anywhere you click after that will place the sticker. I am done with the picture and sound portion but I am stumped on placing the sticker. I am currently trying to write a command if (EZInteraction.wasMouseLeftButtonReleased() && hatsound.play()) {. But it won't work and if I add parenthesis and change the code to if (EZInteraction.wasMouseLeftButtonReleased()) && (hatsound.play()) {, it will say error on token && invalid OnlySynchronized. Please help. This is my first semester taking CS. My code for the sound and placing sticker portion is
while(true) { //while loop is always true
int clickX = EZInteraction.getXMouse(); // declare an integer variable called clickX and the X mouse coordinate integer is assigned to it
int clickY = EZInteraction.getYMouse(); // declare an integer variable called clickY and assign to it the Y mouse coordinate integer
if (EZInteraction.wasMouseLeftButtonReleased()){ //if the left mouse button is released then
if (hatPicture.isPointInElement(clickX, clickY)){ //if the left mouse button is release on this picture
hatsound.play(); //then hatsound will play
}
if (EZInteraction.wasMouseLeftButtonReleased()) && (hatsound.play()) { //if left mouse released and hat sound plays then
EZ.addImage("hat.png", clickX, clickY); //hat.png will be placed
}
You are trying to find out if the sound has to play or not? I don't think hatsound.play() returns a boolean. You an do it by introducing a boolean variable in the following way.
int clickNum = 0;
boolean hatSoundPlay = false;
while(true) { //while loop is always true
int clickX = EZInteraction.getXMouse(); // declare an integer variable called clickX and the X mouse coordinate integer is assigned to it
int clickY = EZInteraction.getYMouse(); // declare an integer variable called clickY and assign to it the Y mouse coordinate integer
if (EZInteraction.wasMouseLeftButtonReleased()){ //if the left mouse button is released then
if (hatPicture.isPointInElement(clickX, clickY)){ //if the left mouse button is release on this picture
if (!hatSoundPlay) {
hatsound.play(); //then hatsound will play
hatSoundPlay = true;
} else {
EZ.addImage("hat.png", clickX, clickY); //hat.png will be placed
hatSoundPlay = false;
}
}
}
}
The best way though is to see the reference of the class playing the sound and to find out what are the methods and variables. What is the type of your hatsound variable?
Is there a way to figure out the first drag movement of the mouse in Java?
For example:
public void mouseDragged(MouseEvent e)
{
if (first drag of the mouse) // what should I write here?
System.out.println("This is the first drag");
else System.out.println("This isn't the first drag");
}
If I drag the mouse 5 times I should see this text in the console:
This is the first drag
This isn't the first drag
This isn't the first drag
This isn't the first drag
This isn't the first drag
boolean first=true;//this should be a instance variable.
first drag! use a boolean variable to detect first or not.like this
public void mouseDragged(MouseEvent e)
{
if (first) { // this is only true if it's first drag
System.out.println("This is the first drag");
first=false;
}
else {
System.out.println("This isn't the first drag");
}
}
update...
this is how u can detect is it first drag.but there are a problem normally mouse drag event triggered while dragging.to avoid this u can modify this little bit.
declare instance variables
boolean draging = true;
boolean mark = true;
boolean first = true;
print only when dragging start.when we print mouse dragging we stop printing it until mouse released and redragging.
public void mouseDragged(java.awt.event.MouseEvent evt) {
draging = true;
if (mark) {
if (first) {
System.out.println("This is the first drag");
}else{
System.out.println("This isn't the first drag");
}
mark = false;
}
}
change first to false so first dragging is enough.and ready for print new drag[mark = true]
public void mouseReleased(java.awt.event.MouseEvent evt) {
if (draging) {
mark = true;
first=false;
}
}
this is the output of 1st and updated examples.there is a problem of 1st code [because event drag is triggered continuously while dragging ,not ones].
first example
This is the first drag
This is the first drag
This is the first drag
.............................//this continues until u finish[released] first drag
This isn't the first drag
This isn't the first drag
This isn't the first drag
................................
updated one
This is the first drag //a drag [ click--move--relesed] mark only 1time
This isn't the first drag
This isn't the first drag
This isn't the first drag
...............................
I would modify FastSnails solution to this slightly to get around the behaviour that mouseDragged fires on every mouse movement while the button is down:
boolean first = true;
boolean triggered = false;
public void mouseDragged(MouseEvent e){
if(!triggered){
if(first){
System.out.println("This is the first drag");
first=false;
}
else{
System.out.println("This isn't the first drag");
}
triggered = true;
}
}
Of course, we then have to reset that flag in a mouseReleased event, which signals the end of the "drag":
public void mouseReleased(MouseEvent e){
triggered = false;
}
Using this method means that the message only gets triggered once per "drag", rather than at every mouse movement.
I needed access to the ACTION_UP that occurs after the ACTION_MOVE event, and the SimpleOnGestureListener didn't directly support that, so I tried to implement custom touch handling in an activity's onTouchEvent method, in order to get more familiar with it.
The activity is an alarm clock's alert screen (appears when an alarm goes off).
There are 5 kinds of gestures I need to handle. They are:
Single tap - Visual feedback
Double tap - Snooze the alarm
Swipe along the Y access - visual feedback based on distance swiped.
ACTION_UP after an ACTION_MOVE with a distance of less than 50% of the screen's Y axis - visual feedback.
ACTION_UP after an ACTION_MOVE with a distance of greater than 50% of the screen's Y axis - dismiss the alarm.
The function I wrote doesn't work correctly, and I'm pretty sure that it is because the variable "startEvent" is being set on every call to onTouchEvent, when it should be only set on the ACTION_DOWN events of a swipe or the first tap of a double tap.
Here is my code:
private final int DOUBLE_TAP_TIMEOUT = 500; // ms
private boolean isScrolling = false; // flag used to detect the ACTION_UP after a scroll
private boolean possibleDoubleTap = false; // flag used to detect a double tap
private MotionEvent startEvent; // the event that starts the gesture we are trying to detect
private float yPercentScrolled; // the percent the along the (Y axis / 2) that has been scrolled.
#Override
public boolean onTouchEvent(MotionEvent event){
switch (event.getActionMasked()){
case(MotionEvent.ACTION_DOWN):
Log.d("MOTION", "DOWN");
// reset the startEvent given the proper conditions
if(
(!isScrolling && !possibleDoubleTap)
|| (possibleDoubleTap && event.getEventTime() - startEvent.getEventTime() <= DOUBLE_TAP_TIMEOUT)
){
startEvent = event;
}
return true;
case(MotionEvent.ACTION_UP):
Log.d("MOTION", "UP");
if(isScrolling){
isScrolling = false;
if(yPercentScrolled >= 1f){
dismissAlarm();
}
else {
layout.setBackgroundColor(bgColor);
startEvent = null;
possibleDoubleTap = false;
isScrolling = false;
}
}
else if(possibleDoubleTap){
// if we have a double tap
if(event.getEventTime() - startEvent.getEventTime() <= DOUBLE_TAP_TIMEOUT){
snoozeAlarm();
return true;
}
// if we don't have a double tap, do nothing
}
else if(!possibleDoubleTap){
possibleDoubleTap = true;
layout.setBackgroundColor(Color.parseColor("#000000"));
}
return true;
case(MotionEvent.ACTION_MOVE):
Log.d("MOTION", "MOVE");
if(!isScrolling){
isScrolling = true;
}
else {
yPercentScrolled = handleScroll(event, startEvent);
}
return true;
default:
Log.d("MOTION", "other:" + String.valueOf(event.getActionMasked()));
return super.onTouchEvent(event);
}
}
Any ideas on why "startEvent" is being reset when it shouldn't? There are no "MOTION:DOWN" in logcat when it is being reset, so this on is stumping me. This is also the only location where startEvent is being assigned to something other than null.
Thanks.
it seems that startEvent = event is assigning a reference to object event instead of the object itself. so when the value of object event changed. the value of startEvent is also changed.
Try to take and store just the value that you need (in your case is the X, Y, and EventTime) instead of the whole event object.
When I press the button the first time it's supposed to display a message, but the next time I am to press it it should do it's normal use.
private void jButton1ActionPerformed (java.awt.event.ActionEven evt) {
jTextArea1.setText(jTextArea1.getText()+"Let the battle begin!\n");
Basically that is what I want it to do at first click, as well as:
if (jProgressBar1.getValue() > 0) {
if (publicInt.rndNum() > 90)
}
etc, etc..
EDIT:
This is what I did to make it work, thanks to a commenter.
if (publicInt.startPhrase() == true) {
jTextArea1.setText("The battle has begun!\n");
publicInt.sp = false;
}
You should use a boolean, let's say pressed, which will be false, and after pressing the button you set if true... the code will be something like:
boolean pressed = false;
private void jButton1ActionPerformed (java.awt.event.ActionEven evt) {
if (!pressed) {
jTextArea1.setText(jTextArea1.getText()+"Let the battle begin!\n");
pressed = true;
}