我正在制作一款2D游戏作为学校项目(只有初学者代码,没有花哨的东西)。
我的角色应该从自己的位置拍摄。
它只能在构造时从玩家那里获得位置,因此它只能从玩家开始的位置射击。
下面是一些我发现相关的代码:
class ShootManager
{
int shotsShooten = 0;
List<Shot> shots = new List<Shot>();
public void FireShot()
{
shots.Add(new Shot());
shotsShooten++;
}
public void Update(GameTime gameTime, KeyboardState KbState)
{
if (KbState.IsKeyDown(Keys.Space))
{ FireShot(); }
foreach (Shot shot in shots)
{
if (shot.Position.X == (float)shot.ISoutOfGame) //if the shot is out of the screen
shots.Remove(shots[shots.Count - 1]); //Removes the last item
shot.Position.X--;
}
}
}
class Shot
{
public Vector2 Position;
public int ISoutOfGame = -50;
public Shot(Vector2 position)
{
Position = position;
}
}
是否有办法绕过在玩家起始位置构建每个镜头,而不是在玩家当前位置创建它?
这里有一些简单的例子,如何做到这一点,它是在VB代码从头部和未测试。
Public Class Bullet
Position as Vector2
Velocity as Vector2
Active as boolean = false
End Class
Public Class Bullets
inherit list(of Bullet)
// public sub Init(Position, Velocity, Type, Acting, Speed, Size..... )
public Sub Init(Position, Velocity)
dim new Bullet as Bullet
Bullet.Position = Position
Bullet.Velocity = Velocity
Bullet.Active = true
add(Bullet)
end Sub
public Sub Update()
for each b as bullet in me.findall(function(c) c.active)
b.position += b.velocity
// if b.position outside screen then b.active = false
// also on collision set active to false
next
me.removeall(function(c) not(c.active))
end sub
public Sub Draw()
for each b as bullet in me.findall(function(c) c.active)
// draw bullets
next
end Sub
End Class