This site requires JavaScript, please enable it in your browser!
Greenfoot back
Fibonacci
Fibonacci wrote ...

2017/2/25

setImage() not generating image, please help!

Fibonacci Fibonacci

2017/2/25

#
Brief history, Been working on a project for college and had manage to get the setImage() to work no problems in displaying an explosion if the players car and another collided, i then progressed on to try and create a damage bar that showed increasing damage for every collision detected as opposed to the game consisting of one impact and the game over screen being displayed, as it turned out the code i used somehow competed negated the checkcollision method and long story short i deleted everything back to before is started to create the damage bar, on problem now is the setImage does not seem to display the explosion when collision is detected, everything works fine, it compiles fine with no errors and runs as it should stopping the game when colliding with another car, playing an explosion sound and then displaying a game over screen, however as stated previously the explosion.png no longer displays in game as it used to before i tried to create a damage bar, any help on this matter would be appreciated, the file name is definitely spelled corrected and is located in the images folder of the project. please find below my code for the Car class of which the checkCollision method is part of. import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * This is the class of enemy car that the player has to avoid. * * @author (D. Green) * @version (1.0) */ public class Car extends Actor { /* * Constructor. */ public Car() { } /* * Act - do whatever the Car wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { moveRight(); moveLeft(); moveUp(); moveDown(); checkCollision(); } /* * This method controls the movement of the car to the right. */ public void moveRight() { int x = getX(); int y = getY(); if (Greenfoot.isKeyDown("right")) { setLocation(x +2, y); } } /* * This method controls the players movement left. */ public void moveLeft() { int x = getX(); int y = getY(); if (Greenfoot.isKeyDown("left")) { setLocation(x -2, y); } } /* * This method controls the players movement left. */ public void moveUp() { int x = getX(); int y = getY(); if (Greenfoot.isKeyDown("up")) { setLocation(x , y -2); } } /* * This method controls the players movement left. */ public void moveDown() { int x = getX(); int y = getY(); if (Greenfoot.isKeyDown("down")) { setLocation(x , y +2); } } public void checkCollision() { Actor crash = getOneIntersectingObject(Enemycar.class); if(crash != null) { World myWorld = getWorld(); myWorld.removeObject(this); Greenfoot.playSound("Explosion.wav"); setImage("explosion.png"); Greenfoot.stop(); Gameover gameover = new Gameover(); myWorld.addObject(gameover, myWorld.getWidth() / 2, myWorld.getHeight() / 2); } } }
Super_Hippo Super_Hippo

2017/2/25

#
You are removing the object from the world and then set a new image to it. The object now has indeed this explosion.png image but you don't see the image because the object is no longer in the world.
This is the class of enemy car that the player has to avoid.
This seems to be a little bit misleading because it seems like this car is the one which you can control. I suggest you add a new actor to the world which displays the explosion. As an alternative (because the game ends after the first crash anyways), you can just remove the 'myWorld.removeObject(this);' line. Btw, you can replace all of you moving code and replace it with this:
1
2
3
4
5
6
int dx=0, dy=0;
if (Greenfoot.isKeyDown("right")) dx+=2;
if (Greenfoot.isKeyDown("left")) dx-=2;
if (Greenfoot.isKeyDown("up")) dy-=2;
if (Greenfoot.isKeyDown("down")) dy+=2;
setLocation(getX()+dx, getY()+dy);
You need to login to post a reply.