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

2020/7/9

random nu.

1
2
Roshan123 Roshan123

2020/7/9

#
i have rand limit 4 i want if a key is down then again i will get rand no. so how will i do that its not working properly will i have to show the code or not????
danpost danpost

2020/7/9

#
Roshan123 wrote...
will i have to show the code or not????
Always show attempted code (many times the entire class is required). It facilitates a faster, more accurate fix and allows other to learn as well.
Roshan123 Roshan123

2020/7/9

#
//world
//globally 
int rand =Greenfoot.getRandomNumber(2);
A a =new A();
B b = new B();
public void act()
{
 if(rand==0)
  {
addObject(a,1,1);
if(Greenfoot.isKeyDown("a"){
removeObject(a);
int rand =Greenfoot.getRandomNumber(2);
}}
if(rand==1)
  {
addObj(b,1,1);
if(Greenfoot.isKeyDown("b"){
removeObject(b);
int rand =Greenfoot.getRandomNumber(2);
}}
if(rand==2)
  {
addObj(c,1,1);
if(Greenfoot.isKeyDown("c"){
removeObject(c);
int rand =Greenfoot.getRandomNumber(2);
}
}}
danpost danpost

2020/7/9

#
You initially have, declared on line 3, a field (or, instance variable) called rand. Then, multiple times in your act method, you declare local variables with the same name (lines 13, 20 and 27). They are all different variables. Remove the declaring type, "int", from the beginning of those 3 lines. Also, rand will never be equal to 2 as you limit it to only 2 values (0 and 1). Generate a random of 3 possibles to include 2 as a possibility.
Roshan123 Roshan123

2020/7/9

#
so how will i write to get rand if any key is pressed condition:- it should limit upto 10
danpost danpost

2020/7/9

#
Roshan123 wrote...
so how will i write to get rand if any key is pressed condition:- it should limit upto 10
Try:
int rand;
Actor[] objs = { new A(), new B(), new C() };

public void act()
{
    String key = Greenfoot.getKey().toLowerCase();
    if (key != null)
    {
        int n = "abc".indexOf(key);
        if (n == rand)
        {
            removeObject(objs[rand]);
            started();
        }
    }
}

public void started()
{
    rand = Greenfoot.getRandomNumber(objs.length);
    addObject(objs[rand], 1, 1);
}
You should only need to insert more actors into the array on line 2.
FadedGray FadedGray

2020/7/9

#
Hello
FadedGray FadedGray

2020/7/9

#
I have a question. How to make enemies come out of a consistent place?
FadedGray FadedGray

2020/7/9

#
FadedGray wrote...
I have a question. How to make enemies come out of a consistent place?
Help? Pls?
danpost danpost

2020/7/9

#
FadedGray wrote...
I have a question. How to make enemies come out of a consistent place?
@FadedGray, start a new discussion for help with this.
actingdiva2.4 actingdiva2.4

2020/7/10

#
I really need help on how to make your other characters of the game to move without you moving them.
danpost danpost

2020/7/10

#
actingdiva2.4 wrote...
I really need help on how to make your other characters of the game to move without you moving them.
@actingdiva2.4, please start a new discussion thread for your issue. Include class codes of character with attempted code.
Roshan123 Roshan123

2020/7/10

#
danpost wrote...
Roshan123 wrote...
so how will i write to get rand if any key is pressed condition:- it should limit upto 10
Try:
int rand;
Actor[] objs = { new A(), new B(), new C() };

public void act()
{
    String key = Greenfoot.getKey().toLowerCase();
    if (key != null)
    {
        int n = "abc".indexOf(key);
        if (n == rand)
        {
            removeObject(objs[rand]);
            started();
        }
    }
}

public void started()
{
    rand = Greenfoot.getRandomNumber(objs.length);
    addObject(objs[rand], 1, 1);
}
You should only need to insert more actors into the array on line 2.
in line 6, its showing me the error "null pointer exception"
Roshan123 Roshan123

2020/7/10

#
while compiling its not showing the error but while i start the scenario its showing me the error
SteveB SteveB

2020/7/10

#
I'm totally new to Greenfoot, but I'm an experienced Java developer. If you read the spec on the getKey() call you'll see: public static java.lang.String getKey() Get the most recently pressed key, since the last time this method was called. If no key was pressed since this method was last called, it will return null. If more than one key was pressed, this returns only the most recently pressed key. So, your act() routine must have been called by something which wasn't a new keypress event. So, no key was pressed since the last call, and it returned null. You can't call toLowerCase() on null, thus you get the exception. So, move the toLowerCase() invocation inside your if statement so it won't get executed unless it's a non-null value. Good luck.
There are more replies on the next page.
1
2