Movimetação do background
-
segunda-feira, 28 de novembro de 2011 12:35
estou desenvolvendo um jogo e gostaria de fazer a imagem de fundo se movimentar,eu ate tentei fiz um codigo para movimentar ,mas quando eu compilei o que movimentou foi o meu personagem e nao a imagem de fundo , segue todo o codigo do jogo para ver se alguem pode me ajudar ,eu devo estar errando eu algo simples, mas nao estou conseguindo ver esta soluçao se alguem poder me ajudar ficaria grato
public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; Texture2D fantasma; Vector2 posicao = new Vector2(100.0f,100.0f); Texture2D espaco; Rectangle retangulo; KeyboardState teclado; float velocidade = 5f; public Game1() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; } protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); fantasma = Content.Load<Texture2D>("fantasma"); espaco = Content.Load<Texture2D>("espaco"); retangulo = new Rectangle(0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height); // TODO: use this.Content to load your game content here } protected override void Update(GameTime gameTime) { // Allows the game to exit if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit(); teclado = Keyboard.GetState(); if (teclado.IsKeyDown(Keys.W)) { posicao.Y -= velocidade; } if (teclado.IsKeyDown(Keys.S)) { posicao.Y += velocidade; } if (teclado.IsKeyDown(Keys.D)) { posicao.X += velocidade; } if (teclado.IsKeyDown(Keys.A)) { posicao.X -= velocidade; } // TODO: Add your update logic here
//Esse que era o responsavel por fazer a imagem movimentar float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds; posicao.Y += elapsed * 100; posicao.Y = posicao.Y % espaco.Width; base.Update(gameTime); } protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); spriteBatch.Begin(); spriteBatch.Draw(espaco, retangulo, Color.White); spriteBatch.Draw(fantasma, posicao, Color.White); spriteBatch.End(); // TODO: Add your drawing code here base.Draw(gameTime); }
- Editado Diego G França terça-feira, 29 de novembro de 2011 10:34 erro no codigo
Todas as Respostas
-
terça-feira, 29 de novembro de 2011 11:30
Eu conseguir resolver o probelma aqui está a resposta ,quem se interessar ,e quiser uma explicaçao mais detalhada é so postar a duvida aquipublic class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; Texture2D fantasma; Vector2 posicao = new Vector2(100.0f,100.0f); Texture2D espaco; int posicao_y; Rectangle retangulo; KeyboardState teclado; int velocidade = 5; public Game1() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; } protected override void Initialize() { // TODO: Add your initialization logic here posicao_y = 0; base.Initialize(); } protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); fantasma = Content.Load<Texture2D>("fantasma"); espaco = Content.Load<Texture2D>("espaco"); // TODO: use this.Content to load your game content here } protected override void Update(GameTime gameTime) { // Allows the game to exit if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit(); teclado = Keyboard.GetState(); if (teclado.IsKeyDown(Keys.W)) { posicao.Y -= velocidade; } if (teclado.IsKeyDown(Keys.S)) { posicao.Y += velocidade; } if (teclado.IsKeyDown(Keys.D)) { posicao.X += velocidade; } if (teclado.IsKeyDown(Keys.A)) { posicao.X -= velocidade; } posicao_y += velocidade / 2; if (posicao_y >= this.Window.ClientBounds. Height) posicao_y = 0; base.Update(gameTime); } protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); spriteBatch.Begin(); spriteBatch.Draw(espaco,new Rectangle(0,posicao_y,this.Window.ClientBounds.Width, this.Window.ClientBounds.Height), Color.White); spriteBatch.Draw(espaco, new Rectangle(0, posicao_y-this.Window.ClientBounds.Height, this.Window.ClientBounds.Width, this.Window.ClientBounds.Height), Color.White); spriteBatch.Draw(fantasma, posicao, Color.White); spriteBatch.End(); // TODO: Add your drawing code here base.Draw(gameTime); }
- Marcado como Resposta Diego G França terça-feira, 29 de novembro de 2011 11:30

