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

2014/10/4

Bouncing opposite direction when the ball hits the worlds edge.

1
2
chamuzi3 chamuzi3

2014/10/4

#
How can i code the ball to bounce off the worlds edge when it hits either the top,bottom right or left edge?
danpost danpost

2014/10/4

#
That depends on what movement code you already have. Show your code. Use 'code' link below the 'Post a reply' input box to insert any code within your posts.
chamuzi3 chamuzi3

2014/10/4

#
when the ball hits the right or left edge, it gets stack.please help
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public void act()  
   
       move(5); 
       checkWalls(); 
   }     
  
   public void checkWalls(){ 
       if(atWorldEdge()){ 
           setRotation(getRotation()*-1); 
       
   
  
   public boolean atWorldEdge()   
   {   
       if(getX() < 10 || getX() > getWorld().getWidth() - 10)   
           return true;   
       if(getY() < 10 || getY() > getWorld().getHeight() - 10)   
           return true;   
       else   
           return false;   
   }   
danpost danpost

2014/10/4

#
So, your movement direction is determined by the rotation of the actor. Negating the rotation, however does not reverse the movement direction consistently. That is, at 90 or 270 degrees, it will work perfectly; but the further from those values the rotation of the actor becomes, the worse it will work. At zero or 180 degrees rotation, there will be no apparent change when the rotation is negated. This means you will have to deal with the horizontal and vertical movements separately; and, negate only the movement along only one of the two directions when an edge is encountered. This can be accomplished by using the rotation, and the code you have will work for top and bottom edges. For a side edge bounce another 180 degrees will have to be added after negating the rotation.
chamuzi3 chamuzi3

2014/10/4

#
I also have another question, i have two tank(actors) 1 and 2 but when am controlling them, dont want them to go through each other as i want them to collide ed when i hit a wall i stop but i can still move away from it so i want to do the same thing for both tanks so that they can go through each other. How can i do this?? :)
chamuzi3 chamuzi3

2014/10/4

#
And also have another problem. Two tanks(Tank and Tank2). Tank one shoots Bullet and Tank2 shoots Bullet2. i will send the code. When i put both the tanks in the game, one can shoot but one cant.Let me send you the code.
1
2
3
4
5
6
7
8
9
10
private void shoot()
 {
     if("f".equals(Greenfoot.getKey()))
     {
     Bullet bullet=new Bullet();
     getWorld().addObject(bullet,getX(), getY());  
     bullet.setRotation(getRotation());
     bullet.move(75);
     }
 }
this is for Tank one shooting Bullet and for Tank 2
1
2
3
4
5
6
7
8
9
10
private void shoot()
{
    
   if("p".equals(Greenfoot.getKey()))
    {
    Bullet2 bullet2=new Bullet2();
    getWorld().addObject(bullet2,getX(), getY());  
    bullet2.setRotation(getRotation());
    bullet2.move(75);
}
danpost danpost

2014/10/4

#
You cannot use 'getKey' more than once per act cycle. The second time it is executed will return 'null' if you are always checking for a key. You either need to find a central location to get the key, saving it in a local variable, so you can check for both possible keys or use 'isKeyDown' with boolean fields to track the state of each key being down or not.
chamuzi3 chamuzi3

2014/10/4

#
How would you write it?
danpost danpost

2014/10/4

#
The first way may have to be coded in your world class act method using something like this:
1
2
3
String key = Greenfoot.getKey();
if ("f".equals(key)) // code to have Tank object shoot a Bullet
if ("p".equals(key)) // code to have Tank2 object shoot a Bullet2
You will probably have to change the access of your 'shoot' methods to 'public' and add conditions that the tanks are in the world. The other way would involve adding instance Boolean fields to the classes of the tanks and perform a pair of double-conditions for trigger and trigger release. Something like this:
1
2
3
4
5
6
7
8
9
// instance field
private boolean shotKeyDown;
// in act method
if (!shotKeyDown && Greenfoot.isKeyDown("f"))
{
    shoot();
    shotKeyDown = true;
}
if (shotKeyDown && !Greenfoot.isKeyDown("f")) shotKeyDown = false;
chamuzi3 chamuzi3

2014/10/4

#
also have another question, i have two tank(actors) 1 and 2 but when am controlling them, dont want them to go through each other as i want them to collide ed when i hit a wall i stop but i can still move away from it so i want to do the same thing for both tanks so that they can go through each other. How can i do this?? :) code if you can. Thanks for your help btw am new to greenfoot and coding.
chamuzi3 chamuzi3

2014/10/4

#
Is there a way of me sending you what i have so far in my game so that you can see for your self whats happening and where the problem is??
chamuzi3 chamuzi3

2014/10/4

#
danpost wrote...
The first way may have to be coded in your world class act method using something like this:
1
2
3
String key = Greenfoot.getKey();
if ("f".equals(key)) // code to have Tank object shoot a Bullet
if ("p".equals(key)) // code to have Tank2 object shoot a Bullet2
You will probably have to change the access of your 'shoot' methods to 'public' and add conditions that the tanks are in the world. The other way would involve adding instance Boolean fields to the classes of the tanks and perform a pair of double-conditions for trigger and trigger release. Something like this:
1
2
3
4
5
6
7
8
9
// instance field
private boolean shotKeyDown;
// in act method
if (!shotKeyDown && Greenfoot.isKeyDown("f"))
{
    shoot();
    shotKeyDown = true;
}
if (shotKeyDown && !Greenfoot.isKeyDown("f")) shotKeyDown = false;
I don't know whats going on but i cant seem to get it together might be becuase am new to codding and greenfoot. Not sure what to do with the code.
chamuzi3 chamuzi3

2014/10/4

#
Is there a way of me sending you what i have so far in my game so that you can see for your self whats happening and where the problem is??
danpost danpost

2014/10/4

#
If you are as new as you say, maybe you should review the tutorial trails provided by oracle (java). You can go here. Study the 'Trails Covering the Basics' (second section on left). Greenfoot has tutorials on how to use the IDE it provides which should be checked out after the java trails. You should then have a better understanding of the classes greenfoot supplies and you can afterwards apply what you have learned to the classes you create within greenfoot.
chamuzi3 wrote...
Is there a way of me sending you what i have so far in my game so that you can see for your self whats happening and where the problem is??
You can share your scenario on this site. Click on the 'Share' button on the top-right of your scenario frame and supply the information requested (you should start at the 'Publish' tab in the pop-up dialog box). Make sure you check to 'Publish source code' checkbox so others can download it and look at the code.
chamuzi3 chamuzi3

2014/10/4

#
Do you have Facebook or any other fastest way of chatting so that we can talk more faster? The game is in the latest game right now check on the page you will see it.
There are more replies on the next page.
1
2