I have a function that orders a movement to a sprite, how am I gonna reset it? whereas it will do the same order all over again, and after another condition is triggered it will stop
inside my function there is a boolean and a float.
MyFunction(float a, float b, boolean trig){
if(true){
//your condition/order
}
else
{
//do nothing
}
}
here is my code for resetting:
MyFunction(false or true).reset();
here is my code for stopping:
MyFunction(false);
is there something wrong in my code? or are there any better style of doing this, thanks in advance :)
you wrote if(true), instead if(trig)
Related
I am not able to understand how this will work as printAll is called from itself and calling in a for loop so how this function will reach at end. as when we call printAll from the for loop it will go and start the method again without ending it and then again comes to for loop and call printAll method without ending it and it will continuos do this, so can anyone explain how this funtions will work. because I am not able to understand this phenomenon.
private void printAll(Integer u, Integer d, List<Integer> savearray) {
if (u.equals(d)) {
this.saveinlist = new ArrayList<Integer>(savearray);
return ;
}
for (Integer i : adjList[u]) {
savearray.add(i);
printAll(i, d, savearray);
savearray.remove(i);
}
}
This is a Recursive method. In a recursive method there are two parts.
1. Base condition
2. Recursive part
Base condition is which when we want to stop this recursive calling.
In recursive part it called it self with some arguments. Because we have to use same set of codes to get our result.
https://www.geeksforgeeks.org/recursion/ go to this link if you know more about
I have this logic in my code
if(r40.isChecked()){
//do this
}
It works great, but I needed the negative, "if rb.isNotChecked" or ".isChecked() = false"
But I cant see to find it, also I can't use the RadioGroup because all my RadioButtons are not inside one, I'm controlling them manually
But I cant see to find it
if(!r40.isChecked()){
//do this
}
I mean you COULD do if(r40.isChecked() == false){
//do something
}
BUT if(!r40.isChecked){
//do something
} Is much better/cleaner
Try with negative check
if(!r40.isChecked()){
//do this
}
this check if radio isn`t checked.
Try this:
if (! r40.isChecked()) {
//Do something
}
In more detail the exclamation mark added in this code reverses the following code in the same block, causing the code to mean:
if (r40.isNotChecked()) {
//Do something
}
I hope this helped!
I'm a beginner in Java programming & I am making an application requiring an object to move around a grid filled with squares.
The object should only move one square at a time and if the user wants to move into another square, they must press the key again. My move method is the following:
public void move() {
x += dx;
y += dy;
}
I am using the KeyListener interface to implement the keyPressed, keyTyped and keyReleased methods and I have conditions like the one in the fragment below inside KeyPressed
//KeyPressed
int c = e.getKeyCode();
if (c == KeyEvent.VK_UP) {
player.setDy(-5);
}
This allows the object to move freely. However, it will clearly continue to move as long as the UP arrow is pressed.
Is there any way to have to object move up by say -5 once and then stop even if the key is still pressed?
I am unsure whether I need to change my move method or the KeyListener methods to do this.
I hope that I have been clear enough as to what I'm asking and I'd highly appreciate any pointers.
first of all : you should use Synchronization if you call class-methods from within listeners like keyPressed or keyReleased - thats because your listener-method can be called from multiple threads so your class-method (player.setDy()) can (and will) be called in parallel - you will need to make sure that each call to setDy happens before the next one.
Also : keyTyped is much better in many cases : https://stackoverflow.com/a/7071810/351861
An example could look like this:
public void keyTyped(KeyEvent arg0) {
if(arg0.getKeyCode() == KeyEvent.VK_UP)
{
synchronized(player)
{
player.setDy(-5);
}
}
}
this will call setDy sequentially and reliably. Now all you need to do is to make sure that setDy works as intended, hence sets the position only once
easiest would be to add a boolean to indicate, that a moving key is pressed
class member : boolean movingKeyPressed = false
in key pressed :
if (movingKeyPressed) {
return;
} else {
// do stuff
movingKeyPressed = true;
}
in key released method :
movingKeyPressed = false;
Is there any easy way such as break for whiles to stop/skip a running function?
public void a(){
do_this();
//break the function here
do_that(); //do not load this
}
What I want to approach could be done with boolean and if statements as well, but I was wondering if there was some shortcut command in order to do this.
public void a(){
do_this();
if(1==1)
return;
do_that(); //do not load this
}
Just curious, why do you want to write such a code?
let the first function return a boolean that indicates if the calling function should move on:
public void a(){
boolean doContinue = do_this();
if(doContinue) {
do_that();
}
}
i you ALWAYS want to skip the second part, it's dead code. you could just remove it.
Always have in mind that your code should be readable for other programmers!
I had a strange problem with Java (solved). I'm asking this because i'm curious of what's going on there.
What's the difference between:
if(Transfers.protoSendLong(output, date.getTime())){}
and simply
Transfers.protoSendLong(output, date.getTime());
The difference I see is that the 1st works and the 2nd don't :S
Is there any difference on execution?
I don't think you need to know what protoSendLong() is about to answer. If you need it, just ask.
EDIT:
You have the code of the method here. That's the most I can give you.
public static boolean protoSendLong(ObjectOutputStream output, long x) {
boolean r = false;
try {
output.writeLong(x);
r = true;
} catch (IOException ex) {
Logger.getLogger(Transfers.class.getName()).log(Level.SEVERE, null, ex);
}
return r;
}
There is no difference it all in the two snippets with respect to invoking the method. It gets invoked in both cases. If the method doesn't do what you expect in one case or another it is nothing to do with this snippet.
Since you have the function call inside if statement, we could assume that the function
Transfers.protoSendLong
return Boolean.
In the code,
if(Transfers.protoSendLong(output, date.getTime()))
{
"do something"
}
Thus, "do something" is executed only when the function "Transfers.protoSendLong" returns TRUE. If it returns false "do something" is skipped.
But in case of ,
Transfers.protoSendLong(output, date.getTime());
TRUE or FALSE maybe returned but nothing would changed the flow of code since there is no if statement or any variable to catch it.