LibGDX problems with if - java

I have this if statement in my LibGDX game. The problem is that my player watching left, he moves in the left direction, which is correct, but if he watshes in right direction he moves left anyway. How can I solve this problem?
public boolean keyUp(int keycode) {
if (keycode == Input.Keys.A && player.b2body.getLinearVelocity().x >=-0.5) {
SpinDashleft();
return true;
}
else if (keycode == Input.Keys.A && player.b2body.getLinearVelocity().x <=0.5) {
SpinDashright();
return true;
}
return false;
}
public void SpinDashleft() {
player.b2body.applyLinearImpulse(new Vector2(-7.1f, 0), player.b2body.getWorldCenter(),true);
}
public void SpinDashright() {
player.b2body.applyLinearImpulse(new Vector2(7.00f, 0), player.b2body.getWorldCenter(),true);
}

What is the 0.5 for? Check for a minimum speed? If so, you probably got the signs wrong there.
if (keycode == Input.Keys.A) {
if (player.b2body.getLinearVelocity().x >= 0.5) {
SpinDashleft();
return true;
}
if (player.b2body.getLinearVelocity().x <= -0.5) {
SpinDashright();
return true;
}
}

Related

Turning around at edges of a world

For a project I am supposed to write code that will have an actor have a chance to turn left or right 25% and then move 50% and then it is also supposed to avoid running out of bounds. The following code is what I have so far but I am not sure what isn't working because i am still having problem keeping the actor inbounds. Thanks
/**
* Creates a new Food object.
*/
public Food()
{
super();
}
//~ Methods ...............................................................
// ----------------------------------------------------------
/**
* Take one step, either forward or after turning left or right.
*/
public void act()
{
int x = Random.generator().nextInt(100);
this.changeDirection();
if (x < 25)
{
this.turn(LEFT);
}
else if ((25 < x) && ( x < 50))
{
this.turn(RIGHT);
}
else
{
this.move();
}
}
public void turnAround()
{
this.turn(RIGHT);
this.turn(RIGHT);
}
public void changeDirection()
{
if ((this.getGridY() == this.getWorld().getHeight() ||
this.getGridX() == this.getWorld().getWidth()))
{
this.turnAround();
this.move();
}
else if ((this.getGridY() == 0 || this.getGridX() == 0))
{
this.turnAround();
this.move();
}
else if ((this.getGridY() == this.getWorld().getHeight()) &&
this.getGridX() == this.getWorld().getWidth())
{
this.turnAround();
this.move();
}
else if ((this.getGridY() == 0) && (this.getGridX() == 0))
{
this.turnAround();
this.move();
}
}

When running my code sometimes steps are ignored, why is this happening? Java

So I made a Pong game in java and for some reason sometimes the ball doesn't check the collision with the bottom or the top of the screen. What can I do to fix this?
this piece is the collision checking with the bottom or the top of the screen:
if(y-20<0 && dir == UP_RIGHT)
dir=DOWN_RIGHT;
else if (y-20<0 && dir == UP_LEFT)
dir=DOWN_LEFT;
//Down
if(y+20>500 && dir == DOWN_RIGHT)
dir=UP_RIGHT;
else if(y+20>500 && dir == DOWN_LEFT)
dir=UP_LEFT;`
The ball collision system:
public void Blogic(Random random,HumanPaddle p1, HumanPaddle p2)
{//collision
//Left
if(x<=50)
{
if(y>= p1.y && y<=p1.y + 80)
RandomDir(6,4,random);
}
//Right
else if(x>=650)
{
if(y>=p2.y && y<=p2.y+ 80)
RandomDir(3,1,random);
}
//Up
if(y-20<0 && dir == UP_RIGHT)
dir=DOWN_RIGHT;
else if (y-20<0 && dir == UP_LEFT)
dir=DOWN_LEFT;
//Down
if(y+20>500 && dir == DOWN_RIGHT)
dir=UP_RIGHT;
else if(y+20>500 && dir == DOWN_LEFT)
dir=UP_LEFT;
}
The ball movement system:
public void move()
{
switch(dir)
{
case STOP:
xVel *=0; yVel *=0;
break;
case LEFT:
xVel--;
break;
case UP_LEFT:
xVel--;
yVel--;
break;
case DOWN_LEFT:
xVel--;
yVel++;
break;
case RIGHT:
xVel++;
break;
case UP_RIGHT:
xVel++;
yVel--;
break;
case DOWN_RIGHT:
xVel++;
yVel++;
break;
default:
break;
}
// Velocity settings
if (xVel>=3)
xVel=3;
else if (xVel <=-3)
xVel=-3;
if(yVel >=3)
yVel=3;
else if(yVel<=-3)
yVel=-3;
x += xVel;
y += yVel;
}
Run:
public void run()
{
for (;;)
{
p1.move();
p2.move();
ball.move();
ball.Blogic(rand, p1, p2);
Score();
repaint();
try
{
Thread.sleep(10);
}
catch (InterruptedException ex)
{
Logger.getLogger(Tennis.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
I'm having a hard time seeing exactly how the collision works without the visual but I would try without the extra checking on the direction for the up and bottom boards. I believe that by doing this you are checking for something that should ways be true anyways so it is simply too strict.
Remove: "&& dir == UP_RIGHT" and the contrary.
For some reason, this might not be true and completely skip over the check.
I made this changes inside that piece and now it is working fine.
if(y <=10)
{
if(dir == UP_RIGHT) dir=DOWN_RIGHT;
else if (dir == UP_LEFT)dir = DOWN_LEFT;
else yVel=-yVel;
}
//Down
if(y>=490)
{
if(dir == DOWN_RIGHT)dir=UP_RIGHT;
else if (dir ==DOWN_LEFT) dir=UP_LEFT;
else yVel=-yVel;
}

Implementing a game menu in processing3

I am making a little game for my university coursework and got stuck at the menu creation. Been trying to solve this for a while. Here goes the code:
Before trying to fiddle around the loop and noLoop() functions and before I moved the arrow placement from void setup to void keypressed my game has worked. Now the program freezes before running draw.
What I want is to only display the menu background before the key, in this case s for start, has been pressed. If anyone could come up with some directions it would be of a great help.
Thanks in advance.
int totalArrows = 6;
arrowclass[] theArrows = new arrowclass[totalArrows];
PImage[] imgList = new PImage[4];
PImage bg;
PImage life;
PImage menu;
int score;
int loose = 3;
void setup() {
size(400, 600);
bg = loadImage("bck.jpg");
life = loadImage("life.png");
menu = loadImage("menu.jpg");
background(menu);
// loading the arrow imgs
for (int i= 0; i<4; i++) {
println(i+".png");
imgList[i] = loadImage(i+".png");
}
noLoop();
}
void draw() {
textSize(32);
text(score,30,100);
// active area
fill(255,31,31);
rect(0,500,width,100);
//lifetrack
if (loose==3){
image(life,10,10);
image(life,45,10);
image(life,80,10);
}
if (loose==2){
image(life,10,10);
image(life,45,10);
}
if (loose==1){
image(life,10,10);
}
// calling class action, movement
for (int i = 0; i<totalArrows; i++) {
theArrows[i].run();
if (theArrows[i].y>475 && theArrows[i].y<600) {
theArrows[i].inactive = true;
}
if(theArrows[i].did == false && theArrows[i].y>598) {
theArrows[i].lost = true;
}
if(theArrows[i].lost && theArrows[i].did == false && theArrows[i].wrongclick == false){
loose--;
theArrows[i].lost = false;
}
}
if (loose == 0){
textSize(32);
text("gameover",width/2,height/2);
for(int i=0; i<totalArrows;i++){
}
}
}
void keyPressed() {
if (key == 'p'){
// placing tha arrows. (i*105 = positioning) THIS PART IS AN ISSUE FOR SURE. SEE ARROW CLASS
for (int i= 0; i<totalArrows; i++) {
theArrows[i] = new arrowclass(-i*105);
}
loop();
}
}
void keyReleased() {
for (int i= 0; i<totalArrows; i++) {
if (theArrows[i].inactive && theArrows[i].did == false) {
if (keyCode == UP && theArrows[i].id == 3){
score++;
theArrows[i].did = true;
}
if (keyCode != UP && theArrows[i].id == 3){
loose--;
theArrows[i].wrongclick = true;
}
if (keyCode == DOWN && theArrows[i].id == 0){
score++;
theArrows[i].did = true;
}
if (keyCode != DOWN && theArrows[i].id == 0){
loose--;
theArrows[i].wrongclick = true;
}
if (keyCode == RIGHT && theArrows[i].id == 2){
score++;
theArrows[i].did = true;
}
if (keyCode != RIGHT && theArrows[i].id == 2){
loose--;
theArrows[i].wrongclick = true;
}
if (keyCode == LEFT && theArrows[i].id == 1){
score++;
theArrows[i].did = true;
}
if (keyCode != LEFT && theArrows[i].id == 1){
loose--;
theArrows[i].wrongclick = true;
}
}
}
}
And the arrow class:
class arrowclass{
int y;
int id;
boolean did;
boolean inactive;
boolean lost;
boolean wrongclick;
// proprieties of the class
arrowclass(int initY){
y = initY;
id = int(random(4));
did = false;
inactive = false;
lost = false;
wrongclick = false;
}
//actions
void run(){
image(imgList[id],width/2,y);
// score goes up, speed goes up
y += 2+score;
// reset arrow to default
if (y>600) {
y = -50;
id = int(random(4));
inactive = false;
did = false;
lost = false;
wrongclick = false;
}
}
}

Android MultiTouch does not work

#Override
public boolean onTouchEvent(MotionEvent event)
{
synchronized (getHolder())
{
int aktion = event.getAction();
if (aktion == MotionEvent.ACTION_DOWN)
{
touched = true;
bar.shareTouch(event.getX(), event.getY(), touched);
}
else if (aktion == MotionEvent.ACTION_UP)
{
touched = false;
bar.shareTouch(event.getX(), event.getY(), touched);
}
if (aktion == MotionEvent.ACTION_POINTER_DOWN)
{
touched2 = true;
bar.shareTouch2(event.getX(), event.getY(), touched2);
}
else if (aktion == MotionEvent.ACTION_POINTER_UP)
{
touched2 = false;
bar.shareTouch2(event.getX(), event.getY(), touched2);
}
}
return true;
}
This is a Code to chech if first Finger is going onto the screen or leaving it. The same for another finger.
public void shareTouch2(float xTouch2, float yTouch2, boolean touched2)
{
if (xTouch2 <= gameViewWidth/2) {
if (touched2 == true) {
touchedLeft2 = true;
}
else if(touched2 == false) {
touchedLeft2 = false;
}
}
else if (xTouch2 > gameViewWidth/2) {
if (touched2 == true) {
touchedRight2 = true;
}
else if(touched2 == false) {
touchedRight2 = false;
}
}
}
public void shareTouch(float xTouch, float yTouch, boolean touched)
{
if (xTouch <= gameViewWidth/2) {
if (touched == true) {
touchedLeft = true;
}
else if(touched == false) {
touchedLeft = false;
}
}
else if (xTouch > gameViewWidth/2) {
if (touched == true) {
touchedRight = true;
}
else if(touched == false) {
touchedRight = false;
}
}
}
private void moveRight()
{
x += 3;
}
private void moveLeft()
{
x -= 3;
}
private void checkTouch() {
if ((touchedLeft == true && touchedRight2 == false) || (touchedLeft2 == true && touchedRight == false)) {
moveLeft();
}
else if ((touchedLeft == false && touchedRight2 == true) || (touchedLeft2 == false && touchedRight == true)) {
moveRight();
}
else if ((touchedLeft == true && touchedRight2 == true) || (touchedLeft2 == true && touchedRight == true)) {
}
}
The checkTouch() is called in the onDraw() Method. Now if I place a finger on the right side of the screen it moves right. Same for left. But if I touch left and the right without removing the left finger the Object still moves left although it should stop. Now when I leave the left finger it still moves left although it should move right.
I hope you understand my problem.
Hope you can help
You can't just assume the ACTION_POINTER_UP Event will only be sent for the same finger ACTION_POINTER_DOWN was sent.
Just as the ACTION_DOWN event is always fired for the first finger to touch down, the ACTION_UP event is only fired for the last finger to lift (i.e. when there are no more fingers on the screen). All others will get an ACTION_POINTER_UP event, even if it's the finger that started the gesture.
However, the Android documentation does specify
Each pointer has a unique id that is assigned when it first goes down [and] remains valid until the pointer eventually goes up.
So in theory, your first and second finger should still have the same ID, regardless whether the Event that reports them.
The solution is simple, then: Use getPointerCount() and getPointerID() to keep track of your fingers.
This may be easier if you refactor your code to account for more than two fingers by replacing the Booleans touchedLeft and touchedLeft2 with a single counter, e.g. fingersOnLeftSide - which has the neat side effect of only needing one shareTouch() method and otherwise reducing redundancy in your code.
EDIT:
Disclaimer: This is just off the top of my head, untested and written without knowledge of any of your code other than what you posted. Not necessarily the best way to solve your problem, but the shortest I could think of.
This is the Event handler:
public boolean onTouchEvent(MotionEvent event)
{
synchronized (getHolder())
{
int aktion = event.getAction();
if (aktion == MotionEvent.ACTION_DOWN || aktion == MotionEvent.ACTION_POINTER_DOWN
|| aktion == MotionEvent.ACTION_UP || aktion == MotionEvent.ACTION_POINTER_UP)
{
bar.clearTouches();
for (int i = 0; i < event.getPointerCount(); i++) {
bar.shareTouch(event.getX(i)); // we don't need the y coordinate anyway
}
}
}
return true;
}
The rewritten shareTouch() to go along with it, as well as clearTouches() I introduced to make this simpler:
private void shareTouch(float x)
{
if (x < gameViewWidth/2) {
fingersLeft++;
} else {
fingersRight++;
}
}
private void clearTouches()
{
fingersLeft = 0;
fingersRight = 0;
}
And finally, the new checkTouch():
public void checkTouch()
{
if (fingersLeft > fingersRight) {
moveLeft();
} else if (fingersRight > fingersLeft) {
moveRight();
}
}

Messy for loop/if statement

Im doing a pacman game. The following code is for ghosts movement and it works correctly. But I have to include a another check. The problem is that I always ruin the logic.
Current code:
public void moveGhost(Tiles target) {
if(specialIntersections()){
direction = direction; //keeps going in the same direction
}
else{
int oppDir;
if(direction == UP){
oppDir = DOWN;
}
else if(direction == DOWN){
oppDir = UP;
}
else if(direction == LEFT){
oppDir = RIGHT;
}
else{
oppDir=LEFT;
}
double minDist = 10000.0;
Tiles potentialNext;
for(int i=0; i<4; i++){
if(i!=oppDir){
potentialNext = maze.nextTile(getCurrentPos(), i);
if(!(potentialNext.wall()) && check(potentialNext)){
if(calculateDistance(target, potentialNext) < minDist){
minDist = calculateDistance(target, potentialNext);
futureDirection = i;
}
}
}
}
}
changeDirection();
timer++;
increment();
x += xinc;
y += yinc;
tunnel();
}
Another check I need to include:
//if the door is a wall (closed) the object cannot go through it
if(DoorIsWall()){
if(!(potentialNext.wall()) && !(potentialNext.door()) && check(potentialNext)){
I generally write a new method when my conditions start to get unruly:
if (isTileValid(potentialNext)) {
// do stuff
}
private boolean isTileValid(TileObject someTile) {
if (someTile.wall()) {
return false;
}
if (someTile.door()) {
return false;
}
if (! check(someTile)) {
return false;
}
return true;
}

Categories