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

2017/4/23

I NEED IMMEDIATE HELP

2
3
4
5
6
7
8
danpost danpost

2017/4/27

#
Xmin_Terminator wrote...
I do not have a health Bar, only score counter, that I want to go down every seccond
Sorry, I meant 'loseScore' and check for a score of zero.
how do I make a countActCycles variable to count the cycles?
Same way a 'score' counter is made in the ScoreCounter class.
Xmin_Terminator Xmin_Terminator

2017/4/27

#
I'm not sure how to do this
danpost danpost

2017/4/27

#
Xmin_Terminator wrote...
I'm not sure how to do this
You can help someone with a counter class which does exactly that and you say you are not sure how? (see here)
Xmin_Terminator Xmin_Terminator

2017/4/27

#
I just used the code you helped me to create. I should probably go over that code and check myself
Xmin_Terminator Xmin_Terminator

2017/4/28

#
I am still unsure how to exactly do this.
Yehuda Yehuda

2017/4/28

#
There is nothing to be unsure about:
danpost wrote...
Xmin_Terminator wrote...
how do I make a countActCycles variable to count the cycles?
Same way a 'score' counter is made in the ScoreCounter class.
danpost wrote...
Xmin_Terminator wrote...
I'm not sure how to do this
You can help someone with a counter class which does exactly that and you say you are not sure how? (see here)
Xmin_Terminator wrote...
I just used the code you helped me to create....
Xmin_Terminator Xmin_Terminator

2017/4/28

#
public class CountingCycles extends Actor
{
    int countActCycles = 0;
    /**
     * Act - do whatever the CountingCycles wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        addActCycles();
        checkCycles();
    }   
    
    public void addActCycles()
    {
        countActCycles++;
    }
    
    public void checkCycles()
    {
        if (countActCycles == 60)
        {
            World myWorld = getWorld();
            Background1 background1 = (Background1)myWorld;
            ScoreCounter scoreCounter = background1.getScoreCounter();
            scoreCounter.loseScore();
        }
    }
}
public class ScoreCounter extends Actor
int score = 0;
    /**
     * Act - do whatever the ScoreCounter wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setImage(new GreenfootImage("Score : " + score,60, Color.GREEN, Color.YELLOW));
    } 
    
    public void addScore()
    {
        score++;
    }
    
    public void loseScore()
    {
        score--;
    }
Why isn't it taking away score every second, it works fine when adding score when collecting points:
public class Player extends Actor
{
   public void act()
   {
       movement();
       getPoints();
   }
    
    public Player()
   {
        GreenfootImage myImage = getImage();
        int myNewHeight = (int)myImage.getHeight()/3;
        int myNewWidth = (int)myImage.getWidth()/3;
        myImage.scale(myNewWidth, myNewHeight);
    }
   
    public void movement() 
    {
        if(Greenfoot.isKeyDown("right"))
        {
            setRotation(0);
            if(getOneObjectInFront(Wall.class)==null)
            if(getOneObjectInFront(Maze.class)==null)
            if(getOneObjectInFront(LaserWall.class)==null)
            move(8);
        }
        if(Greenfoot.isKeyDown("left"))
        {
            setRotation(180);
            if(getOneObjectInFront(Wall.class)==null)
            if(getOneObjectInFront(Maze.class)==null)
            if(getOneObjectInFront(LaserWall.class)==null)
            move(8);
        }
        if(Greenfoot.isKeyDown("up"))
        {
            setRotation(270);
            if(getOneObjectInFront(Wall.class)==null)
            if(getOneObjectInFront(Maze.class)==null)
            if(getOneObjectInFront(LaserWall.class)==null)
            move(8);
        }
        if(Greenfoot.isKeyDown("down"))
        {
            setRotation(90);
            if(getOneObjectInFront(Wall.class)==null)
            if(getOneObjectInFront(Maze.class)==null)
            if(getOneObjectInFront(LaserWall.class)==null)
            move(8);
        }
   }
   
   private Actor getOneObjectInFront(Class c)
   {
       GreenfootImage myImage = getImage();
       int distanceToFront = myImage.getWidth();
       int xOffset = (int)Math.ceil(distanceToFront*Math.cos(Math.toRadians(getRotation()))); 
       int yOffset = (int)Math.ceil(distanceToFront*Math.sin(Math.toRadians(getRotation())));
       return (getOneObjectAtOffset(xOffset, yOffset, c));
   }
   
   public void getPoints()
    {
       Actor Points = getOneIntersectingObject(Points.class);
       if(Points != null)
       {
          World myWorld = getWorld();
          myWorld.removeObject(Points);
          Background1 background1 = (Background1)myWorld;
          ScoreCounter scoreCounter = background1.getScoreCounter();
          scoreCounter.addScore();
       }
   }
}
Yehuda Yehuda

2017/4/28

#
The CountingCycles class is an Actor which has to be added to the world for its code to run. If you want the score to decrease by one every 60 act cycles then put the code from the CountingCycles class into your World subclass.
// all in World
private int actCycles = 0;

@Override
public void act() {
    actCycles++;
    if (actCycles == 60) {
        getScoreCounter().loseScore();
        actCycles = 0;
    }
}
Xmin_Terminator Xmin_Terminator

2017/4/28

#
I used the code but I can't seem to get it to work
Xmin_Terminator Xmin_Terminator

2017/4/28

#
My current code : World subclass :
public void countingCycles() 
    {
        actCycles++;
        if (actCycles == 60)
        {
            getScoreCounter().loseScore();
            actCycles = 0;
        }
    }   
       
    /**
     * Adds objects for background1.
     */
    public Background1()
    {    
        super(1400, 800, 1);
        outlineOfMaze();
        countingCycles();
        mazeParts();
        addLaserWalls();
        prepare();
        addCharacters();
        addPoints();
    }
CountingCycles - Actor
public class CountingCycles extends Actor
{
    /**
     * Act - do whatever the CountingCycles wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        
    }   
}
ScoreCounter = Actor
public class ScoreCounter extends Actor
{
    int score = 0;
    /**
     * Act - do whatever the ScoreCounter wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setImage(new GreenfootImage("Score : " + score,60, Color.GREEN, Color.YELLOW));
    } 
    
    public void addScore()
    {
        score++;
    }
    
    public void loseScore()
    {
        score--;
    }
}
Player - Actor
public class Player extends Actor
{
   public void act()
   {
       movement();
       getPoints();
   }
    
    public Player()
   {
        GreenfootImage myImage = getImage();
        int myNewHeight = (int)myImage.getHeight()/3;
        int myNewWidth = (int)myImage.getWidth()/3;
        myImage.scale(myNewWidth, myNewHeight);
    }
   
    public void movement() 
    {
        if(Greenfoot.isKeyDown("right"))
        {
            setRotation(0);
            if(getOneObjectInFront(Wall.class)==null)
            if(getOneObjectInFront(Maze.class)==null)
            if(getOneObjectInFront(LaserWall.class)==null)
            move(8);
        }
        if(Greenfoot.isKeyDown("left"))
        {
            setRotation(180);
            if(getOneObjectInFront(Wall.class)==null)
            if(getOneObjectInFront(Maze.class)==null)
            if(getOneObjectInFront(LaserWall.class)==null)
            move(8);
        }
        if(Greenfoot.isKeyDown("up"))
        {
            setRotation(270);
            if(getOneObjectInFront(Wall.class)==null)
            if(getOneObjectInFront(Maze.class)==null)
            if(getOneObjectInFront(LaserWall.class)==null)
            move(8);
        }
        if(Greenfoot.isKeyDown("down"))
        {
            setRotation(90);
            if(getOneObjectInFront(Wall.class)==null)
            if(getOneObjectInFront(Maze.class)==null)
            if(getOneObjectInFront(LaserWall.class)==null)
            move(8);
        }
   }
   
   private Actor getOneObjectInFront(Class c)
   {
       GreenfootImage myImage = getImage();
       int distanceToFront = myImage.getWidth();
       int xOffset = (int)Math.ceil(distanceToFront*Math.cos(Math.toRadians(getRotation()))); 
       int yOffset = (int)Math.ceil(distanceToFront*Math.sin(Math.toRadians(getRotation())));
       return (getOneObjectAtOffset(xOffset, yOffset, c));
   }
   
   public void getPoints()
    {
       Actor Points = getOneIntersectingObject(Points.class);
       if(Points != null)
       {
          World myWorld = getWorld();
          myWorld.removeObject(Points);
          Background1 background1 = (Background1)myWorld;
          ScoreCounter scoreCounter = background1.getScoreCounter();
          scoreCounter.addScore();
       }
   }
}
Yehuda Yehuda

2017/4/28

#
Show the full Background class (Ctrl+A Ctrl+C to copy everything). You could also explain what it was that you were trying to do with the CountingCycles class.
danpost danpost

2017/4/28

#
When you create a class (for instance, a ClassName class, regardless of extends), the initialization code for objects created from that class go in the constructor of that class, which would start as follows:
public ClassName()
The code put in its block is executed once, before any actions can be performed on that object. An active world or any Actor object placed in an active world (obviously after their creations) can have code repeatedly executed on them, which greenfoot does through the act method of the subclasses of Actor and World. So, anytime use are programming and you use words like 'continuously', 'repeatedly', 'at regular intervals', 'throughout the life of the object' or 'during gameplay' or anything else along those lines, the 'act' method is where you should be looking to place the code (or place a method call to where the code is located). The 'act' method begins like this:
public void act()
The template for a new Actor subclass has an empty act method included in it. The template for a new World subclass does not; but, you can still add one because the World class API contains an act method with no implementation (it is empty of code).
Yehuda Yehuda

2017/4/28

#
I wasn't sure what's going on with all those methods so I asked to see the whole thing, but I guess you know.
danpost danpost

2017/4/28

#
Yehuda wrote...
I wasn't sure what's going on with all those methods so I asked to see the whole thing, but I guess you know.
Line 18 in the Background class is misplaced. I am not quite sure why there is a CountingCycles subclass of Actor.
Xmin_Terminator Xmin_Terminator

2017/4/28

#
Where do I place line 18 then? and if you really want the whole thing of background here :
There are more replies on the next page.
2
3
4
5
6
7
8