2012-01-13 221 views
1

我在xnadevelopment.com上使用背景滚动教程(已修改以适应我的要求)为Windows Phone 7.1上的游戏创建垂直滚动循环。每当下一张图像被绘制时,似乎背景闪烁。虽然我在循环中使用单个图像,但即使使用多个图像,也会发生闪烁。我发布了一个YouTube视频,显示屏幕顶部出现的闪烁。在XNA中滚动背景闪烁

http://youtu.be/Ajdiw2zILq0 下面是用于创建循环的代码:

背景类:

private List<string> _road; 
    private VericalBackgroundLoop _roadLoop; 
    private readonly Vector2 _roadSpeed = new Vector2(0, 300); 

    public void LoadContent(ContentManager contentManager) 
    { 
     _road = new List<string> 
         { 
          "Test\\Road_Bgnd", 
          "Test\\Road_Bgnd" 
         }; 

     _roadLoop = new VericalBackgroundLoop(); 
     _roadLoop.Initialize(_road, contentManager, Vector2.Zero, true); 
    } 

    public void Update(TimeSpan elapsedTime) 
    { 
     _roadLoop.Update(_roadSpeed, elapsedTime); 
    } 

    public void Draw(SpriteBatch spriteBatch) 
    { 
     _roadLoop.Draw(spriteBatch); 
    } 

背景循环类:

private List<Sprite> _sprites; 
    private bool _isLoopDirectionTopToBottom; 
    private Vector2 _loopDirection; 

    public void Initialize(List<string> spriteNames, ContentManager contentManager, Vector2 loopStartPosition, bool isLoopDirectionTopToBottom) 
    { 
     _sprites = new List<Sprite>(); 
     _isLoopDirectionTopToBottom = isLoopDirectionTopToBottom; 

     _loopDirection = new Vector2(0, -1); 

     // Build the sprite object's list 
     foreach (string spriteName in spriteNames) 
     { 
      Sprite sprite = new Sprite(); 
      sprite.LoadContent(contentManager, spriteName); 

      _sprites.Add(sprite); 
     } 

     if (_isLoopDirectionTopToBottom) 
     { 
      // Set the initial position for the sprite objects 
      foreach (Sprite currentSprite in _sprites) 
      { 
       if (currentSprite == _sprites.First()) 
       { 
        currentSprite.Position = loopStartPosition; 
       } 
       else 
       { 
        Sprite prevSprite = GetSpriteAtIndex(_sprites.IndexOf(currentSprite) - 1); 
        currentSprite.Position = new Vector2(0, prevSprite.Position.Y - prevSprite.Size.Height); 
       } 
      } 
     } 
    } 

    public void Update(Vector2 loopSpeed, TimeSpan elapsedTime) 
    { 
     if (_isLoopDirectionTopToBottom) 
     { 
      foreach (Sprite currentSprite in _sprites) 
      { 
       if (currentSprite == _sprites.First()) 
       { 
        Sprite lastSprite = _sprites.Last(); 
        if (currentSprite.Position.Y > (currentSprite.Size.Height)) 
        { 
         currentSprite.Position.Y = lastSprite.Position.Y - lastSprite.Size.Height; 
        } 
       } 
       else 
       { 
        Sprite prevSprite = GetSpriteAtIndex(_sprites.IndexOf(currentSprite) - 1); 
        if (currentSprite.Position.Y > (currentSprite.Size.Height)) 
        { 
         currentSprite.Position.Y = prevSprite.Position.Y - prevSprite.Size.Height; 
        } 
       } 

       // Update the sprite X position with the speed and time 
       currentSprite.Position -= _loopDirection * loopSpeed * (float)elapsedTime.TotalSeconds; 
      } 
     } 
    } 

    public void Draw(SpriteBatch spriteBatch) 
    { 
     foreach (Sprite sprite in _sprites) 
     { 
      sprite.Draw(spriteBatch); 
     } 
    } 

    private Sprite GetSpriteAtIndex(int index) 
    { 
     return _sprites[index]; 
    } 

我需要搞清楚的帮助为什么闪烁是发生的g,为什么运动似乎不稳定(这在设备上稍微好一些,但是不稳定)。 game.cs类中的IsFixedTimeStep设置为true。谢谢。

编辑:似乎如果使用3个或更多的图像不会发生闪烁。这可能是由于第一张图像没有足够快地放回起始位置。但我仍然试图找出乳清动画仍然是如此生涩:(

+0

我会使用毫秒,而不是基于时间的运动秒。否则,你会失去很多精度,因为通常一帧不到一秒钟。 – dowhilefor 2012-01-13 11:01:23

+0

要使用毫秒,我相信速度必须修改?有关速度应该更新多少的任何想法? – user1147470 2012-01-13 11:41:53

+0

如果将时间除以1000,则将速度除以1000 – annonymously 2012-01-13 12:14:30

回答

0

你不会是第一个报告看到闪烁或口吃。在应用程序中心论坛上有很多帖子关于这一点(很高兴你是使用我的教程/样本的方式寻找运气)

这里是别人的报告,你所看到的一个例子! - >http://forums.create.msdn.com/forums/t/30500.aspx

这里是我见过的迄今为止从XNA的一个最佳答案框架开发人员 - > http://forums.create.msdn.com/forums/p/9934/53561.aspx#53561

基本上你已经偶然发现到“一个”的解决方案,但正如肖恩的帖子所指出的那样,解决这个问题的方法有很多种,它只取决于游戏的正确性。