我正在制作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;
}
}
有没有什么方法可以让每个投篮都在球员的起始位置构建,而不是在玩家目前的位置创建?
答案 0 :(得分:0)
以下是一些简单的示例,您如何做到这一点,它来自头部的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