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

2020/4/27

How to make projectiles turn around?

scaps scaps

2020/4/27

#
How can I make it so that when my actor fires projectiles, the program looks which direction the actor is faceing (left or right), and fires a projectile in that direction.
danpost danpost

2020/4/27

#
scaps wrote...
How can I make it so that when my actor fires projectiles, the program looks which direction the actor is faceing (left or right), and fires a projectile in that direction.
Depends on how your actor turns around. Show actor's codes.
scaps scaps

2020/4/28

#
Sure,
        import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Guy here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Guy extends Actor
{
    /**
     * Act - do whatever the Guy wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    private boolean left = false; 
    private boolean right = true;
    private int delay = 0;
    
    public void act() 
    {
        if(delay > 0){
            delay--;
        }
         
        movement();
        
        if(delay == 0){
        shoot();
    }
    }    
    
    private void movement(){
        if(Greenfoot.isKeyDown("left")){
            move(-4);
            left= true; 
            right= false;
        }
        
        
        if(Greenfoot.isKeyDown("right")){
            move(4);
            left= false; 
            right= true;
            
        }
        if(right==true){
            setImage(new GreenfootImage("alligator.png"));
        }
        if(left==true){
            setImage(new GreenfootImage("alligator - Copy.png"));
        }   
        
            
        }
        private void shoot(){
            World world= getWorld();
     projectile_1 projectile= new projectile_1();
            
            if(Greenfoot.isKeyDown("z")){
            getWorld().addObject( projectile,getX(),getY());
            delay= 80;

        }
        
            
            
    }
        }
    
    
    

danpost danpost

2020/4/28

#
You only really need one of the boolean fields (left and right). I would keep the left one. Then, use its value to set the turn of the projectile.
scaps scaps

2020/4/28

#
alright ill try thanks!
scaps scaps

2020/4/28

#
Um I know this is a bit silly, but how can I get the projectile to turn when its fired, would it involve having the actor set the turn of a projectile when its fired or what? and if so how would I do that.
danpost danpost

2020/4/28

#
scaps wrote...
Um I know this is a bit silly, but how can I get the projectile to turn when its fired, would it involve having the actor set the turn of a projectile when its fired or what? and if so how would I do that.
Set its rotation at line 58 above:
if (left) projectile.turn(180);
scaps scaps

2020/4/28

#
Its not behaving how I want it the projectile is turning with the actor, anyway to make it so that the projectile doesnt turn with the actor, and just move on its own. The projectile is supposed to mimic a bullet.
danpost danpost

2020/4/28

#
scaps wrote...
Its not behaving how I want it the projectile is turning with the actor, anyway to make it so that the projectile doesnt turn with the actor, and just move on its own. The projectile is supposed to mimic a bullet.
Show projectile_1 class codes.
scaps scaps

2020/4/28

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class projectile_1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class projectile_1 extends Actor
{
    /**
     * Act - do whatever the projectile_1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    World world= getWorld();
    
    public void act() 
    {
       move(2);
        
        if (isAtEdge()){
         getWorld().removeObject(this);  
            
        }
        
  
    }    
    
    
}
danpost danpost

2020/4/28

#
Alright, please show revised Guy class codes.
scaps scaps

2020/4/28

#
alright,
        import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Guy here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Guy extends Actor
{
    /**
     * Act - do whatever the Guy wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    private boolean left = false;
    private int delay = 0;
    
    public void act() 
    {
        if(delay > 0){
            delay--;
        }
      projectile_1 projectile = new projectile_1();
        movement();
        
        if(delay == 0){
        shoot();
    }
    }    
    
    private void movement(){
        if(Greenfoot.isKeyDown("left")){
            move(-4);
            left= true; 
            
        }
        
        
        if(Greenfoot.isKeyDown("right")){
            move(4);
            left= false; 
            
            
        }
        if(left==false){
            projectile_1 projectile = new projectile_1();
            setImage(new GreenfootImage("alligator.png"));
            projectile.turn(360);
        }
        if(left==true){
            projectile_1 projectile = new projectile_1();
            setImage(new GreenfootImage("alligator - Copy.png"));
            projectile.turn(180);
        }   
       projectile_1 projectile = new projectile_1();

        
            
        }
        private void shoot(){
            World world= getWorld();
     projectile_1 projectile= new projectile_1();
            
            if(Greenfoot.isKeyDown("z")){
            getWorld().addObject( projectile,getX(),getY());
            delay= 80;

        }
        
            
            
    }
        }
    
    
    

danpost danpost

2020/4/28

#
I count 5 lines where you create a new projectile_1 object. You should only have one line doing that. Actually, none of them are truly where the one line you should have should be. From everywhere except the shoot method, remove all lines that use "projectile". Change the shoot method to this:
private void shoot(){
    if (Greenfoot.isKeyDown("z")){
        projectile_1 projectile = new projectile_1();
        getWorld().addObject(projectile, getX(), getY());
        if (left) projectile.turn(180);
        delay = 80;
    }
}
You need to login to post a reply.