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/8

#
For each image (and you must do both the same to keep proportion):
GreenfootImage[] images = { image1, image2 };
int scalePct = 50; // half sizing (50%)
for (GreenfootImage img : images) img.scale(img.getWidth()*scalePct/100, img.getHeight()*scalePct/100);
Izzmemitch Izzmemitch

2017/1/8

#
if i try this code it says it doesnt recognise image1 and image2, so i replaced them with what i called the 2 images but it still doesnt recognise them
Izzmemitch Izzmemitch

2017/1/8

#
it says cannot find symbol - variable image 1 or image 2
danpost danpost

2017/1/8

#
Izzmemitch wrote...
it says cannot find symbol - variable image 1 or image 2
You are supposed to reference your two images with 'image1' and 'image2' (either replace them with references to your images or declare those variables and assign your images to them).
Izzmemitch Izzmemitch

2017/1/8

#
public MG2Arrow() { GreenfootImage images = { MG2Arrow, MG2Meter }; int scalePct = 50; // half sizing (50%) for (GreenfootImage img : images) img.scale(img.getWidth()*scalePct/100, img.getHeight()*scalePct/100); }
danpost danpost

2017/1/8

#
Looks good -- provided you have GreenfootImage reference fields called 'MG2Arrow' and 'MG2Meter' that are already assigned the appropriate images.
Izzmemitch Izzmemitch

2017/1/8

#
im definitely doing something wrong here it still gives me the cannot find symbol - variable MG2Arrow public class MG2Arrow extends Actor { public MG2Arrow() { GreenfootImage images = { MG2Arrow, MG2Meter }; int scalePct = 50; // half sizing (50%) for (GreenfootImage img : images) img.scale(img.getWidth()*scalePct/100, img.getHeight()*scalePct/100); } this one is in the MG2Meter class
danpost danpost

2017/1/8

#
Maybe you did not recall what I said earlier:
danpost wrote...
there will be two images needed; one for the background of the gauge and the other for the level part (the moving part). That does not mean two actors are needed; just that the gauge actor will require both each time it
Actually, it might be best to have two actors; but only one class for the Meter (for the gauge and the arrow). I will explan in my next post.
Izzmemitch Izzmemitch

2017/1/8

#
so two images in one class?
danpost danpost

2017/1/8

#
Both images should be in the same class, call it the LevelGauge class. The image of the LevelGauge class will be your Mg2Meter image. Then, this class will create an Mg2Arrow object when it is created and add it into the same world the meter is added into when it is added into a world. Alternatively, and this might actually be the better order, is to have the arrow image for the LevelGauge class and have the MG2Meter image for the actor it creates and adds to the world when it is added into a world. The background (or meter image) itself is not what is important; but, the rotation of the arrow is. However, the background and the arrow both make up the actual LevelMeter object and should be contained in one class Therefore:
import greenfoot.*;

public class LevelMeter extends Actor
{
    GreenfootImage GM2Arrow = new GreenfootImage("arrow.png"); // adjust name as needed
    GreenfootImage GM2Meter = new GreenfootImage("meter.png"); // adjust name as needed
    int balance;
    
    public LevelMeter()
    {
        GreenfootImage[] images = { GM2Meter, GM2Arrow };
        for (GreenfootImage img : images) 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, getX(), getY());
    }

    public void setBalance(int balance)
    {
        if (balance*15 > getRotation()) turn(1);
        if (balance*15 < getRotation()) turn(-1);
    }

    private class Gauge extends Actor { }
}
You should keep a reference to the LevelMeter object in your World subclass:
private LevelMeter levelMeter = new LevelMeter();

// in constructor
addObject(levelMeter, someX, someY);

// public getter method
public LevelMeter getLevelMeter()
{
    return levelMeter;
}
Then, when ship has a container added or removed, it can access the meter and tell it the new balance.
Izzmemitch Izzmemitch

2017/1/8

#
i think i got it up the right scale now with this code though for each class the MG2Meter and the MG2Arrow public MG2Meter() { GreenfootImage myImage = getImage (); int myNewHeight = (int)myImage.getHeight()*3/4; int myNewWidth = (int)myImage.getWidth()*3/4; myImage.scale(myNewWidth, myNewHeight); } public MG2Arrow() { GreenfootImage myImage = getImage (); int myNewHeight = (int)myImage.getHeight()/2; int myNewWidth = (int)myImage.getWidth()/2; myImage.scale(myNewWidth, myNewHeight); }
Izzmemitch Izzmemitch

2017/1/8

#
owww okay wow need to adjust this then to what you just posted
danpost danpost

2017/1/8

#
Replace lines 23 through 27 above with this:
public void setBalance(int bal)
{
    balance = bal;
}

public void act()
{
    if (balance*15 > getRotation()) turn(1);
    if (balance*15 < getRotation()) turn(-1);
}
danpost danpost

2017/1/8

#
You can replace lines 11 and 12 with the following:
GreenfootImage img = MG2Meter;
img.scale(img.getWidth()*3/4, img.getHeight()*3/4):
img = MG2Arrow;
img.scale(img.getWidth()/2, img.getHeight()/2);
Izzmemitch Izzmemitch

2017/1/8

#
Wow, im very thankful for this, looking forward to when im able to help people the way you're helping me!
There are more replies on the next page.
1
2
3
4