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

2019/4/26

how can I add selected actor in another world?

1
2
Tasya16 Tasya16

2019/4/26

#
I make this game where the player can choose between 2 actors. how can I add to the arena when it is clicked?
Tasya16 Tasya16

2019/4/26

#
public class Karakter1 extends Actor
{
  MainLoc arena = (MainLoc) getWorld();
    public void act() 
    {
     if(Greenfoot.mouseClicked(this)){addedToWorld();}
    }    
    public void addedToWorld()
    { MainLoc arena = (MainLoc) getWorld();
        Greenfoot.setWorld(new MainLoc());
       arena.addObject(new Vera(),150,450);
    }
}
Tasya16 Tasya16

2019/4/26

#
I made that but nothing happened, someone please help me
danpost danpost

2019/4/26

#
Tasya16 wrote...
I made that but nothing happened, someone please help me
You are adding Vera to the current world -- not the new one.
Tasya16 Tasya16

2019/4/26

#
so i want when Karakter1 is clicked the world changes and vera is added to the new world
Tasya16 Tasya16

2019/4/26

#
danpost wrote...
Tasya16 wrote...
I made that but nothing happened, someone please help me
You are adding Vera to the current world -- not the new one.
but I wrote setWorld ,the world changed but vera is not in that world
danpost danpost

2019/4/26

#
Tasya16 wrote...
so i want when Karakter1 is clicked the world changes and vera is added to the new world
When clicked, create the new world first, then add Vera to it and finally set the world active.
danpost danpost

2019/4/26

#
Tasya16 wrote...
but I wrote setWorld ,the world changed but vera is not in that world
Yes. Vera was not in the new world because you added her to the old world which is no longer active.
Tasya16 Tasya16

2019/4/26

#
but I want Vera is added only when user chooses her
Tasya16 Tasya16

2019/4/26

#
what is the logic behind that? I cant make the code check if the world changes because there is another character
danpost danpost

2019/4/26

#
Tasya16 wrote...
what is the logic behind that? I cant make the code check if the world changes because there is another character
I think you are confused -- let's start over. This is what your code given above does. When a click is detected (line 6), you execute your addedToWorld method. It gets a reference to the current world (line 9), then you set a new world active (line 10) , and finally add a new Vera object to the old world (line 11). The reference acquired at line 9 does not change just because you create a new world, or because you have a new active world. In fact, I see no need to even get a reference to the world which you are about to leave. What you need is a reference to the newly created world which you set active. Remove line 9 (except the first character) and break line 10 into two separate lines -- one to create the new world (and assign it to a local variable) and one to set that world active. Then, have line 11 add Vera to the new world.
Tasya16 Tasya16

2019/4/26

#
public class Karakter1 extends Actor
{
  MainLoc arena = (MainLoc) getWorld();
    public void act() 
    {
     if(Greenfoot.mouseClicked(this)){addedToWorld();}
    }    
    public void addedToWorld()
    { MainLoc arena = (MainLoc) getWorld();
        Greenfoot.setWorld(new MainLoc());}
      { arena.addObject(new Vera(),150,450);
    }
}
Tasya16 Tasya16

2019/4/26

#
danpost wrote...
Tasya16 wrote...
what is the logic behind that? I cant make the code check if the world changes because there is another character
I think you are confused -- let's start over. This is what your code given above does. When a click is detected (line 6), you execute your addedToWorld method. It gets a reference to the current world (line 9), then you set a new world active (line 10) , and finally add a new Vera object to the old world (line 11). The reference acquired at line 9 does not change just because you create a new world, or because you have a new active world. In fact, I see no need to even get a reference to the world which you are about to leave. What you need is a reference to the newly created world which you set active. Remove line 9 (except the first character) and break line 10 into two separate lines -- one to create the new world (and assign it to a local variable) and one to set that world active. Then, have line 11 add Vera to the new world.
number 9 is the new world
danpost danpost

2019/4/26

#
Tasya16 wrote...
number 9 is the new world
No, it is not. It is the world that the clickable Karakter1 object is in. 'new MainLoc()' on line 10 creates the new world.
Tasya16 Tasya16

2019/4/27

#
How to set the world active ?
There are more replies on the next page.
1
2