I need help using finding a way so that if an actor (let's call it "hero") touches an enemy then it will create my GameOver actor in the middle of the screen. I've tried multiple things but I'm very new to java and haven't found a way yet.
import java.util.*;
import greenfoot.*;
import java.awt.Color;
/**
*
*/
public class Beach extends World
{
public static boolean touching;
/**
* Constructor for objects of class MyWorld.
*/
public Beach()
{
super(1000, 600, 1);
addObject (new Turtle(),500,550);
addObject (new Seagull(),10,50);
prepare();
}
/**
* Prepare the world for the start of the program.
* That is: create the initial objects and add them to the world.
*/
private void prepare()
{
Crab crab = new Crab();
addObject(crab,18,266);
Crab crab2 = new Crab();
addObject(crab2,22,359);
Crab crab3 = new Crab();
addObject(crab3,575,427);
Crab crab4 = new Crab();
addObject(crab4,981,321);
crab3.setLocation(978,460);
Crab crab5 = new Crab();
addObject(crab5,25,558);
}
public void act()
{
if (touching==true)
{
addObject(new GameOver(),500,300);
}
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
*
*/
public class Turtle extends Actor
{
public Turtle()
{
setRotation(270);
}
/**
* Act - do whatever the Turtle wants to do. This method is called whenever the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
if (Greenfoot.isKeyDown("up"))
{
setRotation(270);
move(1);
}
if (Greenfoot.isKeyDown("down"))
{
setRotation(90);
move(1);
}
if (Greenfoot.isKeyDown("left"))
{
setRotation(180);
move(1);
}
if (Greenfoot.isKeyDown("right"))
{
setRotation(0);
move(1);
}
if (Greenfoot.isKeyDown("right") & (Greenfoot.isKeyDown("up")))
{
setRotation(315);
}
if (Greenfoot.isKeyDown("right") & (Greenfoot.isKeyDown("down")))
{
setRotation(45);
}
if (Greenfoot.isKeyDown("left") & (Greenfoot.isKeyDown("up")))
{
setRotation(225);
}
if (Greenfoot.isKeyDown("left") & (Greenfoot.isKeyDown("down")))
{
setRotation(135);
}
if (isAtEdge()==true)
{
turn(180);
move(15);
}
if (isTouching(Seagull.class) || isTouching(Crab.class))
{
Beach.getInstance.touching=true;
}
}
}
getWorld().addObject(new GameOver(),500,300);
Greenfoot.stop();
public boolean touching=false;
if (isTouching(Seagull.class) || isTouching(Crab.class))
{
getWorld().touching=true;
}Greenfoot.stop();
getWorld().addObject(new GameOver(),500,300);
Greenfoot.stop();
Beach beach=(Beach)getWorld();
beach.touching=true;
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Fish here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Fish extends Actor
{
/**
* Act - do whatever the Fish wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
checkKeypress();
move(2);
if(isTouching(Hand.class))
{
World myWorld2 = getWorld();
World myWorld = getWorld();
GameOver gameover = new GameOver();
myWorld.addObject(gameover, myWorld.getWidth()/2, myWorld.getHeight()/2);
myWorld2.addObject(gameover, myWorld2.getWidth()/2, myWorld2.getHeight()/2);
myWorld.removeObject(this);
Greenfoot.stop();
}
}
/**
* Checks to see if command is given.
*/
public void checkKeypress()
{
if (Greenfoot.isKeyDown("down"))
{
turn(4);
}
if (Greenfoot.isKeyDown("up"))
{
turn(-4);
}
}
}
World world = getWorld(); world.addObject(new GameOver(), world.getWidth()/2, world.getHeight()/2); world.removeObject(this); Greenfoot.stop();
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class MyWorld2 here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class MyWorld2 extends World
{
private int timer;
/**
* Constructor for objects of class MyWorld2.
*
*/
public MyWorld2()
{
super(600, 400, 1);
prepare();
}
/**
* Type description here
*/
private void prepare()
{
Fish fish = new Fish();
addObject(fish, 100, 400);
Hand hand = new Hand();
for(int i = 0; i < 6; i++)
{
int x = Greenfoot.getRandomNumber(getWidth()/2);
int y = Greenfoot.getRandomNumber(getHeight()/2);
addObject(new Hand(), x, y);
}
}
public void setTimer(int value)
{
timer=value;
}
public void act()
{
int aps = 28;
if (++timer%aps == 0)showText("Score: "+(timer/aps), 125, 40);
}
}import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Hand here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Hand extends Actor
{
/**
*
*/
public Hand()
{
GreenfootImage image = getImage();
image.scale(50, 50);
setImage(image);
}
/**
* Act - do whatever the Hand wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
turnAtEdge();
randomTurn();
move(1);
lookForFish();
}
/**
* Describe method here
*/
public void randomTurn()
{
if (Greenfoot.getRandomNumber(100)>90)
{
turn(Greenfoot.getRandomNumber(101)-50);
}
}
/**
* Describe method here
*/
public void turnAtEdge()
{
if(isAtEdge())
{
turn(20);
}
}
/**
* Describe Method Here
*/
public void lookForFish()
{
if (isTouching(Fish.class))
{
removeTouching(Fish.class);
Greenfoot.stop();
}
}
}