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

2014/7/4

Platform wall help!

rrose5 rrose5

2014/7/4

#
I am making it so that the actor stops at walls and i have the right wall completely working but I need help with making the actor stop at the left walls, I don't know what to make inverse to fix it. right wall codes: ---------------------------------------------------------------------------------as
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());
}
--------------------------------------------------------------------------------------- any help is greatly appreciated!
danpost danpost

2014/7/4

#
You know, I often see code similar to that being used; and, though I have programmed a lot of things with walls/obstacles/etc., I have never used anything even close to it. However, it would seem that using '-xDistance' in line 6 and adding instead of subtracting on lines 21 and 22 would complete the other direction.
rrose5 rrose5

2014/7/4

#
Cool thanks, I'll post later if it works or not. I would also have to change all of the rights to lefts for obvious reasons.
rrose5 rrose5

2014/7/4

#
I made changes and it looks like this. it didnt work though, notice anything wrong?
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
        {
            stopByRightWall (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());
    }
danpost danpost

2014/7/4

#
Line 13 had a 'Right' that should be 'Left'. Lines 21 and 22 have minuses that should be pluses.
rrose5 rrose5

2014/7/4

#
Ah thanks
rrose5 rrose5

2014/7/4

#
I fixed it but the actor still glitches through walls
rrose5 rrose5

2014/7/4

#
Oh I didn't fix the minuses
rrose5 rrose5

2014/7/4

#
its still not working, notice anything else wrong?
 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());
    }
danpost danpost

2014/7/4

#
rrose5 wrote...
its still not working, notice anything else wrong? < Code Omitted >
No. Maybe you should explain how it is not working (specifically) and post the entire class code.
rrose5 rrose5

2014/7/4

#
the code is not working because the actor glitches through walls and he gets an odd push at the end (from the right wall method?). anyway here's the code.
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();
        
        
        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();
    }   
}
danpost danpost

2014/7/4

#
Just because you coded methods to deal with walls on the left does no make them work. You still need to execute those methods. You need a 'checkLeftWalls' call in your 'act' method.
rrose5 rrose5

2014/7/5

#
Oh wow I cannot believe I missed that, thought I had it in the act method for sure , thanks
You need to login to post a reply.