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
MisterUnknown MisterUnknown

2019/6/23

#
Oh, I was talking about the shootKey = this.Player.playerNum line
danpost danpost

2019/6/23

#
MisterUnknown wrote...
Oh, I was talking about the shootKey = this.Player.playerNum line
So -- what is your question?
MisterUnknown MisterUnknown

2019/6/23

#
How to make this line work in my sourcecode: shootKey = this.Player.playerNum == 0 ? "o", "e";
danpost danpost

2019/6/23

#
MisterUnknown wrote...
How to make this line work in my sourcecode: shootKey = this.Player.playerNum == 0 ? "o", "e";
Everything I gave is already where it belongs.
MisterUnknown MisterUnknown

2019/6/24

#
Right now there are erris with having "public class Pipe extends Actor" in the player.class and this.PlayerNum, due to it not finding the right symbol
danpost danpost

2019/6/24

#
MisterUnknown wrote...
Right now there are erris with having "public class Pipe extends Actor" in the player.class and this.PlayerNum, due to it not finding the right symbol
PlayerNum should be playerNum with p intead of P. And it may be that you put the Pipe class after the Player class and not in the Player class. Show Player class codes.
MisterUnknown MisterUnknown

2019/6/24

#
public void doBulletHit(int playerNum)
{
    World myWorld = getWorld();
        BattleWorld battleWorld = (BattleWorld)myWorld;
        myPlayerNum = playerNum;
    if  (isTouching(Bullet.class) && !(didShoot))
        {
            
            Bar bar = battleWorld.getBar(playerNum);
            
            Actor bull = (Actor) getOneIntersectingObject(Bullet.class);
            bleed((Greenfoot.getRandomNumber(3)+2), bull.getX(), bull.getY());
            getWorld().removeObject(bull);
           
            
            bar.loseHealth();
            if(bar.health <=0)
            {
                GameOver gameover = new GameOver(myPlayerNum);
                myWorld.addObject(gameover, myWorld.getWidth()/2, myWorld.getHeight()/2);
                myWorld.removeObject(this);
                if (++counter == 5) Greenfoot.setWorld(new TitleScreen());
            }
            
        }
        //else if ((playerNum == 0)&& isTouching(Bullet.class) && !(didShoot))
        //{
          
            
         //   Bar bar2 = battleWorld.getBar();
         //   Actor bull = (Actor) getOneIntersectingObject(Bullet.class);
         //   bleed((Greenfoot.getRandomNumber(3)+2), bull.getX(), bull.getY());
         //   getWorld().removeObject(bull);
           
           // bar2.loseHealth();
           // if(bar2.health <=0)
            //{
                //GameOver gameover = new GameOver(myPlayerNum);
                //myWorld.addObject(gameover, myWorld.getWidth()/2, myWorld.getHeight()/2);
                //myWorld.removeObject(this);
                //if (++counter == 100) Greenfoot.setWorld(new TitleScreen());
           // }  
        }
           public void shootStuff()
   {
       
        if(Greenfoot.isKeyDown(shootKey) && shotCoolDown == 0) {
            getWorld().addObject(new Bullet(getRotation()), getX(), getY());
            shotCoolDown = 20;
            didShoot = true;
        }
        if(shotCoolDown > 0)
           shotCoolDown -= 1;
    }
    public void setShootKey(String key)
        {
            shootKey = key;
        }
           protected void addedToWorld(World world)
{
    world.addObject(pipe, getX(), getY());
}
}
public class Pipe extends Actor 
{
    String shootKey;
    
    public Pipe()
    {
        
        shootKey = this.Player.playerNum == 0 ? "e": "o";
        GreenfootImage img = new GreenfootImage("Rohr"+(this.Player.playerNum+1)+".PNG");
        img.scale(25, 25);
        setImage(img);
    
}
}
Super_Hippo Super_Hippo

2019/6/25

#
Move line 63 to line 78. Before posting code the next time, press ctrl+shift+i in the editor. This makes the code much easier to read.
danpost danpost

2019/6/25

#
Move the shootStuff method down along with the shotCoolDown field into the Pipe class. I would modiffy the method to this:
public void shootStuff()
{
    if (shotCoolDown > 0) shotCoolDown--;
    else if (Greenfoot.isKeyDown(shootKey))
    {
        getWorld().addObject(new Bullet(getRotation()), getX(), getY());
        shotCoolDown = 20;
    }
}
Plus add an act method to call it:
public void act()
{
    if (this.Player.getWorld() == null) { getWorld().removeObject(this); return; }
    shootStuff();
}
You can now remove lines 55 thru 59 above, plus you can remove the not needed boolean didShoot field.
MisterUnknown MisterUnknown

2019/6/25

#
Thanks for the hard work, but now in
public void act()
{
    if (this.Player.getWorld() == null) { getWorld().removeObject(this); return; }
    shootStuff();
    
}
and
 public Pipe()
    {
        
        shootKey = this.Player.playerNum == 0 ? "e": "o";
        GreenfootImage img = new GreenfootImage("Rohr"+(this.Player.playerNum+1)+".PNG");
        img.scale(25, 25);
        setImage(img);
    
}
Player is still and unknown variable It tells me that act() is already defined and my mainworld tells me that Pipe cannot be converted to Player.Pipe
player.pipe = new Pipe();
cannot find symbol - method setShootKey(java.lang.String)
player.setShootKey("o");
danpost danpost

2019/6/25

#
MisterUnknown wrote...
in << Codes Omitted >> Player is still and unknown variable
Show entire Player class codes (do press Control-Shift-I before copying).
It tells me that act() is already defined
The latest was to be a replacement.
my mainworld tells me that Pipe cannot be converted to Player.Pipe << Code Omitted >>
Remove all references to any pipe in mainWorld.
cannot find symbol - method setShootKey(java.lang.String) << Code Omitted
Remove that line also.
MisterUnknown MisterUnknown

2019/6/25

#
Alright, here it is
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot und MouseInfo)

/**
 * Ergänzen Sie hier eine Beschreibung für die Klasse BattleWorld.
 * 
 * @author (Ihr Name) 
 * @version (eine Versionsnummer oder ein Datum)
 */
public class BattleWorld extends World
{
    Player player = new Player(0, "tank.png", "left", "right", "up", "down");
    Player player2 = new Player(1, "tank2.png", "a", "d", "w", "s");
    Bar bar = new Bar();
    Bar bar2 = new Bar();
    public static int p1X = 100;
    public static int p1Y = 300;
    public static int p2X = 700;
    public static int p2Y = 300;

    /**
     * Konstruktor für Objekte der Klasse BattleWorld
     * 
     */
    public BattleWorld()
    {    
        // Erstellt eine neue Welt mit 600x400 Zellen und einer Zell-Größe von 1x1 Pixeln.
        super(800, 600, 1);
        player.pipe = new Pipe();
        player.pipe.setImage("Rohr1.PNG");
        player.pipe.getImage().scale(50, 50);
        player.setShootKey("o");
        player2.pipe = new Pipe();
        player2.pipe.setImage("Rohr2.PNG");
        player2.pipe.getImage().scale(50, 50);
        player2.setShootKey("e");
        addObject(player, 100, 300);
        addObject(player2, 700, 300);

        addObject(bar, 200, 40);
        addObject(bar2, 600, 40);

        //addObject(playbutton, 300, 200);
    }

    public Bar getBar(int playerNum)
    {
        if (playerNum == 0)
        {
            return bar;
        }
        else
        {
            return bar2;
        }

    }

}
danpost danpost

2019/6/25

#
MisterUnknown wrote...
Alright, here it is << Code Omitted >>
That is not the Player class. However, a lot needs done here also. Change lines 11 and 12 to the following:
Player player = new Player(0);
Player player2 = new Player(1);
and remove lines 15 thru 18 and lines 28 thru 35. The players' attributes will be taken care of in the Player class.
MisterUnknown MisterUnknown

2019/6/25

#
Oh, excuse me. Here is the actual player class code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot und MouseInfo)
import java.lang.String;
import java.lang.Math;

/**
 * Ergänzen Sie hier eine Beschreibung für die Klasse Player.
 * 
 * @author (Ihr Name) 
 * @version (eine Versionsnummer oder ein Datum)
 */
public class Player extends Actor
{
    String leftKey, rightKey, upKey, downKey;
    GreenfootImage myImage;
    public int myPlayerNum = 0;
    int playerNum;
    boolean didShoot = false;
    public double xVel = 0.0;
    public double yVel = 0.0;
    public double terminalSpeed = 5.0;
    public int counter;
    boolean playerContact = false;
    double currentForce = 0;
    public Pipe pipe;
    //Pipe pipe = new Pipe();
    int shotCoolDown = 0;
    String shootKey = "o";
    
    public Player(int num, String image, String left, String right, String up, String down)
    {
        myImage = new GreenfootImage(image);
        myPlayerNum = playerNum;
        playerNum = num;
        leftKey = left;
        rightKey = right;
        upKey = up;
        downKey = down;
        didShoot = false;
        myImage.scale(50, 50);
        setImage(myImage);
        
        if (num == 0)
        {
             myImage = new GreenfootImage("tank.png");
            rightKey = "right";
            leftKey = "left";
            upKey = "up";
            downKey = "down";
        }
        else
        {
            myImage = new GreenfootImage("tank2.png");
            rightKey = "d";
            leftKey = "a";
            upKey = "w";
            downKey = "s";
        }
    }
    /**
     * Act - tut, was auch immer Player tun will. Diese Methode wird aufgerufen, 
     * sobald der 'Act' oder 'Run' Button in der Umgebung angeklickt werden. 
     */
    public void act() 
    {
        movementEngine();
        turnTowardsEnemy();
        wallBounce();
        calculateForce();
        doBulletHit(myPlayerNum);
        playerCollider();
        mutualmovement();
        shootStuff();
        if (this.Player.getWorld() == null) { getWorld().removeObject(this); return;}
               }    
    public void mutualmovement()
    {
       
        Pipe actor = (Pipe) getOneIntersectingObject(Pipe.class);
        if (actor != null) {
            actor.setLocation(this.getX(),this.getY());
        }
        
    }
    public void turnTowardsEnemy()
    {
        if(myPlayerNum == 1) {
         turnTowards(BattleWorld.p2X, BattleWorld.p2Y);
         }
         else {
             turnTowards(BattleWorld.p1X, BattleWorld.p1Y);
         }
    }
    
    public void movementEngine()
    {
        //int angle = getRotation();
        
        setLocation((int) (getX() + xVel), (int) (getY() + yVel));
        if(Greenfoot.isKeyDown(rightKey))
            xVel += 0.5;
        if(Greenfoot.isKeyDown(leftKey))
            xVel -= 0.5;
        if(Greenfoot.isKeyDown(upKey))
            yVel -= 0.5;
        if(Greenfoot.isKeyDown(downKey))
            yVel += 0.5;
        friction();
        terminalVelocity();
    }
    
     public void friction()
    {
        if(xVel >= 0)
            xVel -= 0.125;
        if(xVel <= 0)
            xVel += 0.125;
        if(yVel >= 0)
            yVel -= 0.125;
        if(yVel <= 0)
            yVel += 0.125;
    }
    
    public void terminalVelocity()
    {
        if(xVel >= terminalSpeed)
            xVel = terminalSpeed;
        if(xVel <= -terminalSpeed)
            xVel = -terminalSpeed;
        if(yVel >= terminalSpeed)
            yVel = terminalSpeed;
        if(yVel <= -terminalSpeed)
            yVel = -terminalSpeed;
    }
    public void bleed(int particles, int x, int y)
    {
        for(int a = 0; a < particles; a++) {
            bleed(x, y);
        }
    }
    public void bleed(int x, int y)
    {
        getWorld().addObject(new Particle("blood", ((Greenfoot.getRandomNumber(21)-10)/4.0), ((Greenfoot.getRandomNumber(21)-10)/5.0)), x, y);
    }
        public boolean atWorldLeft()
    {
        if(getX() < 20)
            return true;
        else return false;
    }
    
    public boolean atWorldRight()
    {
        if(getX() > getWorld().getWidth() - 20)
            return true;
        else return false;
    }
    public boolean atWorldTop()
    {
        if(getY() < 20)
            return true;
        else return false;
    }
    
    public boolean atWorldBottom()
    {
        if(getY() > getWorld().getHeight() - 20)
            return true;
        else return false;
    }
    
    
    public void bleed()
    {
        getWorld().addObject(new Particle("blood", ((Greenfoot.getRandomNumber(21)-10)/4.0), ((Greenfoot.getRandomNumber(21)-10)/5.0)), getX(), getY());
    }
    public void wallBounce()
    {
        if(atWorldRight() && xVel > 0) {
            xVel *= -1.1;
            setLocation((int) (getX() + xVel), (int) (getY() + yVel));
        }
        if(atWorldLeft() && xVel < 0) {
            xVel *= -1.1;
            setLocation((int) (getX() + xVel), (int) (getY() + yVel));
        }
        if(atWorldTop() && yVel < 0) {
            yVel *= -1.1;
            setLocation((int) (getX() + xVel), (int) (getY() + yVel));
        }
        if(atWorldBottom() && yVel > 0) {
            yVel *= -1.1;
            setLocation((int) (getX() + xVel), (int) (getY() + yVel));
        }
    }
    public void calculateForce()
    {
        currentForce = Math.hypot(Math.abs(xVel), Math.abs(yVel));
        
    }
    public void playerCollider()
    {
        if (getWorld() == null) return;
        if(isTouching(Player.class)) {
            playerContact = true;
            Actor oPlayer = (Actor) getOneIntersectingObject(Player.class);
            int oPlayerX = oPlayer.getX();
            int oPlayerY = oPlayer.getY();
            double k = 1.5 * currentForce;
            int angle = getRotation();
            if (oPlayerX > getX()) {
                 if(oPlayerY < getY()) {
                    
                    xVel = (-1) * (k) * Math.cos(Math.toRadians(360-angle));
                    yVel = (1) * (k) * Math.sin(Math.toRadians(360-angle));
                }
                else {
                    
                    xVel = (1) * (k) * Math.cos(Math.toRadians(angle-180));
                    yVel = (1) * (k) * Math.sin(Math.toRadians(angle-180));
                }
            }
            else {
                if(oPlayerY < getY()) {
                    
                    xVel = (1) * (k) * Math.cos(Math.toRadians(180-angle));
                    yVel = (-1) * (k) * Math.sin(Math.toRadians(180-angle));
                }
                else {
                    
                    xVel = (-1) * (k) * Math.cos(Math.toRadians(angle));
                    yVel = (-1) * (k) * Math.sin(Math.toRadians(angle));
                }
            }
            
        }
        else {
            playerContact = false;
        }
        
}
public void doBulletHit(int playerNum)
{
    World myWorld = getWorld();
        BattleWorld battleWorld = (BattleWorld)myWorld;
        myPlayerNum = playerNum;
    if  (isTouching(Bullet.class) && !(didShoot))
        {
            
            Bar bar = battleWorld.getBar(playerNum);
            
            Actor bull = (Actor) getOneIntersectingObject(Bullet.class);
            bleed((Greenfoot.getRandomNumber(3)+2), bull.getX(), bull.getY());
            getWorld().removeObject(bull);
           
            
            bar.loseHealth();
            if(bar.health <=0)
            {
                GameOver gameover = new GameOver(myPlayerNum);
                myWorld.addObject(gameover, myWorld.getWidth()/2, myWorld.getHeight()/2);
                myWorld.removeObject(this);
                if (++counter == 5) Greenfoot.setWorld(new TitleScreen());
            }
            
        }
        
        }
   
    //public void setShootKey(String key)
        //{
            //shootKey = key;
        //}
           protected void addedToWorld(World world)
{
    world.addObject(pipe, getX(), getY());
}

public class Pipe extends Actor 
{
    String shootKey;
    
    public Pipe()
    {
        
        shootKey = this.Player.playerNum == 0 ? "e": "o";
        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; }
    shootStuff();
    
}
public void shootStuff()
{
    if (shotCoolDown > 0) shotCoolDown--;
    else if (Greenfoot.isKeyDown(shootKey))
    {
        getWorld().addObject(new Bullet(getRotation()), getX(), getY());
        shotCoolDown = 20;
    }
}
}
danpost danpost

2019/6/25

#
Okay, now you have duplicate crap everywhere. Remove lines 2, 3, 14, 15, 17, 26, 27, 31, 32, 38 and 40. Remove line 24 and un-comment line 25. Then change lines 44 and 52 to start with:
setImage(new GreenfootImage("..."));
Insert between lines 57 and 58:
getImage.scale(50, 50);
setImage(getImage());
Remove line 73. Remove lines 78, 79 and 81; then change actor in line 80 to pipe. Change myPlayerNum in line 86 to playerNum. Insert between lines 91 and 92:
pipe.setRotation(this.getRotation());
Remove line 245. Now, maybe you should pass the playerNum value to each new Bullet object so you can easily check if this player fired it or not. Then, you would not need didShoot and the doBulletHit method can be much simplified.
There are more replies on the next page.
1
2
3
4