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

2018/4/21

can I use an (id==) for If canSee.asap.class?

Shabir Shabir

2018/4/21

#
[Disallowed URL] Hi guys, I'm working on a schoolproject. I have a problem I have made a game with 3 levels. In level1 I have on player. In level 2 I have 2 players. I want to get from level 2 tot level 3, so I need the 2players in level2 tot get tot a house where they then get to level 3, But It's not working fyi I have 1class with 2 id (id==1 and id==2). I want it to be like fireboy & watergirl, but use the same house instead of 2 At the bottom is the code which will get me to level 3. .
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class LittleRedcap here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class LittleRedCap extends BaseClass
{
    private int speed=3;
    private int id;

    public LittleRedCap (int idParameter)
    {
        this.id=idParameter;

        if(id==2)
        {
            GreenfootImage myImage = getImage();
            int myNewHeight = (int) myImage.getHeight()/2;
            int myNewWidth = (int) myImage.getWidth()/2;
            myImage.scale (myNewWidth, myNewHeight);
        }

        if(id==3)
        {
            GreenfootImage myImage = getImage();
            int myNewHeight = (int) myImage.getHeight()/2;
            int myNewWidth = (int) myImage.getWidth()/2;
            myImage.scale (myNewWidth, myNewHeight);
        }
    }

    public void act() 
    {
        keys();
        pickFlowers();
        Announcement();
        nextLevel(2);
        nextLevel(3);

    } 

    public void pickFlowers()
    {
        if(canSee(Flower.class))
        {

            ScoreBoard scoreboard = (ScoreBoard) getWorld().getObjects(ScoreBoard.class).get(0);
            super.score=scoreboard.getScore();
            Flower flower = (Flower) getOneObjectAtOffset(0, 0, Flower.class);
            if(flower.getPoisonous())
            {
                super.score = super.score-5; 
            }else {
                super.score++;
            }
            removeObject(Flower.class);
            World world = getWorld();
            world.removeObjects(world.getObjects(ScoreBoard.class));
            world.addObject (new ScoreBoard(super.score), 85,25);

        }

    }

    public void Announcement()
    {
        if(this.score<0)
        {World world = getWorld();
            world.addObject (new Announcement("Game Over"), 300,300);
            Greenfoot.stop();
        }
    }

    /**
     * With the arrow keys, you are able to control Little Redcap
     */
    public void keys()
    {
        if (this.id==1)
        {
            if (Greenfoot.isKeyDown("left"))
            {
                setLocation(getX() - speed, getY());
                setImage("redcapleft.gif");

                
            }
            if (Greenfoot.isKeyDown("right"))
            {
                setLocation(getX() + speed, getY());
                setImage("redcapright.gif");

                
            }

            if (Greenfoot.isKeyDown("up"))
            {
                setLocation(getX(), getY() - speed);
                setImage("redcapbehind.gif");

              
            }

            if (Greenfoot.isKeyDown("down"))
            {

                setLocation(getX(), getY() + speed);
                setImage("redcapfront.gif");

                

            }
        }
        
         if (this.id==2)
        {

            if (Greenfoot.isKeyDown("a"))
            {
                setLocation(getX() - speed, getY());
                setImage("redcapleft.gif");
                GreenfootImage myImage = getImage();
                int myNewHeight = (int) myImage.getHeight()/2;
                int myNewWidth = (int) myImage.getWidth()/2;
                myImage.scale (myNewWidth, myNewHeight);

            }

            if (Greenfoot.isKeyDown("d"))
            {
                setLocation(getX() + speed, getY());
                setImage("redcapright.gif");
                GreenfootImage myImage = getImage();
                int myNewHeight = (int) myImage.getHeight()/2;
                int myNewWidth = (int) myImage.getWidth()/2;
                myImage.scale (myNewWidth, myNewHeight);
            }

            if (Greenfoot.isKeyDown("w"))
            {
                setLocation(getX(), getY() - speed);
                setImage("redcapbehind.gif");
                GreenfootImage myImage = getImage();
                int myNewHeight = (int) myImage.getHeight()/2;
                int myNewWidth = (int) myImage.getWidth()/2;
                myImage.scale (myNewWidth, myNewHeight);

            }

            if (Greenfoot.isKeyDown("s"))
            {
                setLocation(getX(), getY() + speed);
                setImage("redcapfront.gif");
                GreenfootImage myImage = getImage();
                int myNewHeight = (int) myImage.getHeight()/2;
                int myNewWidth = (int) myImage.getWidth()/2;
                myImage.scale (myNewWidth, myNewHeight);

            }
        }
        

        if (this.id==3)
        {

            if (Greenfoot.isKeyDown("left"))
            {
                setLocation(getX() - speed, getY());
                setImage("redcapleft.gif");
                GreenfootImage myImage = getImage();
                int myNewHeight = (int) myImage.getHeight()/2;
                int myNewWidth = (int) myImage.getWidth()/2;
                myImage.scale (myNewWidth, myNewHeight);

            }

            if (Greenfoot.isKeyDown("right"))
            {
                setLocation(getX() + speed, getY());
                setImage("redcapright.gif");
                GreenfootImage myImage = getImage();
                int myNewHeight = (int) myImage.getHeight()/2;
                int myNewWidth = (int) myImage.getWidth()/2;
                myImage.scale (myNewWidth, myNewHeight);
            }

            if (Greenfoot.isKeyDown("up"))
            {
                setLocation(getX(), getY() - speed);
                setImage("redcapbehind.gif");
                GreenfootImage myImage = getImage();
                int myNewHeight = (int) myImage.getHeight()/2;
                int myNewWidth = (int) myImage.getWidth()/2;
                myImage.scale (myNewWidth, myNewHeight);

            }

            if (Greenfoot.isKeyDown("down"))
            {
                setLocation(getX(), getY() + speed);
                setImage("redcapfront.gif");
                GreenfootImage myImage = getImage();
                int myNewHeight = (int) myImage.getHeight()/2;
                int myNewWidth = (int) myImage.getWidth()/2;
                myImage.scale (myNewWidth, myNewHeight);

            }
        }

    }

    public void setLocation(int x, int y)
    {
        int oldX = getX();
        int oldY = getY();
        super.setLocation(x, y);
        if(!getIntersectingObjects(Wall.class).isEmpty())
        {
            super.setLocation(oldX, oldY);
        }
    }

    public int getId()
    {
        return this.id;
    }

    public void nextLevel (int levelParameter)
    {
        if(levelParameter ==2) {      

            if ((score ==10) && (canSee(House.class)))
            {
                World level2 = new Level2();
                Greenfoot.setWorld(level2);
                //setImage();
            }
        }

        if(levelParameter ==3) {      

            if ((score==20) && (canSee(House.class)))
            {
                World level3 = new Level3();
                Greenfoot.setWorld(level3);
                //setImage();
            }
        }
    } 

}
danpost danpost

2018/4/21

#
Where is the canSee method?
Shabir Shabir

2018/4/21

#
I'm not so good with coding. and my English is not so good to explain. But I have used this code to get from one level to another
public void nextLevel (int levelParameter)
    {
        if(levelParameter ==2) {      
 
            if ((score ==10) && (canSee(House.class)))
            {
                World level2 = new Level2();
                Greenfoot.setWorld(level2);
                //setImage();
            }
        }
 
        if(levelParameter ==3) {      
 
            if ((score==20) && (canSee(House.class)))
            {
                World level3 = new Level3();
                Greenfoot.setWorld(level3);
                //setImage();
            }
        }
    } 
danpost danpost

2018/4/21

#
Again --
danpost wrote...
Where is the canSee method?
(not: where are you calling it from).
Shabir Shabir

2018/4/23

#
danpost danpost

2018/4/23

#
Shabir wrote...
this is the project i'm working on can you open it then you might see what I mean.
Best is to share it here on the site with the Publish source code checkbox checked.
Shabir Shabir

2018/4/27

#
I have shared the game it's called littleredcap1.6
Shabir Shabir

2018/4/27

#
If you open the game you will see 2 players one is controlled with wasd and the other with the arrows. after you pick the flowers you have to get to the house to get to the next level. but the player controlled with wasd doesn't work. what I want is that one player has to get into the house and if the second player gets there then level 3 begins. like the game water girl fire boy
Shabir Shabir

2018/4/27

#
The flower which is located at (147,447 ) I can't get there with the player controlled with wasd, but I can with the one controlled with the arrows. I have used the same code for both.
You need to login to post a reply.