Xmin_Terminator wrote...
I do not have a health Bar, only score counter, that I want to go down every seccondhow do I make a countActCycles variable to count the cycles?
Same way a 'score' counter is made in the ScoreCounter class.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--;
}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();
}
}
}// all in World
private int actCycles = 0;
@Override
public void act() {
actCycles++;
if (actCycles == 60) {
getScoreCounter().loseScore();
actCycles = 0;
}
}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();
}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()
{
}
}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--;
}
}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 ClassName()
public void act()