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

2019/6/26

Player shoots itself.

MisterUnknown MisterUnknown

2019/6/26

#
In my two-player-game, I am currently working on one player just works fine, while the other hits itself with the bullet. Bullet code:
public class Bullet extends Actor
{
    boolean justCreated = false;
    private int timer = 10;
    public Bullet(int dir)
    {
        
        setRotation(dir);
        justCreated = true;
    }
    /**
     * Act - tut, was auch immer Bullet tun will. Diese Methode wird aufgerufen, 
     * sobald der 'Act' oder 'Run' Button in der Umgebung angeklickt werden. 
     */
    public void act() 
    {
       
        if(justCreated){
            justCreated = false;
            Greenfoot.playSound("gun.wav");
            move(35);
            
        }
        move(8);
        
        if(atWorldEdge())
            getWorld().removeObject(this);
    }    
     public boolean atWorldEdge()  
    {  
        if(getX() < 10 || getX() > getWorld().getWidth() - 10)  
            return true;  
        if(getY() < 10 || getY() > getWorld().getHeight() - 10)  
            return true;  
        else  
            return false;  
    }  
    
}
Entire player-class:
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;
    int shotCoolDown = 0;
    String shootKey = "o";
    public Player(int playerNum, String image, String left, String right, String up, String down)
    {
        myImage = new GreenfootImage(image);
        myPlayerNum = playerNum;
       
        leftKey = left;
        rightKey = right;
        upKey = up;
        downKey = down;
        didShoot = false;
        myImage.scale(50, 50);
        setImage(myImage);
       
         if (playerNum == 0)
        {
             myImage = new GreenfootImage("tank.png");
            rightKey = "right";
            leftKey = "left";
            upKey = "up";
            downKey = "down";
            shootKey = "o";
        }
        else
        {
            myImage = new GreenfootImage("tank2.png");
            rightKey = "d";
            leftKey = "a";
            upKey = "w";
            downKey = "s";
            shootKey = "e";
        }
        myImage.scale(50, 50);
        setImage(myImage);
    }
    /**
     * 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();
       shootStuff();
        didShoot = false;
        
    }    
    
    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(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  ((playerNum == 1)&& 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 == 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;
}
}
danpost danpost

2019/6/26

#
Change Bullet class to:
public class Bullet extends Actor
{
    int id;
    
    public Bullet(int ident)
    {
        id = ident;
    }
    
    public void act()
    {
        move(8);
        if (isAtEdge()) getWorld().removeObject(this);
    }
}
Then, replace line 253 in the Player class with this:
Actor bullet = new Bullet(playerNum);
bullet.setRotation(getRotation());
getWorld().addObject(bullet, getX(), getY());
bullet.move(35);
Greenfoot.playSound("gun.wav");
danpost danpost

2019/6/26

#
Now, you can put the following at line 228:
Bullet bull = (Bullet)getOneIntersectingObject(Bullet.class);
if (bull != null && bull.id != playerNum)
and remove line 233.
MisterUnknown MisterUnknown

2019/6/26

#
Actor bullet = new Bullet(playerNum);
the object requires two integers, so one more alongside playerNum. any suggestions?
danpost danpost

2019/6/26

#
MisterUnknown wrote...
Actor bullet = new Bullet(playerNum);
the object requires two integers, so one more alongside playerNum. any suggestions?
Review my last two posts. Make sure you did everything.
MisterUnknown MisterUnknown

2019/6/26

#
Yeah, I fixed it by myself thanks. Eventhough it wont even start to shoot and the conosle is being opened as I push a shootkey
You need to login to post a reply.