Shooting Bullet in JAVA
I am writing a shmup type game in java
Here is the function to shoot 5 bullets from the player
public Shot Shoot5Bullet()
{
for(int direction=0;direction<5;direction++){
Shot shot = new Shot(start_x, start_y,direction);
}
return shot;
}
However it output error with
.\Player.java:39: cannot find symbol
symbol : variable shot
location: class Player
return shot;
Can anyone help me to fix the problem? thx :D
Your 5 Shot instances are lost inside the scope of the foor loop.
You have to declare the shot variable outside of the loop.
Another but: You can't return 5 instances of the variable in a simple Shot type, you need to return an array.
You have to declare the shot variable outside of the loop.
Another but: You can't return 5 instances of the variable in a simple Shot type, you need to return an array.
I like your code. Is
As Endurion said, the reason for the error is that when you try to return
direction
some kind of angle modifier so that the shots emanate from the player in a fan shape? That's pretty nifty.As Endurion said, the reason for the error is that when you try to return
shot
, the compiler can't find that variable because that variable no longer exists when you exit the for loop. Try this:public Shot[] Shoot5Bullet(){ Shot[] shots = new Shot[5]; for(int direction = 0; direction < 5; direction++) shots[direction] = new Shot(start_x, start_y, direction); return shots;}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement