简体   繁体   English

XNA如何使对象不可见

[英]XNA How to make an object invisible

I'm making a 2D Shooter game in XNA. 我正在XNA制作2D射击游戏。 I had been working on the shooting speed of the player (Every how often can the player shoot another bullet) and made it so that the player can only shoot again once the previous bullet has traveled a certain distance like so: 我一直在努力研究玩家的射击速度(玩家每次射击另一颗子弹的频率),并且只有在前一个子弹行进一定距离后,玩家才能再次射击:

if (this.bulletList[0].BULLETS[(this.bulletList[0].BULLETS.Count) - 1].X >= Pos.X + attackSpeed)
                canShoot = true;

Where bulletList is the available Projectiles the player can shoot, BULLETS is the List of Bullets already shot, and attackSpeed is the rate in which the bullets should be shot, or simply put: the distance the bullet has to travel until another bullet can be shot. 其中bulletList是玩家可以射击的可用射弹,BULLETS是已经射击的子弹列表,attackSpeed是子弹射击的速率,或简单地说:子弹必须行进的距离,直到可以射击另一颗子弹。

Now I've been working on Collisions. 现在我一直在研究碰撞。 My method was to dispose of a bullet after it hits a target like so: 我的方法是在击中目标之后处理子弹,如下所示:

for (int i = 0; i < player.BULLETLIST[0].BULLETS.Count; i++)
        {
            if (CollisionManager.PlayerBulletOnBot(player.BULLETLIST[0].BULLETS[i], bot))
                player.BULLETLIST[0].BULLETS.RemoveAt(i);
        }

Problem is, if the bullet has been removed upon hitting a target, I can no longer ask if that bullet has passed the given distance for another bullet to be shot. 问题是,如果子弹在击中目标时被移除,我就不能再问这个子弹是否已超过给定距离以便射击另一颗子弹。 To solve that, I'd like the bullet to turn invisible upon hit, and afterwards it'll be disposed of in another function that's already been made. 为了解决这个问题,我希望子弹在击中时变得不可见 ,之后它将被处理掉已经制作的另一个功能。

Simply have a Visible flag for a Bullet instance: 只需一个Bullet实例的Visible标志:

class Bullet {
    public bool Visible { get; set; }
}

When it hits.. make it invisible: 当它击中..使它不可见:

// ... hit
bulletInstace.Visible = false;

Then check before drawing it: 然后在绘制之前检查:

if (bulletInstance.Visible)
    drawBullet(bullet);

If it isn't visible, your drawing code should just skip over it. 如果它不可见,您的绘图代码应该跳过它。

When you draw an object using spriteBatch.Draw(...) you have to select a color for the sprite itself. 使用spriteBatch.Draw(...)绘制对象时,必须为sprite本身选择一种颜色。

We all know that the color is a mixture of "red, green and blue" values (or rgb). 我们都知道颜色是“红色,绿色和蓝色”值(或rgb)的混合物。 What not many people know is that, at least in XNA, there is a fourth value called alpha. 没有多少人知道,至少在XNA中,有第四个值叫做alpha。

The alpha value indicates the transparency of your object, meaning that if you use it like the code below, your object will be half invisible (or half visible). alpha值表示对象的透明度,这意味着如果您像下面的代码一样使用它,您的对象将是半隐形(或半可见)。

spriteBatch.Draw(..., Color.White * 0,5f,...);

You can play with that :) 你可以玩:)

Check a little more here , on the old XNA forums. 检查多一点在这里 ,对老XNA论坛。

You need rate of fire ( rof ) property that you increase to some maxRof value when you hold down button. 当您按住按钮时,您需要将火焰( rof )属性增加到某个maxRof值。 and when rof is equal to maxrof then you add bullet to a list of bullets (and reset rof to 0). 当rof等于maxrof时,你将子弹添加到子弹列表中(并将rof重置为0)。

Do not make it invisible, remove it from list. 不要让它不可见,将其从列表中删除。 Every instance of bullet should have "Active" property set to true on fire. 每个子弹实例都应将“Active”属性设置为true。 (when you fire a bullet, add it to list) And when collsition happened set this property to false. (当你发射子弹时,将其添加到列表中)当发生collsition时,将此属性设置为false。 pseudo code as example: 伪代码为例:

UPDATE UPDATE

for each bullet in bullets
  -- update bullet position
  -- check collision if happened if yes then set Active to false
end for

bullets.removeall(function(c) NOT(c.active));

DRAW

for each bullet in bullets.findall(function(c) c.active)
  -- draw your bullets
end for

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM