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

2019/5/19

I want to add an object when an actor is in certain position but the world doesn't add it

1
2
Tasya16 Tasya16

2019/5/19

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
/**
 * Write a description of class Dialog1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Dialog1 extends Actor
{
    /**
     * Act - do whatever the Dialog1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    { World duniaRatu = new MyWorld();
       List ratu = getWorld().getObjects(Ratu.class);
        Actor player = (Actor)ratu.get(0);
    int ratuY = player.getY();
    if(ratuY== 500){duniaRatu.addObject(new Dialog1(),100,100);}
    }    
}
danpost danpost

2019/5/19

#
Tasya16 wrote...
I want to add an object when an actor is in certain position but the world doesn't add it
Which world does not add it -- the current one or the new one you created on line 16 which is not active? Remove the command line from line 16 and change duniaRatu on line 20 to getWorld().
danpost danpost

2019/5/19

#
I am concerned about having a new Dialog1 object added to the world when the actor doing so is one already in the world. Are you to have multiple Dialog1 actors in the world at the same time?
Tasya16 Tasya16

2019/5/20

#
danpost wrote...
Tasya16 wrote...
I want to add an object when an actor is in certain position but the world doesn't add it
Which world does not add it -- the current one or the new one you created on line 16 which is not active? Remove the command line from line 16 and change duniaRatu on line 20 to getWorld().
I want to add it to an active world
Tasya16 Tasya16

2019/5/20

#
So there is an actor Ratu flies to the world when it arrives in 500 (getY() == 500) I want it to stop then come dialogue texts on top of her head
Super_Hippo Super_Hippo

2019/5/20

#
You have to understand how act methods work. In each act cycle, Greenfoot calls the act method of the active world and the act method of every actor which is in the world. As a start, put this into the act method of the Ratu class:
if (getY()==500) addObject(new Dialog1(),100,100);
Now you have to make sure to only add one Dialog1 object and not hundreds and thousands if the player stays at y=500.
Tasya16 Tasya16

2019/5/20

#
Super_Hippo wrote...
You have to understand how act methods work. In each act cycle, Greenfoot calls the act method of the active world and the act method of every actor which is in the world. As a start, put this into the act method of the Ratu class:
if (getY()==500) addObject(new Dialog1(),100,100);
Now you have to make sure to only add one Dialog1 object and not hundreds and thousands if the player stays at y=500.
that's right, how can i make it to add only once?
Super_Hippo Super_Hippo

2019/5/20

#
You could have a boolean "dialogAdded" or something like that. Set it to true when you add the dialog. Only add it when it was false. If there will be a lot of dialogs, the best way might be to have an int to save which dialog was the last one.
danpost danpost

2019/5/20

#
Super_Hippo wrote...
You could have a boolean "dialogAdded" or something like that.
Better would be just to ask if one is already in the world:
if (getY() == 500 && getWorld().getObjects(Dialog1.class).isEmpty()) getWorld().addObject(new Dialog1(), 100, 100);
Another way is to keep a reference to the Dialog1 object. That way, only one is ever created:
// in Ratu class
private Dialog1 dialog1 = new Dialog1();

// in act of Ratu
if (getY() == 500) getWorld().addObject(dialog1, 100, 100);
Tasya16 Tasya16

2019/5/20

#
if (getY() == 500 && getWorld().getObjects(Dialog1.class).isEmpty()) getWorld().addObject(new Dialog1(), 100, 100);
i tried this one but still doesn't appear on the world
Tasya16 Tasya16

2019/5/20

#
public class Ratu extends Actor
{
  private int frame=0;
    private GreenfootImage gerak1 = new GreenfootImage("ratu1.png");
    private GreenfootImage gerak2 = new GreenfootImage("ratu2.png");
    public void act() 
    {
if(frame==200) { getWorld().addObject(new Dialog1(),570,300); } frame++;
      setLocation(getX(),getY()+2);
      if(getY()== 500){setLocation(getX(),getY()-2); 
          
    }   
}
I end up using frames,it works now
Tasya16 Tasya16

2019/5/20

#
but thank you so much danpost and Super_Hippo for helping
danpost danpost

2019/5/20

#
Now that you show the contents of Ratu, you could just do this:
public class Ratu extends Actor
{
    private GreenfootImage[] gerak = { new GreenfootImage("ratu1.png"), new GreenfootImage("ratu2.png") };
    
    public void act()
    {
        if (getY() < 500)
        {
            setLocation(getX(), getY()+2);
            setImage(gerak[getY()%20]);
            if (getY() == 500) getWorld().addObject(new Dialog1(), 570, 300);
        }
    }
}
Tasya16 Tasya16

2019/5/20

#
actually I want to use the frame so i can change the image of dialogue because there are more dialogue to come
Tasya16 Tasya16

2019/5/20

#
public class Ratu extends Actor
{
  private int frame=0;
    private GreenfootImage gerak1 = new GreenfootImage("ratu1.png");
    private GreenfootImage gerak2 = new GreenfootImage("ratu2.png");
    public void act() 
    {
dialog();
      setLocation(getX(),getY()+2);
      if(getY()== 500){setLocation(getX(),getY()-2); 
    }   
}
public void dialog()
{ Actor dialog1 = new Dialog1();
    if(frame==200) { getWorld().addObject(new Dialog1(),570,300); frame=200;} 
else if(frame==201) { getWorld().removeObject(dialog1);}
frame++;
}
} 
but dialog1 doesn't disappear from the world
There are more replies on the next page.
1
2