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

2019/6/21

How to make the object rotate with another

1
2
3
4
Super_Hippo Super_Hippo

2019/6/22

#
You set the key for each player with the setShootKey method.
MisterUnknown MisterUnknown

2019/6/22

#
Okay, but the setShootKey(String Key) is an illegal start of expression
MisterUnknown MisterUnknown

2019/6/22

#
So, I am currently moving all the stuff into my player-class. Where do I have to put this again: ? 1 2 3 4 player.pipe = new Pipe(); player.pipe.setImage("Rohr1.PNG"); player.pipe.getImage().scale(50, 50); player.pipe.setShootKey("o");
Super_Hippo Super_Hippo

2019/6/22

#
Illegal start of expression? Make sure you don't put the method into another method. In the world before adding the player to the world.
MisterUnknown MisterUnknown

2019/6/22

#
Alright, but obviously it doesnt work if
 player.pipe.setShootKey("o");
player2.pipe.setShootKey("e");
are being defined in battleworld (giving me this error: cannot find symbol method setShootKey(java.lang.String);) while
public void setShootKey(String key)
        {
            shootKey = key;
        }
is in the player-class
Super_Hippo Super_Hippo

2019/6/22

#
The setShootKey method is in the Pipe class. I suggested to move it to the Player class, yes. In this case, use:
player.setShootKey("o");
MisterUnknown MisterUnknown

2019/6/22

#
Now, the pipes dont appear at all and the bullet continues to shoot perpentucular (senkrecht)
Super_Hippo Super_Hippo

2019/6/23

#
Did you override the addedToWorld method?
danpost danpost

2019/6/23

#
You can make things a lot easier by using an inner class for the pipes (putting the Pipe class within the Player class). That way, when a player creates a pipe, it already "belongs" to that player. Each pipe will automatically "know" which player created it. Below is a skeleton of the idea:
public class Player extends Actor
{
    int playerNum;
    Pipe pipe = new Pipe();
    ...
    
    public Player(int num)
    {
        playerNum = num;
        if (num == 0)
        {
            ...
        }
        else
        {
            ...
        }
        ...
    }
    
    protected void addedToWorld(World world)
    {
        world.addObject(pipe, getX(), getY());
    }
    
    ...
    
    public class Pipe extends Actor
    {
        String shootKey;
        
        public Pipe()
        {
            shootKey = this.Player.playerNum == 0 ? "o", "e";
            GreenfootImage img = new GreenfootImage("Rohr"+(this.Player.playerNum+1)+".PNG");
            img.scale(25, 25);
            setImage(img);
        }
        
        public void act()
        {
            if (this.Player.getWorld() == null) { getWorld().removeObject(this); return; }
            ...
        }
    }
}
Notice that in the inner class (Pipe), this.<< outer class name >> (this.Player) refers to the outer class object that created the inner class object. The addedToWorld method in the Player class and the first line in the Pipe act method work together to keep the pipe with the player whether in a world or not. Also, notice that the constructors use the player number to appropriately set its states. This relieves the World class of that obligation and only the one value need be passed (that is, the world need only create and add the Player objects into itself).
MisterUnknown MisterUnknown

2019/6/23

#
public Player(int num) { playerNum = num; if (num == 0) { ... } else { ... } ... } What do I have to put inside this? The image of an particular player? & how am I giving playerNum 0 a specific key without any error
danpost danpost

2019/6/23

#
MisterUnknown wrote...
<< Code Omitted >> What do I have to put inside this? The image of an particular player? & how am I giving playerNum 0 a specific key without any error
Yes, both the image and the keys can be set there.
MisterUnknown MisterUnknown

2019/6/23

#
Could you explain me this line further more?
 shootKey = this.Player.playerNum == 0 ? "o", "e";
Super_Hippo Super_Hippo

2019/6/23

#
It should be a ":" instead of the ",".
if (A)
{
    B;
}
else
{
    C;
}
is the same as
A ? B : C;
So what the line does is it sets the shootKey to either "o" or "e" based on the playerNum of the Player object which created this pipe
MisterUnknown MisterUnknown

2019/6/23

#
I understood that part, but I lack in the profession of making it happen
danpost danpost

2019/6/23

#
The only decision to be made in the Pipe class constructor was which key to shoot with; so, I used the ternary operator, A ? B; : C;, there (there was no decision to be made for the image; just used the value itself). To construct a Player object, there are multiple things to set (the four keys for moving; the image; maybe more); hence, the standard if-else structure is used there. All you need to do is set the appropriate keys and images within the two blocks of that structure.
There are more replies on the next page.
1
2
3
4