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

2018/4/1

Setting a Variable to a Parameter??

Steve9719 Steve9719

2018/4/1

#
I created a variable called maxtime and added a paramter to my constructor called maxtime, now it's asking me to "Modify the Barrel constructor so that the maxtime class variable is set to the maxtime parameter. I tried to do it by setting maxtime = maxtime; but I'm stuck and I really have no idea what to do... @danpost please help! Here's my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import greenfoot.*;
 
public class Barrel extends Actor
{
 int timer;
 int maxtime;
 
 public void act()
 {
     resetBarrel();
 }
 public Barrel(int maxtime)
 {
    timer = 0;
    maxtime = 250;
    maxtime = maxtime;
 }
 public void resetBarrel()
 {
     timer++;
     if (timer > maxtime)
     {
       timer = 0;
       setLocation(Greenfoot.getRandomNumber(650)+70, Greenfoot.getRandomNumber(450)+70);
     }
 }
}
Vercility Vercility

2018/4/1

#
Rename the parameter to maxTime2
1
maxTime = maxTime2;
Or use this.maxTime
1
this.maxTime = maxTime;
Ure using 2 variables with identical names, Java will automatically use the 'closer' one, which is the parameter one in this case. U then set it to itself, effectively doing nothing.
Steve9719 Steve9719

2018/4/1

#
Hey thanks Vercility, I used
1
this.maxtime = maxtime
and I think it worked. My code isn't messing up anymore thankfully. Quick question though.. I have to modify the line above so that we subtract a random number between 0-49 from the maxtime. Any idea on how to subtract a random number?
danpost danpost

2018/4/1

#
Steve9719 wrote...
I have to modify the line above so that we subtract a random number between 0-49 from the maxtime. Any idea on how to subtract a random number?
Just subtract Greenfoot.getRandomNumber(50).
Steve9719 Steve9719

2018/4/1

#
It works perfectly, thanks guys!
You need to login to post a reply.