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

2017/4/23

I NEED IMMEDIATE HELP

1
2
3
4
5
6
7
Xmin_Terminator Xmin_Terminator

2017/4/26

#
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--;
    }
}
Xmin_Terminator Xmin_Terminator

2017/4/26

#
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();
       }
   }
   
   public void losingPoints()
   {
       World myWorld = getWorld();
       Background1 background1 = (Background1)myWorld;
       ScoreCounter scoreCounter = background1.getScoreCounter();
       scoreCounter.loseScore();
   }
}
Xmin_Terminator Xmin_Terminator

2017/4/26

#
public ScoreCounter getScoreCounter() { return scoreCounter; } (This is from the world sub-class)
Xmin_Terminator Xmin_Terminator

2017/4/26

#
I have shown my current code above, the points system adding is working, when the player touches a point, the point gets removed and a point is added to the score, but the taking away function is not working. If you are wondering why I want to take away points, it is because you will always get maximum points otherwise, as there is nothing to destroy you. Adding the loss of points over times means that time and speed becomes a factor, so not only do you have to complete it with getting all the points, but also complete it quickly.
danpost danpost

2017/4/26

#
Don't you need to call the 'losingPoints' method from the act method for it to do anything?
Xmin_Terminator Xmin_Terminator

2017/4/26

#
How would I call it in my code and where would I put it.
danpost danpost

2017/4/26

#
Xmin_Terminator wrote...
How would I call it in my code and where would I put it.
danpost wrote...
call the 'losingPoints' method from the act method
Xmin_Terminator Xmin_Terminator

2017/4/26

#
how do I call the method from the act method, what do I put in the code and where
danpost danpost

2017/4/26

#
Xmin_Terminator wrote...
how do I call the method from the act method, what do I put in the code and where
The same way you call 'getPoints' and 'movement' from the act method!
Xmin_Terminator Xmin_Terminator

2017/4/26

#
how do I make it so that instead of losing a point every act, only lose a point every 60 acts (1second)
danpost danpost

2017/4/26

#
Xmin_Terminator wrote...
how do I make it so that instead of losing a point every act, only lose a point every 60 acts (1second)
Add an int field to the class to count act cycles (by having it increment each act cycle); when it becomes 60, zero it and have plane lose health.
Xmin_Terminator Xmin_Terminator

2017/4/26

#
how would I code something to count act cycles, how do I add an int field, and to which class. Also to detect if it is 60 do I do (a guess)
if(countActCycles = 60)
{
    score--;
    countActCycles = 0;
}
danpost danpost

2017/4/26

#
Xmin_Terminator wrote...
how would I code something to count act cycles, how do I add an int field, and to which class. Also to detect if it is 60 do I do (a guess)
if(countActCycles = 60)
{
    score--;
    countActCycles = 0;
}
That is pretty close, kid. Line 1 should use '==' instead of '=' (you want to compare the values, not assign one to the other); and line 23 should call 'loseHealth', not decrement 'score' (which was not mentioned previously). You will also need to check for zero health after calling 'loseHealth' (it could become zero here also).
Xmin_Terminator Xmin_Terminator

2017/4/26

#
I do not have a health Bar, only score counter, that I want to go down every seccond
Xmin_Terminator Xmin_Terminator

2017/4/26

#
how do I make a countActCycles variable to count the cycles?
There are more replies on the next page.
1
2
3
4
5
6
7