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

2012/11/11

Problem with codes!

jabirfatah91 jabirfatah91

2012/11/11

#
Hi , You have to do a favor for me. I am having a big problem with my cannon shooting game. As far I know my codes are correct. But when I try to play that (and press “space” key in order to project the ball from cannon) the ball does some strange behavior. It does not move any projectile motion where it just goes up by drawing some lines itself and comes down automatically. I will upload my scenario with opening my source code and I will name my scenario “Cannon shooting unsolved”. Please look at my codes and give me some solution too. I will be very grateful to you. Thanks/.
danpost danpost

2012/11/11

#
First, in your Cannonball class, change your constructor to the following:
public CannonBall(int angle, int power)
{
    v0x=power*Math.cos(Math.toRadians(330-angle));
    v0y=-power*Math.sin(Math.toRadians(330-angle));
}
The value '330 - angle' is derived from the facts that the cannon, at zero rotation, has about a 30 degree angle to begin with (which means 'angle + 30') and because the direction of angles is reversed from that of java, we will need to subtract that from 360: 360 - (angle + 30) = 330 - angle Also, because of the direction difference, we need to negate the final value for 'v0y'. I think that statement could be written as v0y=power*Math.sin(Math.toRadians(angle+30)); which negates both inside and outside, cancelling each other out. The other change was in your 'sin' and 'cos' for 'x' and 'y', which were backward. The other thing in that class that needs changing, is in your act method when determining 't', you are dividing by '10000', which has one too many zeros (to produce seconds). Make it '1000'. Finally, in the Cannon class, in the 'makeCannonBall' method, you have an unwanted semi-colon at the end of the 'if' clause (remove it).
jabirfatah91 jabirfatah91

2012/11/11

#
Hi Danpost! Thanks thousands! It works. But the problem is that it just produces only one ball when I press the key "space" and the ball rolls at the ground edge of the world untill it reaches the last border. What I want to do is create a single ball by single pressing of "space" key. That means (1). every time I press the "space" key( without resetting the scenerio), a new single ball should be created automatically and of course (2). the ball must get the projectile motion & (3). I shuld be able to increase/decrease the power of projectile by pressing the "up" and "down" arrow key ( increasing or decreasing power means making the trajectory longer or shorter by controlling the speed of the cannonBall). Can you please explian how can I do that? .I should be able to make one like that.
Malmotri Malmotri

2012/11/11

#
Hi! In your battleclass Change to this.
 super(800, 600, 1,false);
the word false will make it possible for the cannonball to go outside your world.
danpost danpost

2012/11/12

#
I will get to creating a ball on each press of the "space" key in a moment. First, let me address your other issues. (2) from what I saw, the balls created do have projectile motion (3) the "up" and "down" keys do increase and decrease the initial speed of the balls. A better alternative to the suggestion Malmotri gave for removing balls when they reach the each of the world is with something you already have in your scenario; however, you need to subtract at least two from 'getWorld().getHeight()' in the 'if' clause. OK, back to the creating of the balls. Add a boolean instance field in your sub-class of World set to 'false' (call it ,'spaceDown') and use the following code to trigger a new ball
if (!spaceDown && Greenfoot.isKeyDown("space"))
{
    // code to add the new cannonball into the world
    spaceDown = true;
}
if (spaceDown && !Greenfoot.isKeyDown("space")) spaceDown = false;
jabirfatah91 jabirfatah91

2012/11/12

#
Hi I can create a new ball by adding "false" as malmotri said. But, danpost, can you tell me about the codes you have written- what does it do other than creating a new ball on each press of the "space" key? Now I have two problems, respectively (1). big and (2). little. (1). My ball should be projected according to Cannon's rotation! But it doesn't. For ex. when I rotate the face or trigger of the Cannon is to up, the ball doesn't project itself up, where it just falls down as it was before. (2). The ball should be come up from the Cannon's tube/trigger. But it is produced itself at the central position where the wheel is. (it may be fixed later, coz it's not that very important.) Next, (3). I want to increase the power of ball. In this moment it just increases the initial speed very little while pressing the "up" arrow key. Finally (4). Reffering to your comment, " however, you need to subtract at least two from 'getWorld().getHeight()' in the 'if' clause"- what does it do and where I have to do that exactly?
Malmotri Malmotri

2012/11/12

#
Try to change your constructor in CannonBall to this.
public CannonBall(int angle, int power)
    {
        v0x=power*Math.cos(Math.toRadians(30+angle));
        v0y=power*Math.sin(Math.toRadians(30+angle));
    }
And the keypress method in Cannon to this.
 public void checkKeyPress()
        {
        if(Greenfoot.isKeyDown("left")&&angle<60)
        {
            angle=angle+1;
        }
        if(Greenfoot.isKeyDown("right")&&angle>-20)
        {
            angle=angle-1;
        }
jabirfatah91 jabirfatah91

2012/11/12

#
SUPER malmo!! Thanks a lot, it works now. now only adding messsage remains...
danpost danpost

2012/11/12

#
Getting to your issues above (not in order): (4) Since you have now unbounded your world (put the extra parameter 'false' in your world 'super' constructor), this is not an issue. (3) You should allow passage over all possibilities of power. Incrementing is the way to go. (2) You will need to do two things to fix this. One set a minimum on the power to 30 and add a 'setPaintOrder' statement in your world constructor so that the cannonball is hidden behind the cannon while passing up the tube. (1) I addressed this earlier in the discussion.
You need to login to post a reply.