How should I create a new dog in the cat class? But a new bone in the dog class?
It depends on when you want to create the objects and if you always create one when you create another. I mean, you can put 'new Dog()' anywhere in the Cat class to create one; but, where to put it? -- it really depends.
for example, I want when the cat hits the edge to replace the cat with the dog
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class maredr0 here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class cat extends animals
{
/**
* Act - do whatever the cat wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
int x,y;
if(this!=null&&Greenfoot.isKeyDown("right"))
{
x=this.getX();
y=this.getY();
World world=this.getWorld();
world.addObject(new dog(),x,y);
world.removeObject(this);
}
}
}
you can tell me how to write because I wrote this and did not let me
What? Did you get an error message? did the scenario stop running? or, just kept running, but no dog replacement? (when you hit the "right" arrow key).