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

2017/12/26

Need help with score/life

1
2
3
RevoltecX7 RevoltecX7

2017/12/26

#
Hi, I want ask you for help with score/lifes. I have: -2 worlds -1 character -1 counter for life -1 counter for score Now what I want to do is: when my character will go to 2.World, how can i transfer this score/life to next level? And second question - how can I "communicate" between score and life classes?(When my Score class will count for example no. 100, life will be increased). I mean.. when u play as character, you will make "if's" in this class of character for increasing? Or in class-> Score, u will make something like function for increase life? Sorry for bad eng.
danpost danpost

2017/12/27

#
RevoltecX7 wrote...
when my character will go to 2.World, how can i transfer this score/life to next level?
Create 2. World in a line and set the 2. World active in another line. Then you can add lines to make the appropriate modifications to the 2. World in the same block of code.
how can I "communicate" between score and life classes?(When my Score class will count for example no. 100, life will be increased). I mean.. when u play as character, you will make "if's" in this class of character for increasing? Or in class-> Score, u will make something like function for increase life?
Best place to put the if is at the place where the score is found to need a higher value (what that means is that it should not be placed in the Score class or the Lifes class). Without seeing your codes, it is difficult to say how the values or the objects containing them are to "communicate". Sorry for bad eng.
xbLank xbLank

2017/12/27

#
If you want to pass values between worlds use the constructor.
public class World1 extens World
{
	public int score,
	           health;
	public void act()
	{
		if(/*ondition for world change*/)
		{
			World2 w = new World(score,health);
			Greenfoot.setWorld(w);
		}
	}
}

public class World2 extens World
{
	private int score,health;
	public World2(int world1Score,int world1Health)
	{
		score = world1Score;
		health = world1Health;
	}
}
xbLank xbLank

2017/12/27

#
To your second question: Create a method in your score class that returns its value, so you can later check the score value in your life class.
public class ScoreCounter extends Actor
{
	private int score = 0;
	public void Count()
	{
		score++;
	}
	public int getScore()
	{
		return score;
	}
}


//Modify your world class so you can actually use the Counter in your other classes.
public class World1 extends World
{
	private ScoreCounter scoreCount;
	private LifeCounter lifeCount;
	public World1
	{
		scoreCount = new ScoreCounter();
		addObject(scoreCount,0,0);
		
		lifeCount = new LifeCounter();
		addObject(lifeCount,0,0);
	}
	public ScoreCounter getScoreCount()
	{
		return scoreCount;
	}
	public LifeCounter getLifeCount()
	{
		return lifeCount;
	}
}

public class LifeCounter extends Actor
{
	private int life = 0;
	private ScoreCounter counter;
	private boolean wasIncreased;
	public void Count()
	{
		life--; //or whatever
	}
	public void act()
	{
		World1 w = (World1).getWorld();
		counter = w.getScoreCount();
		if(counter.getScore() == 100 && !wasIncreased)
		{
			life+=10; //or whatever
			wasIncreased = true;
		}
	}
}
RevoltecX7 RevoltecX7

2017/12/28

#
Thank you so much 일리아스 , you help me so much with that first problem. With second i will must think about it. Because, my character is doing all of it. In character class is - when he take score, it will count score, if life, then life etc... But you think, it's better, when it is in world class score/life?
xbLank xbLank

2017/12/28

#
You should create a Counter class for Life and Score. Later you can cast these counters in your character class.
RevoltecX7 RevoltecX7

2018/1/22

#
Don't you know, how to use this code for more levels? In that line when i create "world". I have it now for only Level1. But how to get for other levels? (this class, where is this code, is in Charachter)
private void eatNut(){
        Actor nut = getOneIntersectingObject(Nut.class);
        if(nut != null){
            getWorld().removeObject(nut);
            Level1 world = (Level1) getWorld();
            Score score = world.getScore();
            score.addScore();
        }
}
    
private void checkLife(){
        Level1 world = (Level1) getWorld();
        Life life = world.getLife();
        if(getY() == 399){
            life.takeLife();
            setLocation(200, 300);
        }
}
RevoltecX7 RevoltecX7

2018/1/22

#
It's good way, if i will sending info about level to same class in World? For example for better imagination: My character will touch the tree -> which is way for next level. For this moment will be send 3 information(score, life, current level). After call the same level, it will just change way for generate blocks/nuts/enemies/etc. Or it's some another better way?
danpost danpost

2018/1/22

#
Maybe you can acquire some pointer from the way I programmed my Super Level Support Class Demo scenario.
RevoltecX7 RevoltecX7

2018/1/22

#
Thanks! This should be helpful. Exactly this is what I finding. :)
RevoltecX7 RevoltecX7

2018/1/22

#
Soo, the point of next level with sending score/life. Is that Level-> with subclasses?
danpost danpost

2018/1/22

#
Well, Level will be your start-up world. It is not a game playing level in itself, but sets up everything so transitioning between different levels can be easily managed. It creates the initial GUI objects and holds then in 'static' fields (aka: class fields) which makes then accessible from anywhere within the project. It is important to assign these fields in the constructor of the class; otherwise they are not renewed during a reset of the project. After initial set-up is done, it (usually) proceeds to either a menu type world or to the first game playing level. I used the 'started' method for that so that I could maintain a title screen until the 'Run' button is clicked. Sidenote: I just realized that I could add a 'public static void newGame' method to the Level class so as to skip the title screen during a "running reset" (to allow for starting a new game without having to stop and restart the scenario). In other words, the method can be called from anywhere within the project to reset all counters and begin at the first level again.
RevoltecX7 RevoltecX7

2018/1/22

#
Okay. Thanks for help. I will try to realize it. :D
RevoltecX7 RevoltecX7

2018/1/24

#
Um, the problem with X-levels is solved. But I don't know, how to change my code to similary yours -> for saving score/life etc. It's too much complicated for me. I will try do it. But it's too much complicated for me now.
RevoltecX7 RevoltecX7

2018/1/24

#
Can you please explain me, what exactly this doing?
public static Actor getNewStillActor()
{
        return new Actor()
        {
            public void setLocation(int x, int y){}
        };
}
You return Actor... but what it mean? and why?
There are more replies on the next page.
1
2
3