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

2017/5/6

Why did the man is going slower when he reach score=2000????

Timon Timon

2017/5/6

#
This is the code from myWorld.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MyWorld extends World
{
    int started=0;
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 600, 1); 
        prepare();
        spawnObjects();
        spawnBoxes();

    }
    public int game=1;
    public void start()
    {
        if(game==1)
        {
            started=1;
            addObject(new spawner(), 0, 0);
            addObject(new Score(), 400, 100);
        }
    }

    public void Game1()
    {
      game=0;
      addObject(new Boss1(), 400, 300);
    }

    public void Boss1()
    {
        addObject(new Thief1(), 750, 489);  
    }

    public void GameOver()
    {
        End end = new End();
        addObject(end,400,250);   

    }

    private void prepare()
    {

        Line line = new Line();
        addObject(line,352,596);
        line.setLocation(352,589);
        Mans mans = new Mans();
        addObject(mans,155,496);
        mans.setLocation(400,489);

    }

    public void spawnObjects()
    {
        addObject(new Toutorial(), 400, 100);
    }
    int x;
    public void spawnBoxes()
    {   
        if (started==1)
        {
            x = Greenfoot.getRandomNumber(30);
            if (x==0)
            {
                addObject(new Boxes(), 550, 550);
                addObject(new Walls(), 650, 532);  
                addObject(new Walls(), 750, 532);
            }
            if (x==1)
            {
                addObject(new Boxes(), 750, 550);
            }
            if (x==2)
            {
                addObject(new Boxes(), 650, 550);
                addObject(new Walls(), 750, 532);
            }
            if (x==3)
            {
                addObject(new BadBoxes(), 750, 550);
            }
            if (x==4)
            {
                addObject(new Boxes(), 550, 550);
                addObject(new BadWalls(), 650, 532);  
                addObject(new BadWalls(), 750, 532);
            }
            if (x==5)
            {
                addObject(new BadBoxes(), 550, 550);
                addObject(new BadWalls(), 650, 532);  
                addObject(new BadWalls(), 750, 532);
            }
            if (x==6)
            {
                addObject(new BadBoxes(), 550, 550);
                addObject(new Walls(), 650, 532);  
                addObject(new Walls(), 750, 532);
            }
            if (x==7)
            {
                addObject(new BadBoxes(), 550, 550);
                addObject(new BadWalls(), 650, 532);  
                addObject(new Walls(), 750, 532);
            }
            if (x==8)
            {
                addObject(new BadBoxes(), 550, 550);
                addObject(new Walls(), 650, 532);  
                addObject(new BadWalls(), 750, 532);
            }
            if (x==9)
            {
                addObject(new Boxes(), 550, 550);
                addObject(new BadWalls(), 650, 532);  
                addObject(new Walls(), 750, 532);
            }
            if (x==10)
            {
                addObject(new Boxes(), 550, 550);
                addObject(new Walls(), 650, 532);  
                addObject(new BadWalls(), 750, 532);
            }
            if (x==11)
            {
                addObject(new BadBoxes(), 650, 550);
                addObject(new BadWalls(), 750, 532);
            }
            if (x==12)
            {
                addObject(new BadBoxes(), 650, 550);
                addObject(new Walls(), 750, 532);
            }
            if (x==13)
            {
                addObject(new Boxes(), 650, 550);
                addObject(new BadWalls(), 750, 532);
            }
            if (x==14)
            {
                addObject(new Boxes(), 650, 550);
                addObject(new Boxes(), 750, 550);
            }
            if (x==15)
            {
                addObject(new BadBoxes(), 650, 550);
                addObject(new BadBoxes(), 750, 550);
            }
            if (x==16)
            {
                addObject(new BadBoxes(), 650, 550);
                addObject(new Boxes(), 750, 550);
            }
            if (x==17)
            {
                addObject(new Boxes(), 650, 550);
                addObject(new BadBoxes(), 750, 550);
            }
            if (x==18)
            {
                addObject(new Coin(), 750, 325);
            }
            if (x==19)
            {
                addObject(new Barrels(), 716,487);
            }
            if (x==20)
            {
                addObject(new Barrels(), 716,487);
                addObject(new Barrels(), 716,319);
            }
            if (x==21)
            {
                addObject(new Barrels(), 714,490);
                addObject(new Barrels(), 586,490);
                addObject(new Barrels(), 619,322);
            }
        }
    }
}
This is the code from the Score.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Score here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Score extends Actor
{
    /**
     * Act - do whatever the Score wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        this.countScore();
    } 
    public int score=0;
    public int start=1;
     public void countScore()
    {
        if (start==1)
        {
           score++;
        }
        setImage(new GreenfootImage("Score: " + score, 48, Color.WHITE, Color.BLACK));
        if(score==2000)
        {
            MyWorld m = (MyWorld) getWorld();
            m.Game1();
            start=0;
        }
    }
}
This is the code from Man.
import greenfoot.*;
public class Mans extends Actor
{
    public int ySpeed;
    public int groundLevel=489;
    private boolean pressed;
    public void act()
    {
        boolean onGround = false;
        ySpeed++;
        setLocation(getX(), getY()+ySpeed);
        // at ground level
        if (getY() > groundLevel)
        {
            ySpeed = 0;
            setLocation(getX(), groundLevel);
            onGround = true;
        }
        // hitting box
        Actor object = getOneIntersectingObject(Objects.class);
        if (object != null)
        {
            int yDir = (int)Math.signum(ySpeed);
            setLocation(getX(), object.getY()-yDir*((object.getImage().getHeight()+getImage().getHeight())/2+1));
            ySpeed = 0;
            if (yDir > 0) onGround = true;
        }
        // jumping
        if (onGround && Greenfoot.isKeyDown("up"))
        {
            ySpeed = -15;
        }
        // firing weapon       
        if (pressed == Greenfoot.isKeyDown("space"))
        {
            pressed = !pressed;
            if (pressed) getWorld().addObject(new Weapons(), getX()+80, getY()-25); 
        } 
        //die
        if (this.isAtEdge())
        {   this.removeTouching(Objects.class);
            this.setImage("Mans3.png");
            this.setLocation(400, 400);
            Greenfoot.playSound("game-over.wav");
            Greenfoot.stop();
            MyWorld m = (MyWorld) getWorld();
            m.GameOver();
        }
        //get a Coin
        if(this.isTouching(Coin.class))
        {
          this.removeTouching(Coin.class);  
          this.setLocation(400, 350);
          Greenfoot.playSound("pop.wav");
        }
        //start Boss Battle
        if(this.isTouching(Boss1.class))
        {
           this.removeTouching(Boss1.class);
           this.setLocation(400, 350);
           MyWorld m = (MyWorld) getWorld();
           m.Boss1();
        }
        
    }
}
I hope you can help me.
Yehuda Yehuda

2017/5/7

#
I'll start with your code (to make it simpler). The prepare method will do the same thing if it's just the following:
private void prepare() {
    addObject(new Line(), 352, 589);
    addObject(new Mans(), 400, 489);
}
Line 22 won't ever do anything with your given code. Line 70 should be at line 75. The spawnBoxes method will be simpler if instead of having the whole thing in an if statement you just put
if (started != 1) {
    return;
}
at the beginning. The spawnBoxes method should probably use a switch statement.
        int x = Greenfoot.getRandomNumber(30);
        switch (x) {
            case 0:
                // code for x being equal to 0
                break;
            case 1:
                // code for x == 1
                break;
            case 2:
                // code if(x == 2)
                break;
            // ...
        }
Line 34 in the Mans class should probably be:
if (pressed != Greenfoot.isKeyDown("space"))
The way you have it is right when you start the game you have Weapons being added into the world, then when "space' is pressed the Weapons stop coming, so they always and only come when space is not pressed (I'm assuming this is not what you want). After all that I don't see any code calling the start() method. (If there is other code calling methods then they can be interfering.) So there is no code being run that creates a Score object.
Timon Timon

2017/5/9

#
ok thanks I have It.
Yehuda Yehuda

2017/5/10

#
Timon wrote...
ok thanks I have It.
I know I did give a lot of changes (and I myself am not sure what the reason for it was) but I don't see how any of them helped for your problem of the speed slowing down.
You need to login to post a reply.