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

2015/6/9

Pushing Problem

17jrousso 17jrousso

2015/6/9

#
I have a problem with the player not being pushed by the wall. The wall and player are null to each other. I would like to have the wall push the player if they touch each other.
public void shove()
    {
        if(canSee(WallRect.class))
        {
           setLocation(getX()-speed, getY());
        }
    }
danpost danpost

2015/6/9

#
Need to see how this method is incorporated within the sequence of events described in the act method as well as what value the speed field may contain at this point.
17jrousso 17jrousso

2015/6/10

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

/**
 * Write a description of class WallRect here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class WallRect extends Animal
{
    public int speed = 1;
    public void act()
    {
        setLocation(getX()-speed, getY());
        Actor player1=getOneIntersectingObject(Player1.class);
        Actor player2=getOneIntersectingObject(Player2.class);
            if(player1!=null)
            {
                setLocation(getX()+speed,getY());
                setRotation(180);
            }
            if(player2!=null)
            {
                setLocation(getX()+speed,getY());
                setRotation(180);
            }
            if(player1!=null)
            {
                setLocation(getX()+speed,getY());
                setRotation(90);
            }
             if(player2!=null)
            {
                setLocation(getX()+speed,getY());
                setRotation(90);
            }
            if(player1!=null)
            {
                setLocation(getX()+speed,getY());
                setRotation(270);
            }
            if(player2!=null)
            {
                setLocation(getX()+speed,getY());
                setRotation(270);
            }
            if(player1!=null)
            {
                setLocation(getX()+speed,getY());
                setRotation(0);
            }
            if(player2!=null)
            {
                setLocation(getX()+speed,getY());
                setRotation(0);
            }
    }
    public void lizard()
    {
        setRotation(0);  // face downward initially
    }
    public WallRect()
    {
     GreenfootImage squareImage = new GreenfootImage(20, 20);
     squareImage.setColor(new Color(0, 0, 0, 255));
     // drawRect() method not needed because we can just use the rectangular squarImage
     // squareImage.drawRect(0, 0, 20, 20);
     squareImage.fill();
     setImage(squareImage);
    } 
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Player1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Player2 extends Animal
{
     private static final int SPEED = 7;
    public void act() 
    {
        if (canMove())
        {
           
        }
        processKeys();
    }
    /**  
     * Returns true if the Bug is within the World's borders
     */
    
    public boolean canMove()
    
    {
        if (getX() > 0)
        {
           return true; 
        }
        
        else if (getY() > 0)
        {
           return true; 
        }
        
        else if (getX() < 500)
        {
           return true; 
        }
        
        else if (getY() < 500)
        {
           return true; 
        }
        
        else
        
        return false;
    }
        public void processKeys() 
    {
        if(Greenfoot.isKeyDown("a"))
        {
            setLocation(getX()-SPEED,getY());
            //setRotation(180);
            Actor WallRect=getOneIntersectingObject(WallRect.class);
            if(WallRect!=null)
            {
                setLocation(getX()+SPEED,getY());
                //setRotation(180);
            }
        }
        if(Greenfoot.isKeyDown("d"))
        {
            setLocation(getX()+SPEED,getY());
            setRotation(0);
            Actor WallRect=getOneIntersectingObject(WallRect.class);
            if(WallRect!=null)
            {
                setLocation(getX()-SPEED,getY());
                //setRotation(0);
            }
        }
        if(Greenfoot.isKeyDown("w"))
        {
            setLocation(getX(),getY()-SPEED);
            //setRotation(270);
            Actor WallRect=getOneIntersectingObject(WallRect.class);
            if(WallRect!=null)
            {
                setLocation(getX(),getY()+SPEED);
                //setRotation(270);
            }
        }
        if(Greenfoot.isKeyDown("s"))
        {
            setLocation(getX(),getY()+SPEED);
            //setRotation(90);
            Actor WallRect=getOneIntersectingObject(WallRect.class);
            if(WallRect!=null)
            {
                setLocation(getX(),getY()-SPEED);
                //setRotation(90);
            }
        }
    }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Player1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Player1 extends Animal
{
     private static final int SPEED = 7;
     private static final int speed = 1;
    public void act() 
    {
        if (canMove())
        {
        }
        processKeys();
        shove();
    }
    /**  
     * Returns true if the Bug is within the World's borders
     */
    
    public boolean canMove()
    
    {
        if (getX() > 0)
        {
           return true; 
        }
        
        else if (getY() > 0)
        {
           return true; 
        }
        
        else if (getX() < 500)
        {
           return true; 
        }
        
        else if (getY() < 500)
        {
           return true; 
        }
        
        else
        
        return false;
    }
        public void processKeys() 
    {
        if(Greenfoot.isKeyDown("left"))
        {
            setLocation(getX()-SPEED,getY());
            //setRotation(180);
            Actor WallRect=getOneIntersectingObject(WallRect.class);
            if(WallRect!=null)
            {
                setLocation(getX()+SPEED,getY());
                //setRotation(180);
            }
        }
        if(Greenfoot.isKeyDown("right"))
        {
            setLocation(getX()+SPEED,getY());
            setRotation(0);
            Actor WallRect=getOneIntersectingObject(WallRect.class);
            if(WallRect!=null)
            {
                setLocation(getX()-SPEED,getY());
               // setRotation(0);
            }
        }
        if(Greenfoot.isKeyDown("up"))
        {
            setLocation(getX(),getY()-SPEED);
            //setRotation(270);
            Actor WallRect=getOneIntersectingObject(WallRect.class);
            if(WallRect!=null)
            {
                setLocation(getX(),getY()+SPEED);
                //setRotation(270);
            }
        }
        if(Greenfoot.isKeyDown("down"))
        {
            setLocation(getX(),getY()+SPEED);
            //setRotation(90);
            Actor WallRect=getOneIntersectingObject(WallRect.class);
            if(WallRect!=null)
            {
                setLocation(getX(),getY()-SPEED);
                //setRotation(90);
            }
        }
    }
    public void shove()
    {
        if(canSee(WallRect.class))
        {
           setLocation(getX()-speed, getY());
        }
    }  
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class World1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class World1 extends World
{

    /**
     * Constructor for objects of class World1.
     * 
     */
    public World1()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(630, 630, 1); 
        
        WallRect block;
        
        for (int i = 0 ; i < 16 ; i++)
        {
            block = new WallRect() ;
            addObject(block, 600, 20 * i) ;
        }
        for (int i = 20 ; i < 32 ; i++)
        {
            block = new WallRect() ;
            addObject(block, 600, 20 * i) ;
        }
    }
}
17jrousso 17jrousso

2015/6/10

#
This is all the code, it all extends Animal (a prewritten green foot class). Some code is reused from other projects, ignore any out of place comments
danpost danpost

2015/6/10

#
A major issue is the act method of the WallRect class. Once the 'player1' and 'player2' variables are set at lines 17 and 18, they are never reassigned values. So, if 'player1' is not null at line 19, then it will also (still) not be null at lines 29, 39 and 49 (and similar with the 'player2' variable). As a sidenote, changing the rotation of an object with a perfectly square uniform image at 90 degree increments would only be useful if you were using 'move(int)' to move the actor in the four cardinal directions. It appears what you are trying to do is to have the walls move constantly and any player against the wall to be pushed with the movement of the wall. The act method of the WallRect class should simply do that.
public void act()
{
    setLocation(getX()-speed, getY());
    Actor player = getOneIntersectingObject(Player1.class);
    if (player != null) player.setLocation(player.getX()-speed, player.getY());
    player = getOneIntersectingObect(Player2.class);
    if (player != null) player.setLocation(player.getX()-speed, player.getY());
}
You should then remove the 'shove' method (and the call to it in the act method) from the Player1 class.
You need to login to post a reply.