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

2013/7/23

in game speed bar

darkmist002 darkmist002

2013/7/23

#
ok, i did as danpost had suggested before about creating a bar in the game so the player could still control the speed if they wanted to get faster or slower, but it isn't controlling it properly. if i click to the right of the halfway point, it speeds it up to an extent, and then stops or starts slowing back down. the same applies to the left point, it slows down to an extent, then starts speeding back up again. doesn't seem to make it go to the speed i want it where i click it. here's my speed bar coding:
public class actBar extends Actor
{
    /**
     * Act - do whatever the actBar wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    GreenfootImage bar = new GreenfootImage(200, 20);
    int X, xX;
    boolean barLess = false;
    boolean barMore = false;
    int spd = 0;
    public actBar()
    {
        bar.setColor(new Color(255, 255, 255));
        bar.fill();
        bar.drawRect(0, 0, 200, 20);
        bar.setColor(new Color(0, 0, 0));
        bar.drawRect(5, 5, 190, 10);
        bar.fillRect(5, 5, 190, 10);
        setImage(bar);
        Greenfoot.setSpeed(50);
    }
    
    public void act() 
    {
        // Add your action code here.
        if(Greenfoot.mouseClicked(this))
        {
            MouseInfo mouse = Greenfoot.getMouseInfo();
            X = mouse.getX();
            if(X > 750 && X < (750 + (190/2)))//750 is the halfway point of the picture
            {
                X = X - 750;
                barMore = true;
                barLess = false;
                //increase speed
                //Greenfoot.setSpeed(50 + X);
                spd = X;
                adjustSpd(spd);
            }
            if(X < 750 && X > (750 - (190/2)))
            {
                X = 750 - X;
                barLess = true;
                barMore = false;
                //decrease speed
                //Greenfoot.setSpeed(50 - X);
                spd = X;
                adjustSpd(spd);
            }
            if(X == 750)
            {
                Greenfoot.setSpeed(50);
            }
        }
        barUpdate(X);
    }  
    
    public void barUpdate(int x)
    {
        bar.clear();
        bar.setColor(new Color(255, 255, 255));
        bar.fill();
        bar.setColor(new Color(0, 0, 0));
        bar.drawRect(5, 5, 190, 10);
        bar.fillRect(5, 5, 190, 10);
        bar.setColor(new Color(255, 0, 0));
        bar.drawRect(5, 5, 190, 10);
        if(barLess)
        {
            bar.fillRect(5, 5, 95 - X, 10);
        }
        if(barMore)
        {
            bar.fillRect(5, 5, 95 + X, 10);
        }
        if(!barLess && !barMore)
        {
            bar.fillRect(5, 5, 95, 10);
        }
        setImage(bar);
    }
    
    public void adjustSpd(int spd)
    {
        if(spd >= 1)
        {
            Greenfoot.setSpeed(50 + spd);
        }
        if(spd <= -1)
        {
            Greenfoot.setSpeed(50 - spd);
        }
    }
}
danpost danpost

2013/7/23

#
Why are you referencing the halfway point of the picture (750)? When there is a mouse click on the actbar object, you should get the location of the mouse and compare it to 'getX()-getImage().getWidth()+5' (world x-coordinate of the left edge of the color portion of the bar). The code would be something like this:
if (Greenfoot.mouseClicked(this))
{
    MouseInfo mouse = Greenfoot.getMouseInfo();
    int percent = (mouse.getX()-(getX()-getImage().getWidth()+5))*100/190;
    if (percent > 0 && percent<= 100)
    {
        Greenfoot.setSpeed(percent);
        getImage().setColor(Color.white);
        getImage().fillRect(5, 5, 190, 10);
        getImage().setColor(Color.red);
        getImage().fillRect(5, 5, percent*190/100, 10);
    }
}
darkmist002 darkmist002

2013/7/24

#
i had used the reference point because it was out at that coordinate. doing your way doesn't work. if you do the calculations using the halfway point 750, you get: 750 - (750-190+5)= 185 185*100/190 = 97 using another number does the same, so it never changes. that's why i was using the halfway point as a reference, so i could subtract 750 from it if it was above the halfway point, and subtract the X coordinate if it was below. EDIT: i tried to use what you gave but put it in a little differently which made sense math wise, but it still isn't working.
X = mouse.getX();
            if(X > 655 && X <= 845)
            {
                percent = ((X - 655)/190)*100;
                Greenfoot.setSpeed(percent);
                getImage().setColor(Color.WHITE);
                getImage().fillRect(5, 5, 190, 10);
                getImage().setColor(new Color(255, 0, 0));
                getImage().fillRect(5, 5, (percent*190/100), 10);
            }
danpost danpost

2013/7/24

#
Sorry, line 3 should have been:
int percent = (mouse.getX()-(getX()-getImage().getWidth()/2+5))*100/190;
The value of 'percent' will be between 0 and 100 inclusive if the click was performed within the bar itself. The actbar and speed can be adjusted if the value is between 1 and 100. Multiplying this 'percent' value by 190 and dividing by 100 will give the new length of the color bar.
darkmist002 darkmist002

2013/7/24

#
ok, i found what i was inputting wrong, i was putting in X instead of getX() X = mouse.getX(), percent = (X - (X - getImage().getWidth()/2 +5))*100/190 when i should've been putting it like what you had it: X = mouse.getX(), percent = (X - (getX() - getImage().getWidth()/2 +5))*100/190 that and putting /2 in there made it work, thank you. still not sure why the way i thought would work (that did work math wise) didn't work for it. but at least i have a way of getting the speedbar in game now which still makes it where the actors are locked in place and can't be moved when the game is paused.
danpost danpost

2013/7/24

#
When using 'int' values like this, it is wise to multiply before dividing. If you divide first, your result will probably be off due to rounding. With the 'percent' value in your bar, if you divide by 190 first, you will be multiplying 100 by 0 or 1 and the bar will end up empty or full. If you multiply by 100 first, then no rounding will occur before the final division.
darkmist002 darkmist002

2013/7/24

#
ok, it works if i add the if condition for it to be greater than 0 and less than 100 the other way as well. what you said makes since, although i originally thought it would wait until the math was completely done with the calculations before it rounded it. guess after trying it in greenfoot it makes since if you say it rounds it on each calculation.
darkmist002 darkmist002

2013/7/24

#
i wonder how long it will be before i can program stuff like this without having trouble and without having to ask for so much help. been programming with school for a couple of years now, and i'm still asking for help a good bit of the time. not as much as when i first started, but still a lot more than i want to.
You need to login to post a reply.