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

2021/6/19

Add a object on scenario doesn't work

Sajji25 Sajji25

2021/6/19

#
I don't know what is wrong with the code anyone knows? import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class R1 extends World { Intel Intel = new Intel(); AMD AMD = new AMD(); Incorrecta Incorrecto = new Incorrecta (); reiniciar reiniciar = new reiniciar (); Correcta correcto = new Correcta (); /** * Constructor for objects of class R1. * */ public R1() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(1000, 750, 1); addObject(new Intel(),179, 653); addObject(new AMD(),807, 653); } public void act () { if (Greenfoot.mouseClicked(Intel)) { addObject(Incorrecto,460,338); addObject(reiniciar,448,164); } if (Greenfoot.mouseClicked(AMD)) { addObject(correcto,460,97); addObject(reiniciar,448,164); } if (Greenfoot.isKeyDown("G")) { Greenfoot.setWorld(new Pareo()); } } }
Gbasire Gbasire

2021/6/19

#
your object has already been added to the world because it's reference elsewhere in your class. If you want your object to be added multiple times, do this instead :
public void act ()
    {
        if (Greenfoot.mouseClicked(Intel))
     {
        Incorrecta Incorrecto = new Incorrecta();
        addObject(Incorrecto,460,338);
        reiniciar reiniciar = new reiniciar();
        addObject(reiniciar,448,164);
     }
      if (Greenfoot.mouseClicked(AMD))
     {
        Correcta correcto = new Correcta();
        addObject(correcto,460,97);
        reiniciar reiniciar = new reiniciar();
        addObject(reiniciar,448,164);
     }
Super_Hippo Super_Hippo

2021/6/19

#
In your constructor, you add “new” instances of the Intel and AMD classes. There are not the ones in your variables, so the mouseClicked method will always return false. As a side note, you shouldn’t use the very same names for classes and objects to avoid confusion.
You need to login to post a reply.