精灵行走和背景移动:XNA

精灵行走和背景移动:XNA,xna,xna-4.0,Xna,Xna 4.0,我想在XNA中做一件简单的事情,当角色向右移动时,背景会移动 有什么办法吗 谢谢我想你是说像在游戏里一样的马里奥! 使用滚动 创建游戏类。 按照绘制精灵的过程中所述加载资源。 加载背景纹理 private ScrollingBackground myBackground; protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures.

我想在XNA中做一件简单的事情,当角色向右移动时,背景会移动

有什么办法吗


谢谢

我想你是说像在游戏里一样的马里奥! 使用滚动

创建游戏类。 按照绘制精灵的过程中所述加载资源。 加载背景纹理

private ScrollingBackground myBackground;

protected override void LoadContent()
{
    // Create a new SpriteBatch, which can be used to draw textures.
    spriteBatch = new SpriteBatch(GraphicsDevice);
    myBackground = new ScrollingBackground();
    Texture2D background = Content.Load<Texture2D>("starfield");
    myBackground.Load(GraphicsDevice, background);
}
要滚动背景,请在更新方法中更改背景纹理的屏幕位置。 本例通过增加屏幕位置的Y值,每秒将背景向下移动100像素

protected override void Update(GameTime gameTime)
{
    ...
    // The time since Update was called last.
    float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;

    // TODO: Add your game logic here.
    myBackground.Update(elapsed * 100);

    base.Update(gameTime);
}
Y值保持不大于纹理高度,使背景从屏幕底部滚动回顶部

public void Update( float deltaY )
{
    screenpos.Y += deltaY;
    screenpos.Y = screenpos.Y % mytexture.Height;
}
// ScrollingBackground.Draw
使用LoadContent和Update中计算的原点和屏幕位置绘制背景

protected override void Draw(GameTime gameTime)
{
    GraphicsDevice.Clear(Color.CornflowerBlue);

    spriteBatch.Begin();
    myBackground.Draw(spriteBatch);
    spriteBatch.End();

    base.Draw(gameTime);
}
如果纹理没有覆盖屏幕,将绘制另一个纹理。这将使用加载时创建的texturesize向量从屏幕位置减去纹理高度。这会产生一个循环的错觉

public void Draw( SpriteBatch batch )
{
    // Draw the texture, if it is still onscreen.
    if (screenpos.Y < screenheight)
    {
        batch.Draw( mytexture, screenpos, null,
             Color.White, 0, origin, 1, SpriteEffects.None, 0f );
    }
    // Draw the texture a second time, behind the first,
    // to create the scrolling illusion.
    batch.Draw( mytexture, screenpos - texturesize, null,
         Color.White, 0, origin, 1, SpriteEffects.None, 0f );
}
public void Draw(SpriteBatch批处理)
{
//如果纹理仍在屏幕上,则绘制纹理。
如果(屏幕位置Y<屏幕高度)
{
batch.Draw(mytexture、screenpos、null、,
颜色。白色,0,原点,1,精灵效果。无,0f);
}
//第二次绘制纹理,在第一次之后,
//创建滚动错觉。
batch.Draw(mytexture,screenpos-texturesize,null,
颜色。白色,0,原点,1,精灵效果。无,0f);
}

我想你是说像游戏中的马里奥! 使用滚动

创建游戏类。 按照绘制精灵的过程中所述加载资源。 加载背景纹理

private ScrollingBackground myBackground;

protected override void LoadContent()
{
    // Create a new SpriteBatch, which can be used to draw textures.
    spriteBatch = new SpriteBatch(GraphicsDevice);
    myBackground = new ScrollingBackground();
    Texture2D background = Content.Load<Texture2D>("starfield");
    myBackground.Load(GraphicsDevice, background);
}
要滚动背景,请在更新方法中更改背景纹理的屏幕位置。 本例通过增加屏幕位置的Y值,每秒将背景向下移动100像素

protected override void Update(GameTime gameTime)
{
    ...
    // The time since Update was called last.
    float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;

    // TODO: Add your game logic here.
    myBackground.Update(elapsed * 100);

    base.Update(gameTime);
}
Y值保持不大于纹理高度,使背景从屏幕底部滚动回顶部

public void Update( float deltaY )
{
    screenpos.Y += deltaY;
    screenpos.Y = screenpos.Y % mytexture.Height;
}
// ScrollingBackground.Draw
使用LoadContent和Update中计算的原点和屏幕位置绘制背景

protected override void Draw(GameTime gameTime)
{
    GraphicsDevice.Clear(Color.CornflowerBlue);

    spriteBatch.Begin();
    myBackground.Draw(spriteBatch);
    spriteBatch.End();

    base.Draw(gameTime);
}
如果纹理没有覆盖屏幕,将绘制另一个纹理。这将使用加载时创建的texturesize向量从屏幕位置减去纹理高度。这会产生一个循环的错觉

public void Draw( SpriteBatch batch )
{
    // Draw the texture, if it is still onscreen.
    if (screenpos.Y < screenheight)
    {
        batch.Draw( mytexture, screenpos, null,
             Color.White, 0, origin, 1, SpriteEffects.None, 0f );
    }
    // Draw the texture a second time, behind the first,
    // to create the scrolling illusion.
    batch.Draw( mytexture, screenpos - texturesize, null,
         Color.White, 0, origin, 1, SpriteEffects.None, 0f );
}
public void Draw(SpriteBatch批处理)
{
//如果纹理仍在屏幕上,则绘制纹理。
如果(屏幕位置Y<屏幕高度)
{
batch.Draw(mytexture、screenpos、null、,
颜色。白色,0,原点,1,精灵效果。无,0f);
}
//第二次绘制纹理,在第一次之后,
//创建滚动错觉。
batch.Draw(mytexture,screenpos-texturesize,null,
颜色。白色,0,原点,1,精灵效果。无,0f);
}

我想你是说像游戏中的马里奥! 使用滚动

创建游戏类。 按照绘制精灵的过程中所述加载资源。 加载背景纹理

private ScrollingBackground myBackground;

protected override void LoadContent()
{
    // Create a new SpriteBatch, which can be used to draw textures.
    spriteBatch = new SpriteBatch(GraphicsDevice);
    myBackground = new ScrollingBackground();
    Texture2D background = Content.Load<Texture2D>("starfield");
    myBackground.Load(GraphicsDevice, background);
}
要滚动背景,请在更新方法中更改背景纹理的屏幕位置。 本例通过增加屏幕位置的Y值,每秒将背景向下移动100像素

protected override void Update(GameTime gameTime)
{
    ...
    // The time since Update was called last.
    float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;

    // TODO: Add your game logic here.
    myBackground.Update(elapsed * 100);

    base.Update(gameTime);
}
Y值保持不大于纹理高度,使背景从屏幕底部滚动回顶部

public void Update( float deltaY )
{
    screenpos.Y += deltaY;
    screenpos.Y = screenpos.Y % mytexture.Height;
}
// ScrollingBackground.Draw
使用LoadContent和Update中计算的原点和屏幕位置绘制背景

protected override void Draw(GameTime gameTime)
{
    GraphicsDevice.Clear(Color.CornflowerBlue);

    spriteBatch.Begin();
    myBackground.Draw(spriteBatch);
    spriteBatch.End();

    base.Draw(gameTime);
}
如果纹理没有覆盖屏幕,将绘制另一个纹理。这将使用加载时创建的texturesize向量从屏幕位置减去纹理高度。这会产生一个循环的错觉

public void Draw( SpriteBatch batch )
{
    // Draw the texture, if it is still onscreen.
    if (screenpos.Y < screenheight)
    {
        batch.Draw( mytexture, screenpos, null,
             Color.White, 0, origin, 1, SpriteEffects.None, 0f );
    }
    // Draw the texture a second time, behind the first,
    // to create the scrolling illusion.
    batch.Draw( mytexture, screenpos - texturesize, null,
         Color.White, 0, origin, 1, SpriteEffects.None, 0f );
}
public void Draw(SpriteBatch批处理)
{
//如果纹理仍在屏幕上,则绘制纹理。
如果(屏幕位置Y<屏幕高度)
{
batch.Draw(mytexture、screenpos、null、,
颜色。白色,0,原点,1,精灵效果。无,0f);
}
//第二次绘制纹理,在第一次之后,
//创建滚动错觉。
batch.Draw(mytexture,screenpos-texturesize,null,
颜色。白色,0,原点,1,精灵效果。无,0f);
}

我想你是说像游戏中的马里奥! 使用滚动

创建游戏类。 按照绘制精灵的过程中所述加载资源。 加载背景纹理

private ScrollingBackground myBackground;

protected override void LoadContent()
{
    // Create a new SpriteBatch, which can be used to draw textures.
    spriteBatch = new SpriteBatch(GraphicsDevice);
    myBackground = new ScrollingBackground();
    Texture2D background = Content.Load<Texture2D>("starfield");
    myBackground.Load(GraphicsDevice, background);
}
要滚动背景,请在更新方法中更改背景纹理的屏幕位置。 本例通过增加屏幕位置的Y值,每秒将背景向下移动100像素

protected override void Update(GameTime gameTime)
{
    ...
    // The time since Update was called last.
    float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;

    // TODO: Add your game logic here.
    myBackground.Update(elapsed * 100);

    base.Update(gameTime);
}
Y值保持不大于纹理高度,使背景从屏幕底部滚动回顶部

public void Update( float deltaY )
{
    screenpos.Y += deltaY;
    screenpos.Y = screenpos.Y % mytexture.Height;
}
// ScrollingBackground.Draw
使用LoadContent和Update中计算的原点和屏幕位置绘制背景

protected override void Draw(GameTime gameTime)
{
    GraphicsDevice.Clear(Color.CornflowerBlue);

    spriteBatch.Begin();
    myBackground.Draw(spriteBatch);
    spriteBatch.End();

    base.Draw(gameTime);
}
如果纹理没有覆盖屏幕,将绘制另一个纹理。这将使用加载时创建的texturesize向量从屏幕位置减去纹理高度。这会产生一个循环的错觉

public void Draw( SpriteBatch batch )
{
    // Draw the texture, if it is still onscreen.
    if (screenpos.Y < screenheight)
    {
        batch.Draw( mytexture, screenpos, null,
             Color.White, 0, origin, 1, SpriteEffects.None, 0f );
    }
    // Draw the texture a second time, behind the first,
    // to create the scrolling illusion.
    batch.Draw( mytexture, screenpos - texturesize, null,
         Color.White, 0, origin, 1, SpriteEffects.None, 0f );
}
public void Draw(SpriteBatch批处理)
{
//如果纹理仍在屏幕上,则绘制纹理。
如果(屏幕位置Y<屏幕高度)
{
batch.Draw(mytexture、screenpos、null、,
颜色。白色,0,原点,1,精灵效果。无,0f);
}
//第二次绘制纹理,在第一次之后,
//创建滚动错觉。
batch.Draw(mytexture,screenpos-texturesize,null,
颜色。白色,0,原点,1,精灵效果。无,0f);
}

当您将角色向右移动时,背景的增量x…背景将移动,但在这种情况下,角色将始终处于相同的位置。因此,从背景x中减去一个值,使其向左移动。更好的是,如果你让它表现为你的球员相反的X方向,向左移动也会工作!层间运动量*