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

2018/5/9

Need Help with Variable access from world to classes and transfer into different worlds

Shade1177 Shade1177

2018/5/9

#
i have been purposefully avoiding this, but the time has come to stop stalling. i have looked through this site for answers and they seem to just either not help of confuse me more. i have no clue where to even start with this and just need help. anything you can give me on how to do this... ill send more detail and examples of code later, but i just want to get the general conversation out for now to see if i can maybe get some sort of general answer. thank you for any help you can give and i hope that you can understand with this what i'm trying to accomplish. if not? then ill explain in more detail later. thank you and hope to get a reply
Shade1177 Shade1177

2018/5/9

#
this is the cod within my player.class that I am using. public void Score() { if(isTouching(Coin.class)) { int score = ((Level1)getWorld()).getscore(); score = score + 100; } } } and this is the world it comes form. public int score = 0; public int Life = 0; public Level1() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(1000, 800, 1); showText("Score:" + score, 940, 40); prepare(); } public int getscore() { return score; }
Yehuda Yehuda

2018/5/9

#
You would be favoring yourself by mentioning where you had a problem.
Shade1177 Shade1177

2018/5/10

#
pretty much the problem is that the score itself doesn't count up at all when i interact with the coin class. and also i just wanted general help in how to transfer it over between worlds so i could keep the variable value
Yehuda Yehuda

2018/5/10

#
Is the score variable in the World having its value change? I would use public getter and setter methods in the World class for retrieving and setting data. If you can't figure it out paste the full (CTRL+A) code, using code tags, for the classes that you are having trouble with.
danpost danpost

2018/5/10

#
You are creating a new variable in your player class and setting it equal to the value of score in your world. Then you are adding to that new variable. You could just add to score in the world:
((Level1)getWorld()).score += 100;
However, you still need to display that change. Better would be to add another method in your world to allow some change in the score and display that changed value:
public void adjustScore(int amount)
{
    score += amount;
    showText("Score:"+score, 940, 40);
}
then have this line in the player class:
((Level1)getWorld()).adjustScore(100);
Shade1177 Shade1177

2018/5/10

#
ok ill just post all the code of both... LEVEL1 World: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Level1 here. * * @author (your name) * @version (a version number or a date) */ public class Level1 extends World { public int score = 0; public int Life = 0; /** * Constructor for objects of class Level1. * */ public Level1() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(1000, 800, 1); showText("Score:" + score, 940, 40); prepare(); } public int getscore() { return score; } /** * Prepare the world for the start of the program. * That is: create the initial objects and add them to the world. */ private void prepare() { Wall wall = new Wall(); addObject(wall,953,750); Wall wall2 = new Wall(); addObject(wall2,149,608); Wall wall3 = new Wall(); addObject(wall3,428,281); Wall wall4 = new Wall(); addObject(wall4,623,630); Wall wall5 = new Wall(); addObject(wall5,854,317); Wall wall6 = new Wall(); addObject(wall6,146,106); wall4.setLocation(415,750); wall5.setLocation(712,568); wall3.setLocation(479,177); wall2.setLocation(164,541); wall5.setLocation(788,339); wall3.setLocation(465,292); wall5.setLocation(809,253); wall3.setLocation(508,491); wall3.setLocation(508,436); Skeleton skeleton = new Skeleton(); addObject(skeleton,913,100); Skeleton skeleton2 = new Skeleton(); addObject(skeleton2,73,269); Coin coin = new Coin(); addObject(coin,662,751); Coin coin2 = new Coin(); addObject(coin2,138,720); Coin coin3 = new Coin(); addObject(coin3,531,139); Player player = new Player(); addObject(player,515,597); Timer timer = new Timer(); addObject(timer,1000,0); } } and the Player Class import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Player here. * * @author (your name) * @version (a version number or a date) */ public class Player extends Movers { public int BlinkTime = 10; /** * Act - do whatever the Player wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { Move(); blink(); Score(); } /** * Makes the creature go backward */ public void Move() { if ( Greenfoot.isKeyDown("S")) { setLocation(getX(),getY()+2); setRotation(0); Wall wall = null; wall = (Wall)getOneIntersectingObject(Wall.class); if (wall != null) { setLocation(getX(), getY()-4); } } else if ( Greenfoot.isKeyDown("W")) { setLocation(getX(),getY()-2); setRotation(180); Wall wall = null; wall = (Wall)getOneIntersectingObject(Wall.class); if (wall != null) { setLocation(getX(), getY()+4); } } else if ( Greenfoot.isKeyDown("D")) { setLocation(getX()+2,getY()); setRotation(270); Wall wall = null; wall = (Wall)getOneIntersectingObject(Wall.class); if (wall != null) { setLocation(getX()-4, getY()); } } else if ( Greenfoot.isKeyDown("A")) { setLocation(getX()-2,getY()); setRotation(90); Wall wall = null; wall = (Wall)getOneIntersectingObject(Wall.class); if (wall != null) { setLocation(getX()+4, getY()); } } } public void blink() { if(BlinkTime<300) { BlinkTime = BlinkTime + 1; } else if(BlinkTime >= 300) { BlinkTime = 0; } if(BlinkTime >=15 ) { setImage("Player.png"); } else { setImage("Player_Blink.png"); } } public void Score() { if(isTouching(Coin.class)) { int score = ((Level1)getWorld()).getscore(); score = score + 100; } } } Also sorry about my noobishness. this is only my first year coding in general so i might be slow. ill try my best though to keep up
Yehuda Yehuda

2018/5/10

#
See previous post.
Shade1177 Shade1177

2018/5/10

#
oh sorry didn't update to see danpost
Shade1177 Shade1177

2018/5/10

#
HO... thank you danpost!!! this does solve my first problem! THANKS. ahh, I have been troubling over this all day and it has frustrated me at every turn thank you for relieving me of this stress. although now I still have the multi-world problem... but ill come back to that once I finish a little more variables... ill prolly post that problem later if I cant solve it. (mostly cause I don't have any idea of how to do it), but as said later. for now ill keep working on the code. then again thanks danpost and yehuda for helping me. I'm glad I was able to get this off my back.
You need to login to post a reply.