Shooting Bullet in JAVA

Started by
2 comments, last by Kevinator 16 years, 11 months ago
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
Advertisement
Did you compile the Shot class? Is it in the same directory?
bi-FreeSoftware
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.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

I like your code. Is
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