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

2017/5/8

Error

alinasomcutean alinasomcutean

2017/5/8

#
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.Color; /** * Write a description of class GameOver here. * * @author (your name) * @version (a version number or a date) */ public class GameOver extends Actor { /** * Act - do whatever the GameOver wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public GameOver() { setImage(new GreenfootImage("Game Over", 48, Color.WHITE, Color.BLACK)); } } if(isTouching(Number_2.class)||isTouching(Number_4.class)||isTouching(Number_6.class)||isTouching(Number_8.class)||isTouching(Number_9.class)) { GameOver gameover = new GameOver(); addObject(gameover, getWidth()/2, getHeight()/2); } I get an error: cannot find symbol. Why?
danpost danpost

2017/5/8

#
The 'addObject' is a World class method and cannot be called on an Actor object. You need to call it on the World object the actor is in. The same with 'getWidth' and 'getHeight'. The last line with actual code in it should be:
getWorld().addObject(gameover, getWorld().getWidth()/2, getWorld().getHeight()/2);
Not saying if there is more wrong with the code provided (could just be some copy/paste issues).
alinasomcutean alinasomcutean

2017/5/8

#
if(isTouching(Number_1.class)||isTouching(Number_4.class)||isTouching(Number_6.class)||isTouching(Number_8.class)||isTouching(Number_9.class)||isTouching(Obstacle_1.class)||isTouching(Obstacle_2.class)) { World myWorld = getWorld(); GameOver gameover = new GameOver(); myWorld.addObject(gameover, myWorld.getWidth()/2+50, myWorld.getHeight()/2); myWorld.removeObject(this); } If I have this, which write on the screen "Game Over", how can I remove this text? I made a new class, NewGame: public void act() { // Add your action code here. if(Greenfoot.mouseClicked(this)) { getWorld().removeObjects(getWorld().getObjects(Character.class)); getWorld().addObject( new Character (), 50, 580); } }
danpost danpost

2017/5/8

#
This should be sufficient:
if (Greenfoot.mouseClicked(this))
{
    Greeenfoot.setWorld(new MyWorld()); // adjust World subclass name if necessary
}
alinasomcutean alinasomcutean

2017/5/8

#
Thank you!
You need to login to post a reply.