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
Izzmemitch Izzmemitch

2017/1/8

#
THis all goes into the world class right? private LevelMeter levelMeter = new LevelMeter(); // in constructor addObject(levelMeter, someX, someY); // public getter method public LevelMeter getLevelMeter() { return levelMeter; }
Izzmemitch Izzmemitch

2017/1/8

#
this is the code off the MiniGame2... which makes up the MINIGame2World..and do i insert the code there where i made the text bold and italic? 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); private LevelMeter levelMeter = new LevelMeter(); // in constructor addObject(levelMeter, someX, someY); // public getter method public LevelMeter getLevelMeter() { return levelMeter; } } 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); } } }
danpost danpost

2017/1/8

#
Izzmemitch wrote...
THis all goes into the world class right?
Yes.
do i insert the code there where i made the text bold and italic? < Code Omitted >
Only the 'addObject' line goes there in the 'prepare' method. The rest goes inside the class but outside any existing methods or constructors. You will have to replace 'someX' and 'someY' in the addObject line with valid coordinate values.
Izzmemitch Izzmemitch

2017/1/8

#
1.how can i adjust the space between arrow and gauge? 2.so what do i do to get the arrow moving if i remove a container? i thought it would move with the code: public void act() { if (balance*15 > getRotation()) turn(1); if (balance*15 < getRotation()) turn(-1); }
Izzmemitch Izzmemitch

2017/1/8

#
https://www.dropbox.com/s/h22hp0dbfo9oqnt/MIniGame2%20Level%20Gauge.jpeg?dl=0
Izzmemitch Izzmemitch

2017/1/8

#
is what it looks like now
Izzmemitch Izzmemitch

2017/1/8

#
https://youtu.be/HP7OaaPVF00
danpost danpost

2017/1/8

#
Izzmemitch wrote...
how can i adjust the space between arrow and gauge?
Add or subtract some from the 'getY' part of the 'world.addObject' statement in the 'addedToWorld' method of the LevelMeter class.
what do i do to get the arrow moving if i remove a container?
You need to call the 'setBalance' method of the LevelMeter class from the Ship class when a container is added or removed. For example:
((MiniGame2World)getWorld()).getLevelMeter().setBalance(balance);
Izzmemitch Izzmemitch

2017/1/9

#
okay so i got the adjusting down!! now for the calling of setBalance how do i apply it so that it works? i've put it in the LevelMeter class under the public void setBalanance method but it didnt work, then i tried it under the public void Act no success. i dont see the connections yet therefore the question i ask.
danpost danpost

2017/1/9

#
Izzmemitch wrote...
now for the calling of setBalance how do i apply it so that it works? i've put it in the LevelMeter class under the public void setBalanance method but it didnt work, then i tried it under the public void Act no success.
No need for a 'setBalanance' (or 'setBalance') method in the LevelMeter class; and, it does not go in the act method itself. The last line I gave you goes at the end of both the 'addContainer' and 'removeContainer' methods.
Izzmemitch Izzmemitch

2017/1/9

#
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); } i inserted it at the end of the code both the addcontainer and removecontainer and it gives me a cannot find symbol - variable balance error.
danpost danpost

2017/1/9

#
danpost wrote...
Now, you will need a field to track the weight distribution of the containers. Its value can be kept updated if you only use the methods addContainer and removeContainer to add and remove the containers (including the initial ones being added) by adjusting the value of the field within those methods. 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.
The above, earlier in this discussion, was about the balance state of the Ship object.
Izzmemitch Izzmemitch

2017/1/9

#
ok so this balance state is to be applied to the LevelMeter?
danpost danpost

2017/1/9

#
Izzmemitch wrote...
ok so this balance state is to be applied to the LevelMeter?
It is the balance of the Ship object that the meter is to display -- yes.
Izzmemitch Izzmemitch

2017/1/9

#
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 bal) { balance = bal; So I Figure that the formula goes here what is the full formula for this balance of the Ship object } /** * 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*15 > getRotation()) turn(1); if (balance*15 < getRotation()) turn(-1); // Add your action code here. }
There are more replies on the next page.
1
2
3
4