Could anyone tell me why this is returning the error
'AL lib: (EE) alc_cleanup: 1 device not closed'
//mouseJoint collision callback
private QueryCallback queryCallback = new QueryCallback() {
#Override
public boolean reportFixture(Fixture fixture) {
if(fixture.getBody() == chest){
//add to remove list
bodiesToRemove.add(chest);
}
if (fixture.testPoint(tmp.x, tmp.y)){
reportFixture = fixture.getBody();
}
if (!fixture.testPoint(tmp.x, tmp.y))
return false;
//assigning bodyB to fixture
jointDef.bodyB = fixture.getBody();
jointDef.target.set(fixture.getBody().getWorldCenter());
//jointDef.target.set(tmp.x, tmp.y);// initial target point coincide with body anchor
joint = (MouseJoint) world.createJoint(jointDef);// creating the join the physics world
return false;
}
};
//main rendering loop
public void render(float delta) {
// TODO Auto-generated method stub
//code clears the screen with the given RGB colour (black)
Gdx.gl.glClearColor( 0f, 0f, 0f, 1f );
Gdx.gl.glClear( GL20.GL_COLOR_BUFFER_BIT );
stage.setCamera(camera);
stage.act(Gdx.graphics.getDeltaTime());
camera.position.x= chest.getPosition().x;
camera.update();
stage.draw();
world.step(1 / 60f, 8, 3);
renderer.render(world, camera.combined);
/////////////////////////////////////////////////////////////////
// this is the code that is causing the error when I try to delete a body
if(bodiesToRemove != null){
for(int i = 0; i <bodiesToRemove.size; i++){
Body b = bodiesToRemove.get(i);
if(b != null){
world.destroyBody(b);
b.setUserData(null);
b = null;
}
bodiesToRemove.clear();
}
}
////////////////////////////////////////////////////////////
}
////////////////////////////////////////////////////////////////////////////
I've changed this and it work's(pasted below), if anyone would like to explain the right way to do this or if this is the correct way that would be helpful.
I just added a boolean to prevent it from destroying the joint
// ////////////////// TOUCH EVENT ///////////////////////////////
//Called when a finger was lifted or a mouse button was released.
#Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
//If no mouseJoint do nothing
System.out.println("touch" + (screenY*0.01f));
System.out.println("touch" + (screenX*0.01f));
if (joint == null)
return false;
//When user is not touching Screen destroy the
//mouseJoint/set to null
if(ran == false){
world.destroyJoint(joint);
joint = null;}
ran = false;
return true;
}
////////////////////////////////
/////////////////////////////////
if(!world.isLocked() && world.getBodyCount() > 0 ){
for(int i = 0; i <bodiesToRemove.size; i++){
Body b = bodiesToRemove.get(i);
if(b == null){
bodiesToRemove.removeIndex(i);
continue;
}
world.destroyBody(b);
ran = true;
bodiesToRemove.removeIndex(i);
}
}else{
bodiesToRemove.clear();
}
////////////////////////////////////////////////////////////
Box2D throws some odd errors, this is not one of them.
I assume you are using LibGDX, basically what is happening is a crash due to Box2D (I also assume it pops up saying "java or whatever has stopped responding"), this error you are seeing is thrown when the app exits while OpenAL is closing streams and cleaning up resources. Don't worry about that too much.
Ensure you are checking of the world is locked before destroying bodies, it usually throws an error and prints a clear message telling you this, depending on the way you are removing bodies.
if(!world.isLocked())
// do clean up code here
You don't need to clean up the variable inside local scope, or clear the userdata inside the body as the body as it will no longer exist in reference (unless you have it stored somewhere else).
So the proper code would be something like this:
if(!world.isLocked()){
for(int i = bodiesToRemove.size - 1; i--){
Body b = bodiesToRemove.get(i);
if(b == null){
bodiesToRemove.removeAt(i);
continue;
}
world.destroyBody(b);
bodiesToRemove.removeAt(i);
}
}
You are also clearing a list during iteration, not a good idea.
EDIT
The only other thing I can think of that is causing this problem is trying to delete a body that is not actually in the worlds body list.
Try the following:
// Ensure world is not locked AND that there is even bodies in the world
if(!world.isLocked() && world.getBodyCount() > 0){
for(int i = bodiesToRemove.size - 1; i--){
Body b = bodiesToRemove.get(i);
if(b == null){
bodiesToRemove.removeAt(i);
continue;
}
world.destroyBody(b);
bodiesToRemove.removeAt(i);
}
}else{
// There is no bodies in the world, so the bodies in our list are just
// random memory hogs, leave them for the GC by clearing the list
bodiesToRemove.clear();
}
EDIT
As stated by the OP, the problem lies with this block of code:
private QueryCallback queryCallback = new QueryCallback() {
#Override
public boolean reportFixture(Fixture fixture) {
if(fixture.getBody() == chest){
//add to remove list
bodiesToRemove.add(chest);
}
if (fixture.testPoint(tmp.x, tmp.y)){
reportFixture = fixture.getBody();
}
if (!fixture.testPoint(tmp.x, tmp.y))
return false;
//assigning bodyB to fixture
jointDef.bodyB = fixture.getBody();
jointDef.target.set(fixture.getBody().getWorldCenter());
//jointDef.target.set(tmp.x, tmp.y);
joint = (MouseJoint) world.createJoint(jointDef);
return false;
}
};
More specifically, this line:
joint = (MouseJoint) world.createJoint(jointDef);
The query callback is triggered during a world step. In Box2D, you can not create or destroy bodies, fixtures, and joints during the world step.
You have to create it outside this method, the simplest way to do it is via a flag. Set a flag to true and store the instance of the fixture somewhere and handle it outside in the game loop using said flag and fixture.
Related
I'm developing an application in Java to help me land my first job as a junior developer. It's a chess game with a GUI that both human players click on from the same machine.
When it's, say, white's turn to move, the application calls white's getMove(Interface interaction) method until a valid MoveAttempt is returned. Here's the getMove(Interface interaction) method of HumanPlayer:
public MoveAttempt getMove(Interface interaction) {
while(!interaction.selectionMade()) {
}
byte pieceFile = interaction.getPenultimateFile();
byte pieceRank = interaction.getPenultimateRank();
byte toFile = interaction.getUltimateFile();
byte toRank = interaction.getUltimateRank();
return new MoveAttempt(pieceFile, pieceRank, toFile, toRank, getIsWhite());
}
penultimateFile, penultimateRank, ultimateFile and ultimateRank are supposed to store the file (column) and rank (row) of the last two chess tiles clicked. This is achieved through this actionPerformed(ActionEvent event) which Interface has because it implements ActionListener
public void actionPerformed(ActionEvent event) {
LocalizedButton button = (LocalizedButton) event.getSource();
if(penultimateFile == -1) {
penultimateFile = button.getFile();
penultimateRank = button.getRank();
}
else {
ultimateFile = button.getFile();
ultimateRank = button.getRank();
}
}
and by calling this method before each call to getMove(Interface interaction)
public void resetClicks() {
penultimateFile = -1;
penultimateRank = -1;
ultimateFile = -1;
ultimateRank = -1;
}
So the idea is that a move attempt is not made until someone has clicked on two chess squares which is why I have a while loop indefinitely calling selectionMade():
public boolean selectionMade() {
return penultimateFile != -1 && penultimateRank != -1 && ultimateFile != -1 && ultimateRank != -1;
}
This didn't work---pieces didn't move---so in an attempt to see what was happening I put this print statement
System.out.println(interaction.getPenultimateFile() + ", " +
interaction.getPenultimateRank() + ", " +
interaction.getUltimateFile() + ", " +
interaction.getUltimateRank());
into the while loop to see what was going on and now it works---pieces move---except I may have encountered times in which it didn't work but I last I tried I couldn't get it to fail.
I don't want to print anything to the console; what should I do in lieu of having this while loop?
Edit: Putting boolean lol = 0 just above the loop and lol = !lol in the loop doesn't allow the code to work. Neither does calling doNothing().
Edit: Here's the source code: https://github.com/JosephBGriffith/Chess
Right now only the pawns work because I have other bugs that I need to fix. En passant works except the opponent piece doesn't get eliminated.
I would invert the control, so that the UI pushes moves to the game, rather than the game trying to pull moves from the UI.
So your game class might have:
class Game {
boolean move(int fromFile, int fromRank, int toFile, int toRank) { ... }
...
}
If the move wasn't legal (e.g. if it was the other player's turn) then move returns false and the move doesn't occur. That is, the internal state of the Game is unchanged.
And your actionPerformed method becomes:
public void actionPerformed(ActionEvent event) {
LocalizedButton button = (LocalizedButton) event.getSource();
if(penultimateFile == -1) {
penultimateFile = button.getFile();
penultimateRank = button.getRank();
}
else {
game.move(penultimateFile, penultimateRank, button.getFile(), button.getRank());
penultimateFile = -1;
}
}
You could use the return value of move to provide some feedback to the user if the move is illegal.
Something to note about this suggestion is that move is executed on the Swing event thread. In theory this is bad practice, although unless your move method is very slow it won't matter.
Read https://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html and consider whether you want to use invokeLater.
I am programming a Memory game in java and I want to make a GameOver-Screen with an image from the sprites folder (I already have the image for it and it is an image with .jpg) after all cards are flipped. So when all cards are flipped, the game is actually over and this GameOver-Screen appears. I have no idea how I can do that. The only thing I know is that it should start with public void gameOver(){. Probably it is possible to make something with "if()" and between the brackets you could say that the condition is that all cards are flipped.
// Memory.java
import ch.aplu.jgamegrid.*; // Imports the Library of Gamegrid, on which this code is based
import ch.aplu.util.*;
public class Memory extends GameGrid implements GGMouseListener {
private boolean isReady = true;
private MemoryCard card1;
private MemoryCard card2;
public Memory() {
super(6, 6, 115, null, null, false);
MemoryCard[] cards = new MemoryCard[36];
for (int i = 0; i < 36; i++) {
if (i < 18)
cards[i] = new MemoryCard(i);
else
cards[i] = new MemoryCard(i - 18);
addActor(cards[i], getRandomEmptyLocation());
cards[i].show(1);
}
addMouseListener(this, GGMouse.lPress);
// Application thread used to flip back cards
doRun();
show();
while (true) {
// Wait until there is something to do
Monitor.putSleep();
delay(1000);
// Flip cards back
card1.show(1);
card2.show(1);
isReady = true;
// Rearm mouse events
setMouseEnabled(true);
}
}
// imports the GGMouse package
public boolean mouseEvent(GGMouse mouse) {
Location location = toLocation(mouse.getX(), mouse.getY());
MemoryCard card = (MemoryCard) getOneActorAt(location);
// Card already flipped->no action
if (card.getIdVisible() == 0)
return true;
// Show picture
card.show(0);
if (isReady) {
isReady = false;
card1 = card;
} else {
card2 = card;
// Pair found, let them visible
if (card1.getId() == card2.getId())
isReady = true;
else {
// Disable mouse events until application thread flipped back cards
setMouseEnabled(false);
Monitor.wakeUp();
}
}
return true;
}
public static void main(String[] args) {
new Memory();
}
}
Each time a card is flipped, check if all cards have been flipped. Use a loop - you already have one in your code. For each index in the for loop... ie each card... check if getIdVisible() is 0.
If the card at index i is still hidden, return null and continue running your program is normal. However, if you get all the way through the loop, that means all cards are visible, which means you can call the gameOver() method, which shows the game over screen.
Does that sound like it fits with your program?
Edit:
To check if all cards are flipped, one thing you can do is to make MemeryCard[] cards (up in the constructor) a global variable. Then you can just call this method every time you reveal two cards:
public boolean areAllCardsFlipped() {
// Start looking through each card
for (int i = 0; i < 36; i++) {
// We found a card that is NOT flipped. Stop here, because clearly all cards are not flipped.
if (cards[i].getIdVisible() != 0) {
return false;
}
}
// We have looked at all cards, and we know that each one is flipped. We can return true.
return true;
}
I'd say call this method in mouseEvent() before you return true for mouseEvent(), and if this returns true you can call the gameOver() method.
As for how to handle rendering the image... I don't know the api you're using for this game, so I can't tell you specifically how to do that. But at least with this code you should be able to trigger that gameOver.
i'm sure I can figure this out on my own but I feel like the way i'm writing it is already too expensive and coupled.
I am writing a simulator with a security robot and a intruder, I have a collision function, where in the 3rd if statement it checks if an intruder (rectangle of width 11) collides with an surveillance camera (arc) and if that happens then I want to activate my function for the security robot to chase the intruder.
<-- Important note: the checkShapeIntersection function is inside a timeLine event so its running constantly -->
private boolean checkShapeIntersection(Shape block) {
//Name to check for intruder and surveillance collision
String Surveillance = "Arc";
boolean collisionDetected = false;
for (Shape static_bloc : nodes) {
if (static_bloc != block) {
Shape intersect = Shape.intersect(block, static_bloc);
if (intersect.getBoundsInLocal().getWidth() != -1) {
collisionDetected = true;
//Checks of intruder collides with an arc (surveillance), the 11th width is only for intruder rectangles
if ( Surveillance.equals(block.getClass().getSimpleName()) && static_bloc.getBoundsInLocal().getWidth() == 11) {
//Activate Chase function
testRobot.chaseIntruder(static_bloc);
detected = true;
}
}
}
}
if (collisionDetected) {
block.setFill(Color.BLUE);
} else {
block.setFill(Color.RED);
}
return detected;
}
And inside my Security Robot Class
public void chaseIntruder(Shape intruder) {
destinationsList.add(new Vector2D(intruder.getLayoutBounds().getMinX(),intruder.getLayoutBounds().getMinY()));
this.updatePosition();
}
You probably want to use multi-threading here, in Java you can implement Runnable or Thread to achieve wanted functionality.
Here are some links with more info:
In a simple to understand explanation, what is Runnable in Java?
https://docs.oracle.com/javase/7/docs/api/java/lang/Runnable.html
https://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html
https://docs.oracle.com/javase/tutorial/essential/concurrency/runthread.html
Hope it helps.
Because of many boolean checks i am a little confused, let me introduce me to my next code.
} else {
//check whether the player starts again. the first time we lose the player
//will definitely get into this loop
if(!startAgain){
// we only do this to set a timer, we don't want to start a new game so we set this
// to false
newgame = false;
startAgain = true;
deadTime = System.nanoTime();
}
deadTimepassed = (System.nanoTime() - deadTime)/1000000;
if((deadTimepassed < 2000) && (newgame = false)){
newGame();
}
}
}
This else statement can be seen as the first time a player gets hit by an enemy missile. To make an explosion-animation. I need te screen to freeze for 2 seconds and then start the game over again. I don't see why the second if statement is never reached. Here are my ontouch methods and my new game method:
#Override
public boolean onTouchEvent(MotionEvent event) {
//What happens when the player touches the screen!
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// Boolean check for when player is playing, if he isn't the chopper does nothing.
// we see that the chopper only hangs still if all these booleans are true!
if ((!player.isPlaying()) && (newgame = true) && (startAgain = true)) {
player.setPlaying(true);
} if(player.isPlaying()) {
player.setUp(true);
startAgain = false;
}
return true;
}
if (event.getAction() == MotionEvent.ACTION_UP) {
player.setUp(false);
return true;
}
return super.onTouchEvent(event);
}
public void newGame(){
newgame = true;
missles.clear();
if(player.getScore()>best) {
best = player.getScore();
SharedPreferences.Editor editor = saveBest.edit();
editor.putInt("new best",best);
editor.commit();
}
player.resetScore();
player.setY(HEIGHT/2);
}
As one can see, the new game just resets the players position, the only thing i want to do is just freeze the screen for 2 seconds before that happens, this is supposed to happen in the second if statement but it never get reached...
Any suggestions?
ps: deadTimepassed is introduced as 'private long' just beneath the public class method.
For freeze your Thread (so, the program will be freeze, also your screen) during 2 seconds you can use Thread.sleep() function:
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
You have to put it inside a try-catch block because it can gives to you an InterruptedException.
I expect it will be helpful for you!
I have two objects of 2D int arrays called:
originalMap and rendedMap which they contain numbers that correspond to the puzzle game map.
From the thread class i obtain the map array from the class call Puzzle.java and modifying the one.
int [][] rendedMap = Puzzle.getRendedMap().getMap();
int [][] originalMap = Puzzle.getMapObj().getMap();
This is my thread loop:
drawPuzzle(canvas, map); takes the canvas and the array and draws a map.
while(running){
if(!holder.getSurface().isValid())
continue;
canvas = holder.lockCanvas();
if(runSolution && movements.size()>0){
executeSolution();
drawPuzzle(canvas, rendedMap);
if(finished){//checks if the execution finished
runSolution=false;
}
if(message.equals("No more actions") ||
message.equals("Out of bounds, try again!")){
System.out.println(message);
drawPuzzle(canvas, originalMap);
}
}else{
drawPuzzle(canvas, originalMap);
}
drawMovements(canvas);
holder.unlockCanvasAndPost(canvas);
}
}
And this is the method that i modify the array:
public void executeSolution(){
numOfPlanets = 2;
for(int x=0;x<rendedMap.length-1; x++){
for(int y=0;y<rendedMap[x].length-1; y++){
//Checks where the robot is and saves its initial position
if(rendedMap[x][y]>=7 && rendedMap[x][y]<=9){
initX=x; initY=y; initial=true;
System.out.println("Initial X: "+initX +" Y: "+initY );
direction = rendedMap[x][rendedMap[x].length-1];
System.out.println("Direction: "+direction );
}
}//end inner for
}//end for
if(movements.size()>0){
for(int i=0; i<movements.size();i++){
if(movements.get(i).getColour().equals("none")){
if(movements.get(i).getMotion().equals("straight")){
if(direction==RIGHT){
if(rendedMap[initX][initY+1]==0){
finished=true;
message = "Out of bounds, try again!";
return;
}else{
rendedMap[initX][initY+1]= rendedMap[initX][initY+1]+6;
rendedMap[initX][initY]=rendedMap[initX][initY]-6;
}
}
}//action if
}//color if
}//movements for loop
if(numOfPlanets > 0 ){
finished = true;
message ="No more actions";
}
}//movements if statement
}
My problem is when i render the renderMap array for some reason originalMap changes as well and when i am trying to draw back the originalMap it doesn't change back.
I have even print them out and checked line by line and they are the same.
Any tips what might cause this?
I spend like days to figure it out.
if(direction==RIGHT) {
if(rendedMap[initX][initY+1]==0) {
finished=true;
message = "Out of bounds, try again!";
return;
} else {
rendedMap[initX][initY+1]= rendedMap[initX][initY+1]+6;
rendedMap[initX][initY]=rendedMap[initX][initY]-6;
}
}
check your rendedMap array is been initialized because this can often lead to contextual errors, especially within the android IDE.
if(!holder.getSurface().isValid())
continue;
canvas = holder.lockCanvas();
if(runSolution && movements.size()>0){
executeSolution();
drawPuzzle(canvas, rendedMap);
if(finished){//checks if the execution finished
runSolution=false;
}
if(message.equals("No more actions") ||
message.equals("Out of bounds, try again!")){
System.out.println(message);
drawPuzzle(canvas, originalMap);
}
}else{
drawPuzzle(canvas, originalMap);
}
drawMovements(canvas);
recall your drawPuzzle beforte surface view is locked down as the else causes it may not be executed in that contextual loop, as I said befor it is a problem that is comon with our organization apps
hope this helps! do hesistate to ask for more assistence
sorry for english I am indian