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

2019/1/27

Subclasses

Adi18 Adi18

2019/1/27

#
world          ... Actor          animals               cat               dog          objects                ball                bone How should I create a new dog in the cat class? But a new bone in the dog class?
danpost danpost

2019/1/27

#
Adi18 wrote...
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.
Adi18 Adi18

2019/1/27

#
for example, when the cat hits the edge to replace the cat with the dog
danpost danpost

2019/1/27

#
Adi18 wrote...
for example, when the cat hits the edge to replace the cat with the dog
So, if the cat hits the edge, first get the world; then use the world to add a new dog at the cat's location and remove the cat (this).
Adi18 Adi18

2019/1/27

#
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); } } }
Adi18 Adi18

2019/1/27

#
you can tell me how to write because I wrote this and did not let me
danpost danpost

2019/1/27

#
Adi18 wrote...
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).
Adi18 Adi18

2019/1/27

#
apparently going, it is strange, but when replacing the cat with the dog the dog is not at the former coordinates of the cat
danpost danpost

2019/1/27

#
Adi18 wrote...
apparently going, it is strange, but when replacing the cat with the dog the dog is not at the former coordinates of the cat
Please show the dog class code.
Adi18 Adi18

2019/1/27

#
I just fixed it, thanks for help
You need to login to post a reply.