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

2016/3/30

didn't reset ?

1
2
danpost danpost

2016/3/31

#
Line 19 creates a new MyWorld object which is not the world that this Brick object is in. To access the world this brick is in, use:
MyWorld world = (MyWorld)getWorld();
ryvetra ryvetra

2016/3/31

#
it still didn't increase
danpost danpost

2016/3/31

#
Also, line 20 calls a method that returns a value; yet, the value is not assigned to a variable, not used in any expressions and not used as a parameter in any method calls. As is, line 20 just wastes CPU-time.
Super_Hippo Super_Hippo

2016/3/31

#
Change line 20 in the Brick class to
accB = world.addSpeed(accB,"B");
You can remove line 22. If you wanted all Bricks to fall with the same speed, I thought about something like that though:
//In Brick in similar in the Mobil and Mobil2
public static int speed = 4;

public void act()
{
    setLocation(getX(), getY()+speed);
    if (getY() > getWorld().getHeight()-2) removeObject(this);
}
//in world subclass
private int time = 0;

public void act()
{
    time++;
    if (time == 100)
    {
        time = 0;
        Brick.speed += 4;
        Mobil.speed += 2;
        Mobil2.speed += 6;
    }
}
danpost danpost

2016/3/31

#
The way Super_Hippo suggested is close to what I was thinking. I would do it like this:
/**  in MyWorld class  */

// instance field
private int time;

// in constructor
SmoothMover.accLevel = 1;

// in act method or a method it calls
time = (time+1)%100; // cycles value from 0 to 99 repeatedly
if (time == 0) SmoothMover.accLevel++;

/** ******************** */
/**  in SmoothMover class  */

// class field
public static int accLevel;

// instance fields
public int acc;

// instance method
public void speed()
{
    setLocation(getX(), getY()+acc*accLevel);
}

/** ****************** */
/**   in Brick (and mobil classes) */

// constructor (Brick example)
public Brick()
{
    acc = 4;
}

// act method
public void act()
{
    speed();
    checkout("t");
}
ryvetra ryvetra

2016/3/31

#
@Super_Hippo it works , but the spped didn't reset again after greenfoot reset
ryvetra ryvetra

2016/3/31

#
@danpost it works and yhe speed reset :D, what the difference between yours code and Super_Hippo code????
danpost danpost

2016/3/31

#
ryvetra wrote...
@danpost it works and yhe speed reset :D, what the difference between yours code and Super_Hippo code????
Just the resetting of the static field in the constructor of the world class.
ryvetra ryvetra

2016/3/31

#
ah , thanks for the help... :)
You need to login to post a reply.
1
2