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

2016/3/30

didn't reset ?

1
2
ryvetra ryvetra

2016/3/30

#
so i make class where time >= 1000 , the move speed get increase. but after i reset the greenfoot , the move speed still at the increase time , not back to normal .
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class SmoothMover here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class SmoothMover extends Actor
{
    /**
     * Act - do whatever the SmoothMover wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    private static int accB = 4;
    private static int accM = 2;
    private static int accM2 = 6;
    int time;
    private int scoreTime =0;
    public void speedBrick() 
    {
        addSpeed();
        setLocation(getX(), getY()+accB);
        return;
    }

    public void speedMobil() 
    {
        addSpeed();
        setLocation(getX(), getY()+accM);
        return;
    }

    public void speedMobil2() 
    {
        addSpeed();
        setLocation(getX(), getY()+accM2);
        return;
    }

    public void addSpeed()
    {
        if(time >= 1500)
        {
            accB = accB+ 2;
            accM = accM+ 1;
            accM2 = accM2+3;
            time = 0 ;
            Greenfoot.playSound("addSpeed.wav");
        }
        time++;
    }


    public void checkout(String verify)
    {
        if (getWorld() != null && (this.atWorldEdge()) ) {
            if(verify.equalsIgnoreCase("t")){
                //getWorld().addObject(new Brick(), 13, 5);
                getWorld().addObject(new Brick(), 150, 7); 
                getWorld().removeObject(this);
                return;

            }
            else{                           
                //getWorld().addObject(new Brick(), 13, 5);
                getWorld().removeObject(this);
                return;

            }
        }}

    public boolean atWorldEdge()
    {
        if(getWorld() != null && (getX() < 5 || getX() > getWorld().getWidth() - 5))
            return true;            
        if(getWorld() != null && (getY() < 5 || getY() > getWorld().getHeight() - 5))
            return true;
        else
            return false;
    }
}    
Super_Hippo Super_Hippo

2016/3/30

#
static variables are created when the scenario gets compiled. Not when you reset it. Avoid using static variables if they don't have to be static. If it really needs to be static, reset them in the constructor of the world in which the game starts.
ryvetra ryvetra

2016/3/30

#
What code(condition) i must write on constructor world ? If i put the reset on constructor , then it will reset continously ?????
danpost danpost

2016/3/30

#
Let me explain it this way: Your fields 'accB', 'accM' and 'accM2' are to be applied to each SmoothMover object created. You want each object to have its own value for these fields. Hence, they need to be 'instance fields' (fields for each instance of the class). If you make these fields 'static', they cannot each have unique values for the fields. In other words, as 'static', one field will be used for all objects of the class; as 'non-static' each object will have a unique field for itself.
ryvetra ryvetra

2016/3/30

#
So ... what must i do then ? I'm still don't understans
Super_Hippo Super_Hippo

2016/3/30

#
Remove the word 'static' in lines 15, 16 and 17.
ryvetra ryvetra

2016/3/30

#
If i do that , the speed somehow ruined... The first code i use , didn'nt use static on it
Super_Hippo Super_Hippo

2016/3/30

#
Why did you start using static variables?
ryvetra ryvetra

2016/3/30

#
Like the B , M , and M2 speed was increse when it its y = 349( or around that) ( the B , and other created on up edge and moce ti down). But before th3 y , the speed was normal
ryvetra ryvetra

2016/3/30

#
When i use non static , the speed was increase just at time wheee its pass the y coordinte(349), before pass , the speed waa normal
Super_Hippo Super_Hippo

2016/3/30

#
You shouldn't have these methods and variables in the SmoothMover class anyways. You probably have subclasses in which this code should go. As it is now, every SmoothMover class which has a call to the addSpeed-method will increase the speed of all SmoothMover classes (because the variables are static) when it is in the world for 1500 act-cycles. I don't think that this is what you want.
ryvetra ryvetra

2016/3/30

#
Yeah i have the subclasses. I just want the 3 class get speed at certain cycle time(every n econd)
Super_Hippo Super_Hippo

2016/3/30

#
You should use the addSpeed-method in the world subclass then. Then it doesn't matter how many objects of the subclasses of SmoothMover are in the world. It will just increase after a given time.
danpost danpost

2016/3/30

#
If all your Brick, Mobil and Mobil2 type actors are to all increase speed at the same time, then you should probably have the 'time' and a 'level' field in your world class and have a base 'speed' field in each of these actor classes. The speed field can actually be placed in the SmoothMover class and the actors classes can each assign its own value to it; but the field cannot be declared as 'static' as each will have a different value for it. The 'level' field in the world class can be made 'static'; but it must be reset in the world constructor. Then your actors can use the product of its base speed and the level number to determine the current speed. Coding should be much easier also, as everything is consistent among those actor classes. And yes, the world act method can run the timer and increment the level when necessary.
ryvetra ryvetra

2016/3/31

#
it didn't get the speed tho... is it wrong code or wrong way?( sorry for the late reply) world class
 private int time;

public int addSpeed(int speed, String verify)
    {
        if(time >= 100)
        {
            if(verify.equalsIgnoreCase("B")){
            speed = speed + 4;}
            else if(verify.equalsIgnoreCase("M")){
            speed = speed + 2;}
            else if(verify.equalsIgnoreCase("M2")){
            speed = speed + 6;}
            //Greenfoot.playSound("addSpeed.wav");
            time = 0;
        }
        time++;
        return speed;
    }
Brick/Mobil/Mobil2 ( same code)
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Brick here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Brick extends SmoothMover
{
    private  int accB = 4;
    public void act() 
    {        
           speedBrick();
           checkout("t");
    }
    public void speedBrick() 
    {
        MyWorld world = new MyWorld();
        world.addSpeed(accB,"B");
        setLocation(getX(), getY()+accB);
        return;
    }
    }    
There are more replies on the next page.
1
2