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

2012/10/8

Getting a random falling shell

1
2
3
4
-nic- -nic-

2012/10/8

#
I want to have a shell that falls to the ground when the shot is fired but im struggling to have a randomiser does any body have any idea please help
Game/maniac Game/maniac

2012/10/8

#
Greenfoot.getRandomNumber(a); a is the maximum the number can be and the minimum is automatically 0 to have a more specific threshhold use this:
int RandomNum = 0;
public void RandomNumber(int min, int max)
    {
        RandomNum = Greenfoot.getRandomNumber(max-min)+min;
    }
-nic- -nic-

2012/10/8

#
thanks i can get a random number but i cant implament this into a falling shell
Game/maniac Game/maniac

2012/10/8

#
What is your code for the class
-nic- -nic-

2012/10/9

#
this is the called method
public void fire()
    {
        Bullet b = new Bullet();
        getWorld().addObject(b,getX(),getY());
        //shell s= new shell();
        //getWorld().addObject(s,getX(),getY());
        b.setRotation(getRotation());
        b.move(20);
    }
and ive got nothing in the shell.class im really stumped please help
danpost danpost

2012/10/9

#
If we have nothing to work with, it would be difficult to help. Create the class, try to get something working; then, if you find yourself still at a loss, post what you have and ask for help. The code would give us a better idea of what you are trying to do (programatically).
-nic- -nic-

2012/10/9

#
ok sorry
danpost danpost

2012/10/9

#
You probably are using top-view. The range of shell dispersal is probably something like 'getRotation() + 120 + Greenfoot.getRandomNumber(30)' (from within the gun class). 'getRotation()' must be part of what is sent to the constructor of the Shell class object; the addition computational parts can be done before or after the call to the constructor. However, all this is just a guess (as to what you need)!
-nic- -nic-

2012/10/9

#
im using
public void act() 
    {move(1);
       
    }  
    public void fall()
    {
        int r=Greenfoot.getRandomNumber(100)-50;
        turnTowards(r+70,600);
        
        
    }
-nic- -nic-

2012/10/9

#
but 9 time out of ten it falls stright down and somtime it falls at a 45 degree angel
danpost danpost

2012/10/9

#
When moving one pixel at a time, the only way you can move is in an angle that is a multiple of 45 degrees. You probably need to control the location coordinates with doubles and round them to ints when setting the locations. You may have to create a move method that does the controlling of the movement (adjusts the angle, determines the x and y distances to move depending on the angle and speed, adds the distances to its saved current location as doubles, and sets the new location).
danpost danpost

2012/10/9

#
I do not know if you really want to 'turnTowards' a random location each act cycle. Your shells will not fall normally -- changing directions in mid-air. You probably want to set an initial location to 'turnTowards' and either just continue moving in that direction or allow gravity to come into play and having the shell fall in more of a real-life like manner. If using gravity, you will probably want to control the rotation within a double field also.
-nic- -nic-

2012/10/9

#
the fire method is called only once
danpost danpost

2012/10/9

#
You mean: the fall() method?
-nic- -nic-

2012/10/9

#
yes sorry
There are more replies on the next page.
1
2
3
4