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

2013/1/6

All Nighter

BradH BradH

2013/1/6

#
Hey guys i have been up all night, I have a side scroll game where you can walk back and forth among the world. the actor walks with WASD when I press a for the character to walk back I get a runtime error (nullpointer) The problem is that I have a different class for the character when it faces different ways (moving forward and back) I also have a different classes for each weapon (the way it faces) as I move -------> with "d" and the other way with "a". Below is one of my Weapon classes. As one can see I have "a" removing this actor because "a" makes the character turnaround. In another class "a" adds another actor( a turned around version of the Gun actor in the code below). So I was thinking of ways to simplify this and I want to make sure this idea is possible. Can I just make one class for each actor. For example one class for both guns and when "a" is pressed it sets an image to a turned around version of the Gun (which is possible) but would the gun then shoot in that direction it would face? Thanks for reading this sorry if it does not make sense I am pretty tired, haha
 public class Gun extends Weapons
 {   
    private Spacemarinemodel1 spacemarinemodel1; 
  
    public Gun(Spacemarinemodel1 spacemarinemodel1) {   
        this.spacemarinemodel1 = spacemarinemodel1;   
    }   
  
 
    
  
    
    public void act() { 
       
       
        if("l".equals(Greenfoot.getKey())) 
           {
           if(getWorld().getObjects(Bullet1.class).size()<=3)  
                
                  fire();
                
        }
           
          if((Greenfoot.isKeyDown("a")))
        {
        getWorld().removeObject(this);
        

        }
      //switch weapon to Sniper
        if((Greenfoot.isKeyDown("2")))
        getWorld().addObject(new Sniper(spacemarinemodel1), getX(), getY());
        //remove the first weapon 
                
        
        if((Greenfoot.isKeyDown("2")))
                {getWorld().removeObject(this);
            }
              
         
        
       
         int spacemarinemodel1X = spacemarinemodel1.getX();   
        int spacemarinemodel1Y = spacemarinemodel1.getY();   
        // Modify the xOffset and   
        // yOffset to make the gun   
        // appear in the correct   
        // position.   
        int xOffset = 27;   
        int yOffset = -8;   
        int x = spacemarinemodel1X + xOffset;   
        int y = spacemarinemodel1Y + yOffset;   
        setLocation(x, y);  
        
        
         
}
    /**
     * fire the gun
     */
    private void fire()
    {
        Ammo Bullet1 = new Bullet1();
        getWorld().addObject(Bullet1, getX() , getY());
        
       Bullet1.move(45);
    }
    
}
danpost danpost

2013/1/6

#
Changing the image would certainly be much easier than having seperate classes. You can take the 'facingRight' image, rotate it 180 degrees, and then mirror it vertically for the 'facingLeft' image with the proper rotation for movement.
// instance variable
boolean facingLeft = false;

// in act or a method it calls
int dx=0;
if(Greenfoot.isKeyDown("a")) dx--;
if(Greenfoot.isKeyDown("d")) dx++;
if((!facingLeft && dx<0) || (facingLeft && dx>0)
{
    turn(180);
    getImage().mirrorVertically();
    facing = !facing;
}
If you add a constructor to the Bullet1 class to accept a boolean value in a parameter, the direction facing can be passed to it.
// instead of line 63 above
Ammo Bullet1 = new Bullet1(facingLeft);

// in Bullet1 class, new constructor
public Bullet1(boolean facingLeft)
{
    if(facingLeft) turn(180);
}
BradH BradH

2013/1/6

#
Where do I define the variable facing for that last if statement facing = !facing
danpost danpost

2013/1/6

#
Sorry, those are supposed to be 'facingLeft's.
BradH BradH

2013/1/7

#
The problem now is that when I press a (to turn around) I expect the character to turn around and start walking. What the character does is turn around and if I hold it down he turns around again, if I press a then d the character stands on his head with his feet in the air. Do you think I might need a different key to move maybe like arrow keys move and a and d turn around, I will try that in the meantime.
BradH BradH

2013/1/7

#
Shouldn't I change mirriorVertically to mirrorHorizontally
danpost danpost

2013/1/7

#
What code do you now have for the character?
BradH BradH

2013/1/7

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

/**
 * Write a description of class SpacemarineModel1 here.
 * 
 * @author (BradH name) 
 * @version (1/2/13)
 */
public class Spacemarinemodel1 extends SMARINE
{private int Walk = (0);
private static final String
[] IMAGES= { "Space Marine Body2.png" , 
 "Space Marine Body3.png" , 
"Space Marine Body.png" };
private static final int RATE = 15;

Sniper sniper;
//For turning around
boolean facingLeft = false;

int dx = 0;

//speed actor falls down
private int vSpeed = 0;
//accl of object as it falls
private int acceleration = 1;
//for counter(health)
private HealthCounter healthcounter;
public Spacemarinemodel1(HealthCounter pointHealthCounter)
{
healthcounter = pointHealthCounter;
}



    /**
     * Act - do whatever the SpacemarineModel1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
    Shot();
        keypress();
    fall();
    onGround();
    Checkfall();
    

}




    
public void keypress()
{
 
//move image
 
       
        if((Greenfoot.isKeyDown("k")))
       { move(3);
        if(facingLeft)
        move(-3);
    }
        
    
    //make the image animate
        if((Greenfoot.isKeyDown("k")))
 
     Walk = (Walk+1)%(RATE*IMAGES.length);
if (Walk%RATE==0)
{
setImage(IMAGES[Walk/RATE]);
}
    
 
     
//jump
 if((Greenfoot.isKeyDown("space")))
 {
     jump();
    }
}
public void Shot()
{ 
Actor eldarshot;
         eldarshot = getOneIntersectingObject(eldarshot.class);
        if ( eldarshot != null)
        {
            World world;
            world = getWorld();
            world.removeObject(eldarshot);
    
     healthcounter.subtract(5);
}
}
public void jump()
{
vSpeed = -4;

fall();

}
    public  boolean onGround()
        {
          Actor under = getOneObjectAtOffset(0, getImage().getHeight() / 2, Ground.class); 
          return under != null;
        }
  public void fall()
        {
          setLocation (getX() , getY() + vSpeed);
          vSpeed = vSpeed + acceleration;  
          //speed incr as actor falls
          
        }
       
        public void Checkfall()
        {//if on floor speed == 0
           if(onGround())
           {
             vSpeed = 0;  
            }
            // not on floor fall
            else{
              fall();  
            }
            
            //turnaround
             
       

if(Greenfoot.isKeyDown("a")) dx--;
if(Greenfoot.isKeyDown("d")) dx++;
if((!facingLeft && dx < 0 ) || (facingLeft && dx > 0))
{
turn(180);
getImage().mirrorVertically();
facingLeft = !facingLeft;
}

}
     
    }




danpost danpost

2013/1/7

#
I tried to clean up the code a little to get a better idea of how you are trying to do things here. I found that the logic of the 'if' block at lines 61 to 65 did not pan out. The jumping is not regulated to only when on the ground. After line 74 where you set the image, you need to check the value of 'facingLeft' to determine if the image needs mirrored. I was a little confused as to what the 'k' keystroke was for; what it appears to be is that 'k' makes the character move and 'a' and 'd' change the direction that the character faces. Next time, before posting code, press Control-Shift-I. After correcting the above, if you are still having problems with the way the facing of the character is working, re-post the class and explain what is happening compared to what you want to happen. Also, clarify what each keypress is supposed to do.
BradH BradH

2013/1/7

#
Sorry about the mess I need to work on organizing, well before a and d moved the Actor left and right (I might change it back that way) but for now 'k' moves the Actor (3); but if the character is facing left it moves (-3);. there is a variable vspeed and acceleration (which are my variables to add some gravity), when you look in the fall method it adds a vertical speed to the actor (so it falls). The vspeed defined in the fall method is the vspeed + acceleration so the longer the actor falls the faster it falls. I have space to jump and when the actor is falling back down the onGround method detects wether it is on solid ground. Checkfall has the actor stop falling if it is on ground, hence vspeed = 0. What I have is a character that I want to move left and right (and as it moves left it faces left and as it moves right it faces right) in a 2d platform game, which would not be that hard. The part that I have an issue with is getting the weapon (a gun) to also face left, right, and shoot in the direction it is facing. *just wondering what does Ctrl-Shift-I do? Thanks for your time.
danpost danpost

2013/1/7

#
Ctrl-Shift-I properly indents your code (it is the shortcut for Edit>Auto-layout within the code pane).
You need to login to post a reply.