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

2020/1/11

Problems with getRotation, setRotation

NotAProYet NotAProYet

2020/1/11

#
Hey, I am having some issues with my code. I want to make an enemy shoot me but I should still be able to dodge the bullet. Until now the bullet will follow me and I have no possibility to escape the bullet. Please help me! Enemy.class:
danpost danpost

2020/1/11

#
NotAProYet wrote...
Enemy.class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Anemy here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Enemy extends UnterActor
{   public int LebensCounterEnemy;
    int spawnDieEnemyBullet = 250;
    public Enemy(){
        LebensCounterEnemy=0;
  }
    /**
     * Act - do whatever the Enemy wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        shoot();
        
        zählLebenEnemy();
        aim();
  }    
    private void shoot(){
        
    if (spawnDieEnemyBullet==0){
    getWorld().addObject(new EnemyBullet(), 50, 500); 
    setRotation(this.getRotation());
    spawnDieEnemyBullet=300;
    
    
   }
   if (spawnDieEnemyBullet>0){
    spawnDieEnemyBullet=spawnDieEnemyBullet-1;
    
    
    }
  }
    public void zählLebenEnemy(){
    if(isTouching(Bullet.class)){
    LebensCounterEnemy=LebensCounterEnemy+1;
    removeTouching(Bullet.class);
    }
    if(LebensCounterEnemy==1){
    
    
    }
    if(LebensCounterEnemy==2){
    
    
    }
    if(LebensCounterEnemy==3){
    
    getWorld().addObject((new WinScreen()), getWorld().getHeight()/2, getWorld().getWidth()/2);
    Greenfoot.stop();
    }    
  }
  public void aim()
    {
        PlayerOne playerOne = (PlayerOne)getWorld().getObjects(PlayerOne.class).get(0);
        turnTowards(playerOne.getX(), playerOne.getY()); 
    }
  
}


EnemyBullet.class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class AnemyBullet here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class EnemyBullet extends UnterActor
{
    /**
     * Act - do whatever the AnemyBullet wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
      aim();
      move(2);
    }    
    public void aim()
    {
        PlayerOne playerOne = (PlayerOne)getWorld().getObjects(PlayerOne.class).get(0);
        turnTowards(playerOne.getX(), playerOne.getY()); 
    }


    
    
}
Thanks for your help.
danpost danpost

2020/1/11

#
Remove line 17 and lines 20 thru 24 from the EnemyBullet class (that code was why you could not escape the bullets). Then, replace lines 29 and 30 in the Enemy class with the following:
Actor bullet = new EnemyBullet();
getWorld().addObject(bullet, 50, 500);
bullet.setRotation(getRotation());
NotAProYet NotAProYet

2020/1/11

#
Great! It is working! Thank you very much!
NotAProYet NotAProYet

2020/1/11

#
But how can I now make my Bullets do the same? (So that they don´t follow the mouse constantly but are flying a straight line - joust like the EnemyBullet... ) Bullet.class:
danpost danpost

2020/1/11

#
NotAProYet wrote...
Bullet.class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Bullet here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Bullet extends UnterActor
{
    /**
     * Act - do whatever the Bullet wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        direction();
        removeBullet();        
    }   
    
    public void direction(){       
        turnTowards(Greenfoot.getMouseInfo().getX(), Greenfoot.getMouseInfo().getY());
        move(20);              
    }
    
    private void removeBullet(){
        if(isTouching(Wall.class)){
            getWorld().removeObject(this);
    
        }        
    }
}

PlayerOne.class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class PlayerOne here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class PlayerOne extends UnterActor
{
    public int LebensCounter;
    public PlayerOne(){
        LebensCounter=0;
        
    }
    
    /**
     * Act - do whatever the PlayerOne wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        zählLeben();
        stoppe();
        move();
        collisionEnemyBullet();
        shoot();
    }  
    
    public void move(){
    
       if(Greenfoot.isKeyDown("W")){
            move(2);
            setRotation(270);
       }
    
       if(Greenfoot.isKeyDown("D")){
            move(2);
            setRotation(0);
       }
    
       if(Greenfoot.isKeyDown("A")){
            move(2);
            setRotation(180);
        }
    
       if(Greenfoot.isKeyDown("S")){
            move(2);
            setRotation(90);
       }
    
       if(Greenfoot.mouseClicked(null)){
           getWorld().addObject((new Bullet()),  getX(), getY());
        
       }
    }
    
    public void stoppe(){
        if(isTouching(Wall.class)){
            move(-4);
    
        }
    }
    
    private void zählLeben(){
        if(isTouching(EnemyBullet.class)){
            LebensCounter=LebensCounter+1;
            removeTouching(EnemyBullet.class);
        }
        if(LebensCounter==1){
            getWorld().addObject((new Leben23()),83,60);
    
        }
        if(LebensCounter==2){
            getWorld().addObject((new Leben13()),83,60);
    
        }
        if(LebensCounter==3){
            getWorld().addObject((new Leben03()),83,60);
            getWorld().addObject((new GameOverScreen()), getWorld().getHeight()/2, getWorld().getWidth()/2);
            Greenfoot.stop();
        }
    }
    
    private void shoot(){
        
    }    
}
danpost danpost

2020/1/11

#
About the same way. Remove line 22 from the Bullet class and set the rotation of the bullet when you create it (similar to the enemy bullet except using mouse position instead of the rotation of the creator.
NotAProYet NotAProYet

2020/1/12

#
Like that?
NotAProYet NotAProYet

2020/1/12

#
It isn't working ether like that:
NotAProYet NotAProYet

2020/1/12

#
In my opinion it should work like that:
danpost danpost

2020/1/12

#
NotAProYet wrote...
Like that?
if(Greenfoot.mouseClicked(null)){
            getWorld().addObject((new Bullet()),  getX(), getY());
            setRotation(Greenfoot.getMouseInfo().getX(), Greenfoot.getMouseInfo().getY());
       }
That is nothing like my 3 lines of code used for the enemy bullets above.
NotAProYet NotAProYet

2020/1/12

#
... but this is:
danpost danpost

2020/1/13

#
The last line should be a turnTowards, not a setRotation.
NotAProYet NotAProYet

2020/1/13

#
OH! Sry I totally missed that one... It works just fine... Thanks for your help (again)
You need to login to post a reply.