简体   繁体   English

给定时间不可见的游戏对象

[英]Game Object invisible for a given time

Good day everyone, currently I am developing a simple 2D game using SWINGAME. 大家好,目前我正在使用SWINGAME开发一个简单的2D游戏。 I have set a collision between 2 objects. 我在2个对象之间设置了碰撞。 So when they collide, I want to temporarily make one of them invisible for a certain time. 因此,当它们发生碰撞时,我想暂时将其中一个隐藏一段时间。 I am stuck about the time component, let's say I want the object to be invisible for 3 seconds after that it will change back to the default object. 我对时间组件感到困惑,比方说,我希望该对象在3秒钟后保持不可见状态,然后将其更改回默认对象。 Below are the two images, if the collision is true then it will display image2, or else display image1. 下面是两个图像,如果碰撞为true,则将显示image2,否则显示image1。 BTW I use a different image to indicate the invisibility. 顺便说一句,我用不同的图像来表示隐形。 Here's my code. 这是我的代码。

Player class: 玩家等级:

 public void Draw ()
        {
            if (invisible == true) {
                if(elapsedTime <= 3.0){

                elapsedTime += elapsedTime;
                SwinGame.DrawBitmap ("image2.png", (float)X, (float)Y);

                  } 
            }else {
                elapsedTime = 0;
                SwinGame.DrawBitmap ("image1.png", (float)X, (float)Y);
            }
        }

  public bool Invisible {
        get { return invisible; }
        set { invisible = value; }
       }

Object Collision class : 对象碰撞类:

{... //Some codes

    for(int i = 0; i < _obstacles.Count; i++)
    {
      if (_obstacles [i] is Invisible) {
         p.Invisible = true;
         p.Draw ();
           }
    }
//Some codes ...}

This should help you to calculate the time accurately by using the StopWatch class: 这应该可以帮助您使用StopWatch类准确地计算时间:

//somewhere in your code
Stopwatch sw = new Stopwatch();
            sw.Start();

public void Draw ()
        {
            if (invisible == true) {
                if(sw.ElapsedMilliseconds <= 3000){

                SwinGame.DrawBitmap ("image2.png", (float)X, (float)Y);

                  } 
            }else {
                sw.Restart();
                SwinGame.DrawBitmap ("image1.png", (float)X, (float)Y);
            }
        }

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

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