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

2017/1/5

Object out of balance

1
2
3
4
danpost danpost

2017/1/9

#
Izzmemitch wrote...
So I Figure that the formula goes here
No. The only thing that method is to do is set the new value to the 'balance' field of the class.
what is the full formula for this balance of the Ship object
The 'addContainer' and 'removeContainer' methods of the Ship class is where you will be using the formula. I suggested this for the formula.
danpost wrote...
You can use the formula 'n = (i*2)-3', where 'n' is the adjustment whose value will be in the set { -3, -1, 1, 3 } depending on where the container is placed; or '-n' if removed.
Izzmemitch Izzmemitch

2017/1/9

#
https://www.dropbox.com/s/hp7xyvgndvtox5p/Screen%20Shot%202017-01-09%20at%2020.40.55.png?dl=0 just to show you what the code looks like i uploaded these 2 pics above. ------------------------------- the problem: if i put the word balance between the ( ). it then says it cant find symbol - variable n, so i replaced balance with int bal. ((MiniGame2World)getWorld()).getLevelGauge().setBalance(int bal); n = (i*2)-3; instead of ((MiniGame2World)getWorld()).getLevelGauge().setBalance(balance); n = (i*2)-3; int bal is to be found in my levelMeter class as: public void setBalance(int bal) { balance = bal; } so im thinking that this would solve the problem since it would find the code setBalance(int bal), but that isnt true. the only problem with (int bal) it gives is that it says '.class' expected. so that means its asking for a class that isnt there! How can i fix this ----------------------------------
danpost danpost

2017/1/9

#
I gave you the appropriate line of code to adjust the balance in the LevelMeter class from the Ship class -- namely:
((MiniGame2World)getWorld()).getLevelMeter().setBalance(balance);
and I gave you the amount of adjustment needed for balance when a containeer is added or removed from the ship -- namely: +/- (plus in the add method and minus in the remove method) (i*2)-3 where 'i' is the column the container change occurred at starting with column zero. This adjustment to 'balance' must be made before updating the LevelMeter object with 'setBalance'.
Izzmemitch Izzmemitch

2017/1/10

#
i think i understand it better now it because earlier i didnt put in int balance; at the beginning of the class like so: (i underlined beneath what im talking about) My only issue now is that the n in n = (i*2)-3, says that it cant find the variable n. must i also put a piece of code at the beginning of the class to make it work, i'll make underline beneath what im talkin about { private MG2Container containers; private int startX = 310; private int startY = 625; int balance; n = adjustment; public MG2Ship(World world) { this.containers = new MG2Container; for(int i= 0; i < 4; i++){ for(int i2=0; i2 < 5; i2++){ if (i2 >= 3) { this.containers = null; } else { MG2Container container = new MG2Container(false, null, (MiniGame2World)world); this.containers = container; world.addObject(container, this.startX + (i*100), this.startY - (i2*66)); } } } } /** * Act - do whatever the MG2Ship wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { // Add your action code here. } public int getStartX() { return this.startX; } public int getStartY() { return this.startY; } public MG2Container getRandomContainer() { MG2Container allContainers = new MG2Container; int currentIndex = 0; for(int i=0; i < this.containers.length; i++){ for(int i2=0; i2 < this.containers.length; i2++){ if (this.containers != null) { allContainers = this.containers; currentIndex++; } } } if (currentIndex > 0) { int randomIndex = Greenfoot.getRandomNumber(currentIndex); return allContainers; } return null; } public void removeContainer(MG2Container container) { for(int i=0; i < this.containers.length; i++){ for(int i2=0; i2 < this.containers.length; i2++){ if (this.containers != null && container == this.containers) { this.containers = null; break; } } ((MiniGame2World)getWorld()).getLevelGauge().setBalance(balance); n = (i*2)-3; } } public boolean spaceAtRow(int i) { int containersInRow = 0; for(int i2=0; i2 < this.containers.length; i2++){ if (this.containers != null) { containersInRow++; } } if (containersInRow == 5) { return false; } return true; } public void addContainer(int i, MG2Container container) { int containersInRow = 0; for(int i2=0; i2 < this.containers.length; i2++){ if (this.containers != null) { containersInRow++; } } this.containers = container; container.setLocation(this.startX + (i*100), this.startY - (containersInRow*66)); ((MiniGame2World)getWorld()).getLevelGauge().setBalance(balance); n = (i*2)+3; } public boolean isLastContainerInColumn(MG2Container container) { for(int i=0; i < this.containers.length; i++){ boolean foundContainer = false; int myIndex = 0; int lastIndex = 0; for(int i2=0; i2 < this.containers.length; i2++){ if (this.containers == container) { foundContainer = true; myIndex = i2; } if (this.containers != null) { lastIndex = i2; } } if(foundContainer && myIndex == lastIndex) { return true; } } return false; } }
danpost danpost

2017/1/10

#
Izzmemitch wrote...
i think i understand it better now it because earlier i didnt put in int balance; at the beginning of the class
Now we are getting somewhere.
My only issue now is that the n in n = (i*2)-3, says that it cant find the variable n.
When I say 'n' represents the adjustment, I do not mean to create a field for it -- I mean for you to use the equated expression for it; just like 'i' represents the column number of the container -- in 'addContainer', 'i' from the parameter represents the column number of the container; in 'removeContainer, the column number is already represented by 'i2' from the 'for' loop. Remove 'n = adjustment;' from the code. In 'addContainer', add the expression to the 'balance' field; and, in the 'removeContainer, subtract the expression from the 'balance' field (since the expression represents the value of the adjustment to the balance).
Izzmemitch Izzmemitch

2017/1/10

#
Where would that balance field be? } public void removeContainer(MG2Container container) { for(int i=0; i < this.containers.length; i++){ for(int i2=0; i2 < this.containers.length; i2++){ if (this.containers != null && container == this.containers) { this.containers = null; break; } ((MiniGame2World)getWorld()).getLevelGauge().setBalance(balance); } public void addContainer(int i, MG2Container container) { int containersInRow = 0; for(int i2=0; i2 < this.containers.length; i2++){ if (this.containers != null) { containersInRow++; here ---> or } here --> or } this.containers = container; container.setLocation(this.startX + (i*100), this.startY - (containersInRow*66)); ((MiniGame2World)getWorld()).getLevelGauge().setBalance(balance); n = (i*2)+3; here --> } sorry im a bit of a padawan here!
danpost danpost

2017/1/10

#
Izzmemitch wrote...
Where would that balance field be?
It would go along with you adding or removing the container from your array. Adjust the balance first; then, affect the level meter. For example (for in 'addContainer':
balance = balance+(2*i-3);
((MiniGame2World)getWorld()).getLevelGauge().setBalance(balance);
Izzmemitch Izzmemitch

2017/1/12

#
hey Dan, so i been following as much as possible what you said and i still dont see any movement. im gonna paste the code for LevelGauge class, the MG2Ship class and the MiniGame2 class to see if i did something wrong, pls help cause i have to present the game on monday. LevelGauge Class Code import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class LevelGauge here. * * @author (izzmemitch) * @version (January 2017) */ public class LevelGauge extends Actor { GreenfootImage MG2Arrow = new GreenfootImage("MG2Arrow.png"); // adjust name as needed GreenfootImage MG2Meter = new GreenfootImage("MG2Meter.png"); // adjust name as needed int balance; public LevelGauge() { GreenfootImage img = MG2Meter; img.scale(img.getWidth()*3/4, img.getHeight()*3/4); img = MG2Arrow; img.scale(img.getWidth()/2, img.getHeight()/2); setImage(MG2Arrow); } public void addedToWorld(World world) { Gauge gauge = new Gauge(); gauge.setImage(MG2Meter); world.addObject(gauge, 390,305); } public void setBalance(int balance) { balance = balance; } /** * Act - do whatever the LevelGauge wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { if (balance*30 > getRotation()) turn(1); if (balance*30 < getRotation()) turn(-1); // Add your action code here. } private class Gauge extends Actor { } }
Izzmemitch Izzmemitch

2017/1/12

#
MiniGame2World Class Code import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import javax.swing.*; /** * Write a description of class MiniGame2World here. * * @author (your name) * @version (a version number or a date) */ public class MiniGame2World extends World { GameState globalGameState; MG2Ship ship; /** * Constructor for objects of class MiniGame2World. * */ public MiniGame2World() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(1280, 720, 1, false); prepare(); } public void setGameState(GameState gamestate) { this.globalGameState = gamestate; } public GameState getGameState() { return this.globalGameState; } private void prepare() { MG2Quay quay = new MG2Quay(); addObject(quay, 640,680); MG2Train train = new MG2Train(); addObject(train,1100,620); this.ship = new MG2Ship(this); addObject(ship,400,615); MG2Hook hook = new MG2Hook(); addObject(hook,450,-250); LevelGauge gauge = new LevelGauge(); addObject(gauge, 390, 350); } private LevelGauge levelGauge = new LevelGauge(); // public getter method public LevelGauge getLevelGauge() { return levelGauge; } public void act() { buttonPressed(); } public void buttonPressed() { if(Greenfoot.isKeyDown("escape")) { int n = JOptionPane.showConfirmDialog(null, "Do you really want to exit to the main menu?", "Exit", JOptionPane.YES_NO_OPTION); if (n == 0) { MainMenuWorld newWorld = new MainMenuWorld(); newWorld.setGameState(this.getGameState()); Greenfoot.setWorld(newWorld); } } } public MG2Ship getShip() { return this.ship; } public void win() { int n = JOptionPane.showConfirmDialog(null, "You won! Do you want to go to minigame 3?", "Winner!", JOptionPane.YES_NO_OPTION); this.getGameState().setFinished(2, 0); if (n == 0) { MiniGame3World newWorld = new MiniGame3World(); newWorld.setGameState(this.getGameState()); Greenfoot.setWorld(newWorld); } else { MainMenuWorld newWorld = new MainMenuWorld(); newWorld.setGameState(this.getGameState()); Greenfoot.setWorld(newWorld); } } }
Izzmemitch Izzmemitch

2017/1/12

#
MG2Ship Class Code import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class MG2Ship here. * * @author (your name) * @version (a version number or a date) */ public class MG2Ship extends Actor { private MG2Container containers; private int startX = 310; private int startY = 625; int balance; public MG2Ship(World world) { this.containers = new MG2Container; for(int i= 0; i < 4; i++){ for(int i2=0; i2 < 5; i2++){ if (i2 >= 3) { this.containers = null; } else { MG2Container container = new MG2Container(false, null, (MiniGame2World)world); this.containers = container; world.addObject(container, this.startX + (i*100), this.startY - (i2*66)); } } } } /** * Act - do whatever the MG2Ship wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { // Add your action code here. } public int getStartX() { return this.startX; } public int getStartY() { return this.startY; } public MG2Container getRandomContainer() { MG2Container allContainers = new MG2Container; int currentIndex = 0; for(int i=0; i < this.containers.length; i++){ for(int i2=0; i2 < this.containers.length; i2++){ if (this.containers != null) { allContainers = this.containers; currentIndex++; } } } if (currentIndex > 0) { int randomIndex = Greenfoot.getRandomNumber(currentIndex); return allContainers; } return null; } public void removeContainer(MG2Container container) { for(int i=0; i < this.containers.length; i++){ for(int i2=0; i2 < this.containers.length; i2++){ if (this.containers != null && container == this.containers) { this.containers = null; break; } ((MiniGame2World)getWorld()).getLevelGauge().setBalance(balance); balance = balance+(2*i-3); } } } public boolean spaceAtRow(int i) { int containersInRow = 0; for(int i2=0; i2 < this.containers.length; i2++){ if (this.containers != null) { containersInRow++; } } if (containersInRow == 5) { return false; } return true; } public void addContainer(int i, MG2Container container) { int containersInRow = 0; for(int i2=0; i2 < this.containers.length; i2++){ if (this.containers != null) { containersInRow++; } } this.containers = container; container.setLocation(this.startX + (i*100), this.startY - (containersInRow*66)); ((MiniGame2World)getWorld()).getLevelGauge().setBalance(balance); balance = balance+(2*i+3); } public boolean isLastContainerInColumn(MG2Container container) { for(int i=0; i < this.containers.length; i++){ boolean foundContainer = false; int myIndex = 0; int lastIndex = 0; for(int i2=0; i2 < this.containers.length; i2++){ if (this.containers == container) { foundContainer = true; myIndex = i2; } if (this.containers != null) { lastIndex = i2; } } if(foundContainer && myIndex == lastIndex) { return true; } } return false; } }
danpost danpost

2017/1/12

#
Change 'LevelGauge gauge' in the prepare method to 'this.levelGauge' -- or, just drop that line and change 'gauge' in the next line to 'levelGauge' as a LevelGauge object is already assigned to the 'levelGauge' field. In the 'buttonPressed' and in the 'win' methods, 'n == 0' should read 'n = JOptionPane.YES_OPTION' for better readability. The first change above may do something about your gauge not moving.
You need to login to post a reply.
1
2
3
4