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

2014/9/14

Help with fine tuning enemy

rrose5 rrose5

2014/9/14

#
So I have a main actor called "Man" and an enemy called "Skull". Whenever Man walks into Skull he merges a little too much before being killed. How would I make it so that the Man class doesn't merge with the Skull class and dies on contact? Man:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Main character in Scribble Jumper
 * 
 * @rrose5
 * @7/2/2014
 */
public class Man extends Actor
{
    
    private int vSpeed = 0;
    private int acceleration = 2;
    private boolean jumping;
    private int jumpStrength = 20;
    private int speed = 4;
    
    private GreenfootImage run1 = new GreenfootImage("run1.png");
    private GreenfootImage run2 = new GreenfootImage("run2.png");
    private GreenfootImage run3 = new GreenfootImage("run3.png");
    private GreenfootImage run4 = new GreenfootImage("run4.png");
    private GreenfootImage run1l = new GreenfootImage("run1l.png");
    private GreenfootImage run2l = new GreenfootImage("run2l.png");
    private GreenfootImage run3l = new GreenfootImage("run3l.png");
    private GreenfootImage run4l = new GreenfootImage("run4l.png");
    
    private int frame = 1;
    private int animationCounter = 0;
    
    /**
     * Act - do whatever the Man wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        checkFall();
        checkKey();
        platformAbove();
        checkRightWalls();
        checkLeftWalls();
        
        
        animationCounter ++;
    }
    
    public void checkKey()
    {
        if(Greenfoot.isKeyDown("space") && jumping == false)
        {
            jump();
        }
        if(Greenfoot.isKeyDown("d"))
        {
            moveRight();   
        }
        if(Greenfoot.isKeyDown("a"))
        {
            moveLeft();   
        }
    }
    
    public void moveRight()
    {
        setLocation(getX()+speed, getY());  
        
        if(animationCounter % 4 == 0)
        
           animateRight();
    }
    
    public void animateRight()
    {
        if(frame == 1)
        {
           setImage(run1); 
        }
        else if(frame == 2)
        {
           setImage(run2); 
        }
        else if(frame == 3)
        {
           setImage(run3); 
        }
        else if(frame == 4)
        {
           setImage(run4);  
           frame = 1;
           return;
        }
        
        frame ++;
    }
    
    public void moveLeft()
    {
        setLocation(getX()-speed, getY()); 
         if(animationCounter % 4 == 0)
        
            animateLeft();
    }
    
    public void animateLeft()
    {
        if(frame == 1)
        {
           setImage(run1l); 
        }
        else if(frame == 2)
        {
           setImage(run2l); 
        }
        else if(frame == 3)
        {
           setImage(run3l); 
        }
        else if(frame == 4)
        {
           setImage(run4l);  
           frame = 1;
           return;
        }        
        frame ++;
    }
    
    public void fall()
    {
        setLocation(getX(), getY() +vSpeed);
        if(vSpeed <=9)
        {
           vSpeed = vSpeed + acceleration; 
        }
        jumping = true;
    }
    
    public boolean onGround()
    {
        int spriteHeight = getImage().getHeight();
        int lookForGround = (int)(spriteHeight/2) + 5;
        
        Actor ground = getOneObjectAtOffset(0, lookForGround, Ground.class);        
        if(ground == null)
        {
            jumping = true;
            return false;
        }
        else
        {
           moveToGround(ground);
            return true;
        }
    }
    
    public boolean platformAbove()
    {
        int spriteHeight = getImage().getHeight();
        int yDistance = (int)(spriteHeight/ -2);
        
        Actor ceiling = getOneObjectAtOffset(0, yDistance, Ground.class);        
        if(ceiling != null)
        {
            vSpeed =1;
            bopHead(ceiling);
            return true;
        }
        else
        {       
            return true;
        }
    }
    
    public boolean checkRightWalls()
    {
        int spriteWidth = getImage().getWidth();
        int xDistance = (int)(spriteWidth/2);
        
        Actor rightWall = getOneObjectAtOffset (xDistance, 0, Ground.class);
        if(rightWall == null)
        {
           return false; 
        }
        else
        {
            stopByRightWall (rightWall);
            return true;
        }            
    }
    
    public void stopByRightWall (Actor rightWall)
    {
      int wallWidth = rightWall.getImage().getWidth();
      int newX = rightWall.getX() -(wallWidth + getImage().getWidth())/2;
      setLocation(newX -3, getY());
    }
    
    public boolean checkLeftWalls()
    {
        int spriteWidth = getImage().getWidth();
        int xDistance = (int)(spriteWidth/2);
        
        Actor leftWall = getOneObjectAtOffset (-xDistance, 0, Ground.class);
        if(leftWall == null)
        {
           return false; 
        }
        else
        {
            stopByLeftWall (leftWall);
            return true;
        }
    }
        
    public void stopByLeftWall (Actor leftWall)
    {
      int wallWidth = leftWall.getImage().getWidth();
      int newX = leftWall.getX() +(wallWidth + getImage().getWidth())/2;
      setLocation(newX +3, getY());
    }
      
    public void bopHead (Actor ceiling)
    {
       int ceilingHeight = ceiling.getImage().getHeight(); 
       int newY = ceiling.getY() + (ceilingHeight + getImage().getHeight())/2;
       
       setLocation(getX(), newY);       
    }
    
    public void moveToGround(Actor ground)
    {
       int groundHeight = ground.getImage().getHeight(); 
       int newY = ground.getY() - (groundHeight + getImage().getHeight())/2;
       
       setLocation(getX(), newY);
       jumping = false;
    }
    
    public void checkFall()
    {
       if(onGround())
       {
           vSpeed =0;
       }
        else
       {
            fall();
       }
    }
    
    public void jump()
    {
       vSpeed = vSpeed - jumpStrength;
       jumping = true;
       fall();
    }
    
    
    
    
    
    
    
    
}
Skull
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Main character in Scribble Jumper
 * 
 * @rrose5
 * @7/2/2014
 */
public class Skull extends Actor
{
   
    private int vSpeed = 0;
    private int acceleration = 2;
    private int spriteHeight = getImage().getHeight();
    private int spriteWidth = getImage().getWidth();
    private int lookForGroundDistance = (int)spriteHeight/2;
    private int lookForEdge = (int)spriteWidth/2;
    private int speed = 1;
    
    /**
     * Act - do whatever the Man wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        checkFall();
        move();
        kill();
    }
    
    public void move()
    {
      Actor ground = getOneObjectAtOffset(lookForEdge, lookForGroundDistance, Ground.class);
       
      if(ground == null)
      {
         speed *= -1; 
         lookForEdge *= -1;
      }
      else
      {
        move(speed);  
      }
    }
   
    
    public void fall()
    {
        setLocation(getX(), getY() +vSpeed);
        if(vSpeed <=9)
        {
           vSpeed = vSpeed + acceleration; 
        }
    }
    
    public boolean onGround()
    {
        int spriteHeight = getImage().getHeight();
        int lookForGround = (int)(spriteHeight/2) + 5;
        
        Actor ground = getOneObjectAtOffset(0, lookForGround, Ground.class);        
        if(ground == null)
        {
            return false;
        }
        else
        {
           moveToGround(ground);
           return true;
        }
    }
    
    public void moveToGround(Actor ground)
    {
       int groundHeight = ground.getImage().getHeight(); 
       int newY = ground.getY() - (groundHeight + getImage().getHeight())/2;
       
       setLocation(getX(), newY);
    }
    
    public void checkFall()
    {
       if(onGround())
       {
           vSpeed =0;
       }
        else
       {
            fall();
       }
    }
    
    public void kill()
    {
        Actor man;
        man = getOneObjectAtOffset(0, 0, Man.class);
        if (man != null)
        {
            World world;
            world = getWorld();
            world.removeObject(man);
       }
    }
}
NikZ NikZ

2014/9/14

#
getOneObjectAtOffset() means the man must be on the same exact pixel as the skull. You can use getOneIntersectingObject().
NikZ NikZ

2014/9/14

#
Also, lines 99 - 101 can be shortened to getWorld().removeObject(man);
danpost danpost

2014/9/14

#
On line 96, you are using 'getOneObjectAtOffset', which becomes true when the image of the Man object intersects the center of the Skull object. Maybe using 'getOneIntersectingObject' would be more appropriate.
rrose5 rrose5

2014/9/14

#
NikZ wrote...
getOneObjectAtOffset() means the man must be on the same exact pixel as the skull. You can use getOneIntersectingObject().
Where would I put the intersecting part, I tried in the Kill() method but it didn't work.
danpost danpost

2014/9/14

#
Just change line 96 above from 'getOneObjectAtOffset(0, 0, Man.class)' to 'getOneIntersectingObject(Man.class)'. If you have the opposite problem (detection occurs before their images even touch), then load and save the images with my Image Transparency Adder/Trimmer scenario (after downloading it, of course).
NikZ NikZ

2014/9/14

#
Actor man = getOneIntersectingObject(Man.class);
if (man != null) {
    getWorld().removeObject(man);
}
This doesn't work?
NikZ NikZ

2014/9/14

#
danpost wrote...
Just change line 96 above from 'getOneObjectAtOffset(0, 0, Man.class)' to 'getOneIntersectingObject(Man.class)'. If you have the opposite problem (detection occurs before their images even touch), then load and save the images with my Image Transparency Adder/Trimmer scenario (after downloading it, of course).
Oh, I dedn't see that.
rrose5 rrose5

2014/9/14

#
also I fixed lines 99-101 it works great!
danpost danpost

2014/9/14

#
You could also use other methods, like this:
public void kill()
{
    if (isTouching(Man.class)) removeTouching(Man.class);
}
rrose5 rrose5

2014/9/14

#
Thanks Dan I rewrote it to say getOneIntersectingObject(Man.class), it works really well! Edit: I also find it interesting how in writing code there could be several ways of going about certain aspects.
danpost danpost

2014/9/15

#
rrose5 wrote...
I also find it interesting how in writing code there could be several ways of going about certain aspects.
Oftentimes, one way is better than another. Deciding on which way to go about it is part of good programming. The decision should be based on the specifics of your scenario as well as what is most efficient. For example, is it more efficient to have all your enemies looking for one man or for one man to be looking for all enemies? Food for thought.
rrose5 rrose5

2014/9/15

#
That's really cool, I didn't even know that methods could be so specific that one works better then others with specific scenarios.
danpost danpost

2014/9/15

#
danpost wrote...
You could also use other methods, like this:
public void kill()
{
    if (isTouching(Man.class)) removeTouching(Man.class);
}
This is better ('removeTouching' will not fail if none are found, so 'isTouching' is not needed):
public void kill()
{
    removeTouching(Man.class);
}
rrose5 rrose5

2014/9/15

#
Cool I edited that part now.
You need to login to post a reply.