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

2013/1/3

incompatible types

KierTulp KierTulp

2013/1/3

#
Hello! I am having a problem with my shooting code.
public class Beam extends Mover
{
  public int direction = 0;
  public int directionL = 180;
   
    public void act() 
    {
        fireTheBeam();
        shootWhere();
    }
    
    private void fireTheBeam()
    {
        if(direction = 0)
        {
        move(10);
        }
        if(direction = 180)
        {
        move(-10);
        }
    }

     private void shootWhere()
    {
       if(Greenfoot.isKeyDown("right"))
       {
         direction = 0;
       }
       if(Greenfoot.isKeyDown("left"))
       {
         directionL = 180;
       }
    }
}
it is giving incompatible types at the if(direction = 0) parts. What I want to do is make my beam move left if my player is facing left, and make it move right when he faces to the right. I tried doing this by making a variable named direction, and changing it when he looks the other way. thanks in advance.
erdelf erdelf

2013/1/3

#
if you want to compare an integer you have to write
direction == 0
and
direction == 180
KierTulp KierTulp

2013/1/3

#
thanks! it solved that problem, but I found a new one. After I shoot the beam, it still reacts to my movement, so when I shoot the beam to the right, and then move left, it will start moving left. Do you know a solution for that?
erdelf erdelf

2013/1/3

#
you should place the shootWhere method in the constructor, in the act method it will look every act cycle what you are typing
KierTulp KierTulp

2013/1/4

#
It works now, but I have another problem. here's my code so far.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class beam here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */


public class Beam extends Mover
{
  public int direction = 0;
   
  public Beam()
  {
    shootWhere();  
    }
    
    public void act() 
    {
        fireTheBeam();
        
    }

   private void fireTheBeam()
    {
        if( direction == 0)
        {
        move(10);
        }
        if(direction == 180)
        {
        move(-10);
        }
    }

     private void shootWhere()
    {
       if(Greenfoot.isKeyDown("right"))
       {
         direction = 0;
       }

     
       else if(Greenfoot.isKeyDown("left"))
       {
         direction = 180;
       }
    }
}
when I walk right, it will go right, when i walk left, when i stand to the right it will shoot right, but when i stand to the left, it will shoot right. thanks in advance.
danpost danpost

2013/1/4

#
Using 'isKeyDown' to determine the direction of the beam is well and fine if the actor would always be moving; but, since your actor could be still as well, this method of determining the direction will not work. If you think about how a beam would operate, the direction of the beam would be determined long before the actual beam is created (at the moment the trigger for the beam is tripped -- or, at the time the condition for a beam to be instantiated becomes true). Since the direction of the beam is really determined by the direction your actor is facing, just pass that direction to the constructor to have the beam created the correct direction. I cannot give you the exact code because I do not know what your player class looks like; but the complete code for the Beam class should be something like the following:
import greenfoot.*;

public class Beam extends Mover
{
    private boolean firingLeft;

    public Beam(boolean facingLeft)
    {
        firingLeft = facingLeft;
    }
    
    public void act() 
    {
        fireTheBeam();
    }

    private void fireTheBeam()
    {
        if(firingLeft)
        {
            move(-10);
        }
        else
        {
            move(10);
        }
    }
}
Then in your player class, when you create a beam, use something like:
getWorld().addObject(new Beam(facingLeft == true), getX(), getY());
KierTulp KierTulp

2013/1/6

#
I think I am doing something wrong, because it doesn't seem to be working for me, he keeps saying that the facingLeft variable can not be found.
danpost danpost

2013/1/6

#
'facingLeft' is an instance boolean field you need to add to your player class. Anytime your player turns around, set 'facingLeft = !facingLeft;' which toggles the true/false value of the field, keeping track of the direction the player is facing.
danpost danpost

2013/1/6

#
I was looking at the Beam class I coded for you and realized it could be simplified to the following:
import greenfoot.*;

public class Beam extends Mover
{
    public Beam(boolean facingLeft)
    {
        if (facingLeft) turn(180);
    }
    
    public void act() 
    {
            move(10);
    }
}
KierTulp KierTulp

2013/1/6

#
for some reason it is now only firing to the right, even when i walk left. here's my code.
private void animator()      
    {      
        if (Greenfoot.isKeyDown("right"))      
        {       
            sequence++;      
            standWhere = stand;  
            switch (sequence)      
            {      
                case 1: setImage(walk1); break;      
                case 6: setImage(stand); break;      
                case 11: setImage(walk2); break;      
                case 16: setImage(walk3); break;
                case 20: setImage(walk4); break;
                case 25: sequence = 0; break;
            }      
        }
        else if (Greenfoot.isKeyDown("left"))      
        {       
            sequence++;    
            facingLeft = !facingLeft;
            standWhere =  standl;   
            switch (sequence)  
            {      
                case 1: setImage(walk1l); break;      
                case 6: setImage(standl); break;      
                case 11: setImage(walk2l); break;      
                case 16: setImage(walk3l); break;
                case 20: setImage(walk4l); break;
                case 25: sequence = 0; break;
            }      
        }     
        else
        {
            setImage(standWhere);   
        }    
    }  
thats the code for the animator, I put it at the beginning of the walking left animation.
public class Beam extends Mover
{
  private boolean firingLeft;
   
    public void act() 
    {
        move(10);
    }
   
    public Beam(boolean facingLeft)
    {
      firingLeft = facingLeft;   
    }
    
   private void fireTheBeam()
    {
        if(firingLeft)
        {
        move(-10);
        }
        else
        {
        move(10);
        }
    }

    private boolean hit()
    {
        Actor uman = getOneIntersectingObject(Enemy1.class);
        if (uman == null) 
        {
            return false;
        }
        else { 
            return true;
        }
    }
    
    private void checkDestroy()
    {
       if(hit())
       {
         getWorld().removeObjects(getObjectsInRange(40, Enemy1.class));
       }
    
   }
}
thats what i have in the beam right now.
private void checkShoot()
    {
       if(delay>0)
       {
         delay = delay -1;
        }
        
       else if(Greenfoot.isKeyDown("space"))
       {
           fire();
           delay = 30;
        }
    }

     private void fire()
    {
        getWorld().addObject(new Beam(facingLeft == true), getX(), getY());
   }
this is what I have in the player class about the Beam.
danpost danpost

2013/1/6

#
The direction in your animator code is absolute (either it is one or it is the other); so change line 20 in the code for the animator to:
facingLeft = true;
and insert at line 6:
facingLeft = false;
KierTulp KierTulp

2013/1/6

#
It still has the same problem. my code now is:
private void animator()      
    {      
        if (Greenfoot.isKeyDown("right"))      
        {       
            sequence++;      
            standWhere = stand;  
            facingLeft = false;
            switch (sequence)      
            {      
                case 1: setImage(walk1); break;      
                case 6: setImage(stand); break;      
                case 11: setImage(walk2); break;      
                case 16: setImage(walk3); break;
                case 20: setImage(walk4); break;
                case 25: sequence = 0; break;
            }      
        }
        else if (Greenfoot.isKeyDown("left"))      
        {       
            sequence++;    
            facingLeft = true;
            standWhere =  standl;   
            switch (sequence)  
            {      
                case 1: setImage(walk1l); break;      
                case 6: setImage(standl); break;      
                case 11: setImage(walk2l); break;      
                case 16: setImage(walk3l); break;
                case 20: setImage(walk4l); break;
                case 25: sequence = 0; break;
            }      
        }     
        else
        {
            setImage(standWhere);   
        }    
    }  
and the beam one is:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class beam here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */


public class Beam extends Mover
{
  private boolean firingLeft;
   
    public void act() 
    {
        move(10);
    }
   
    public Beam(boolean facingLeft)
    {
      firingLeft = facingLeft;   
    }
    
   private void fireTheBeam()
    {
        if(firingLeft)
        {
        move(-10);
        }
        else
        {
        move(10);
        }
    }

    private boolean hit()
    {
        Actor uman = getOneIntersectingObject(Enemy1.class);
        if (uman == null) 
        {
            return false;
        }
        else { 
            return true;
        }
    }
    
    private void checkDestroy()
    {
       if(hit())
       {
         getWorld().removeObjects(getObjectsInRange(40, Enemy1.class));
       }
    
   }
}
and here's the spawning and delay from the player:
private void checkShoot()
    {
       if(delay>0)
       {
         delay = delay -1;
        }
        
       else if(Greenfoot.isKeyDown("space"))
       {
           fire();
           delay = 30;
        }
    }

     private void fire()
    {
        getWorld().addObject(new Beam(facingLeft == true), getX(), getY());
   }
danpost danpost

2013/1/6

#
danpost wrote...
I was looking at the Beam class I coded for you and realized it could be simplified to the following:
import greenfoot.*;

public class Beam extends Mover
{
    public Beam(boolean facingLeft)
    {
        if (facingLeft) turn(180);
    }
    
    public void act() 
    {
            move(10);
    }
}
Use the code given here. You can add the methods 'hit' and 'checkDestroy' to the code; but nothing more.
vonmeth vonmeth

2013/1/6

#
Edit: nm
You need to login to post a reply.