Jump to content
TouringRacer

Erro em menu (XNA Game Studio)

Recommended Posts

TouringRacer

Já estou farto de partir a cabeça e não sei o que fazer para deixar de obter este erro:

scaled.php?server=137&filename=erroeg.png&res=landing

Fica aqui o código completo:

using System;

using System.Collections.Generic;

using System.Linq;

using Microsoft.Xna.Framework;

using Microsoft.Xna.Framework.Audio;

using Microsoft.Xna.Framework.Content;

using Microsoft.Xna.Framework.GamerServices;

using Microsoft.Xna.Framework.Graphics;

using Microsoft.Xna.Framework.Input;

using Microsoft.Xna.Framework.Media;

using Microsoft.Xna.Framework.Net;

using Microsoft.Xna.Framework.Storage;

namespace ButtonTutorial

{

public class Game1 : Microsoft.Xna.Framework.Game

{

// Global variables

GraphicsDeviceManager graphics;

SpriteBatch spriteBatch;

GraphicsDevice device;

Texture2D backgroundTexture;

int screenWidth;

int screenHeight;

enum BState

{

HOVER,

UP,

JUST_RELEASED,

DOWN

}

const int NUMBER_OF_BUTTONS = 3,

INICIO_BUTTON_INDEX = 0,

OPCOES_BUTTON_INDEX = 1,

SAIR_BUTTON_INDEX = 2,

BUTTON_HEIGHT = 40,

BUTTON_WIDTH = 88;

Color background_color;

Color[] button_color = new Color[NUMBER_OF_BUTTONS];

Rectangle[] button_rectangle = new Rectangle[NUMBER_OF_BUTTONS];

BState[] button_state = new BState[NUMBER_OF_BUTTONS];

Texture2D[] button_texture = new Texture2D[NUMBER_OF_BUTTONS];

double[] button_timer = new double[NUMBER_OF_BUTTONS];

//mouse pressed and mouse just pressed

bool mpressed, prev_mpressed = false;

//mouse location in window

int mx, my;

double frame_time;

// for simulating button clicks with keyboard

KeyboardState keyboard_state, last_keyboard_state;

public Game1()

{

graphics = new GraphicsDeviceManager(this);

Content.RootDirectory = "Content";

}

// wrapper for hit_image_alpha taking Rectangle and Texture

Boolean hit_image_alpha(Rectangle rect, Texture2D tex, int x, int y)

{

return hit_image_alpha(0, 0, tex, tex.Width * (x - rect.X) /

rect.Width, tex.Height * (y - rect.Y) / rect.Height);

}

// wraps hit_image then determines if hit a transparent part of image

Boolean hit_image_alpha(float tx, float ty, Texture2D tex, int x, int y)

{

if (hit_image(tx, ty, tex, x, y))

{

uint[] data = new uint[tex.Width * tex.Height];

tex.GetData<uint>(data);

if ((x - (int)tx) + (y - (int)ty) *

tex.Width < tex.Width * tex.Height)

{

return ((data[

(x - (int)tx) + (y - (int)ty) * tex.Width

] &

0xFF000000) >> 24) > 20;

}

}

return false;

}

// determine if x,y is within rectangle formed by texture located at tx,ty

Boolean hit_image(float tx, float ty, Texture2D tex, int x, int y)

{

return (x >= tx &&

x <= tx + tex.Width &&

y >= ty &&

y <= ty + tex.Height);

}

// determine state and color of button

void update_buttons()

{

for (int i = 0; i < NUMBER_OF_BUTTONS; i++)

{

if (hit_image_alpha(

button_rectangle, button_texture, mx, my))

{

button_timer = 0.0;

if (mpressed)

{

// mouse is currently down

button_state = BState.DOWN;

button_color = Color.Blue;

}

else if (!mpressed && prev_mpressed)

{

// mouse was just released

if (button_state == BState.DOWN)

{

// button i was just down

button_state = BState.JUST_RELEASED;

}

}

else

{

button_state = BState.HOVER;

button_color = Color.LightBlue;

}

}

else

{

button_state = BState.UP;

if (button_timer > 0)

{

button_timer = button_timer - frame_time;

}

else

{

button_color = Color.White;

}

}

if (button_state == BState.JUST_RELEASED)

{

take_action_on_button(i);

}

}

}

// Logic for each button click goes here

void take_action_on_button(int i)

{

//take action corresponding to which button was clicked

switch (i)

{

case INICIO_BUTTON_INDEX:

background_color = Color.Green;

break;

case OPCOES_BUTTON_INDEX:

background_color = Color.Yellow;

break;

case SAIR_BUTTON_INDEX:

background_color = Color.Red;

break;

default:

break;

}

}

// Logic for each key down event goes here

void handle_keyboard()

{

last_keyboard_state = keyboard_state;

keyboard_state = Keyboard.GetState();

Keys[] keymap = (Keys[])keyboard_state.GetPressedKeys();

foreach (Keys k in keymap)

{

char key = k.ToString()[0];

switch (key)

{

case 'e':

case 'E':

take_action_on_button(INICIO_BUTTON_INDEX);

button_color[iNICIO_BUTTON_INDEX] = Color.Orange;

button_timer[iNICIO_BUTTON_INDEX] = 0.25;

break;

case 'm':

case 'M':

take_action_on_button(OPCOES_BUTTON_INDEX);

button_color[OPCOES_BUTTON_INDEX] = Color.Orange;

button_timer[OPCOES_BUTTON_INDEX] = 0.25;

break;

case 'h':

case 'H':

take_action_on_button(SAIR_BUTTON_INDEX);

button_color[sAIR_BUTTON_INDEX] = Color.Orange;

button_timer[sAIR_BUTTON_INDEX] = 0.25;

break;

default:

break;

}

}

}

/// <summary>

/// Allows the game to perform any initialization it needs to before starting to run.

/// This is where it can query for any required services and load any non-graphic

/// related content. Calling base.Initialize will enumerate through any components

/// and initialize them as well.

/// </summary>

protected override void Initialize()

{

// TODO: Add your initialization logic here

// starting x and y locations to stack buttons

// vertically in the middle of the screen

int x = Window.ClientBounds.Width / 2 - BUTTON_WIDTH / 2;

int y = Window.ClientBounds.Height / 2 -

NUMBER_OF_BUTTONS / 2 * BUTTON_HEIGHT -

(NUMBER_OF_BUTTONS % 2) * BUTTON_HEIGHT / 2;

for (int i = 0; i < NUMBER_OF_BUTTONS; i++)

{

button_state = BState.UP;

button_color = Color.White;

button_timer = 0.0;

button_rectangle = new Rectangle(x, y, BUTTON_WIDTH, BUTTON_HEIGHT);

y += BUTTON_HEIGHT;

}

IsMouseVisible = true;

background_color = Color.Black;

graphics.PreferredBackBufferWidth = 500;

graphics.PreferredBackBufferHeight = 500;

graphics.IsFullScreen = false;

graphics.ApplyChanges();

Window.Title = "Golden Path";

base.Initialize();

}

/// <summary>

/// LoadContent will be called once per game and is the place to load

/// all of your content.

/// </summary>

protected override void LoadContent()

{

// Create a new SpriteBatch, which can be used to draw textures.

spriteBatch = new SpriteBatch(GraphicsDevice);

button_texture[iNICIO_BUTTON_INDEX] =

Content.Load<Texture2D>(@"images/1");

button_texture[OPCOES_BUTTON_INDEX] =

Content.Load<Texture2D>(@"images/2");

button_texture[sAIR_BUTTON_INDEX] =

Content.Load<Texture2D>(@"images/3");

backgroundTexture =

Content.Load<Texture2D>(@"images/0");

screenWidth = device.PresentationParameters.BackBufferWidth;

screenHeight = device.PresentationParameters.BackBufferHeight;

// TODO: use this.Content to load your game content here

}

/// <summary>

/// UnloadContent will be called once per game and is the place to unload

/// all content.

/// </summary>

protected override void UnloadContent()

{

// TODO: Unload any non ContentManager content here

}

/// <summary>

/// Allows the game to run logic such as updating the world,

/// checking for collisions, gathering input, and playing audio.

/// </summary>

/// <param name="gameTime">Provides a snapshot of timing values.</param>

protected override void Update(GameTime gameTime)

{

// Allows the game to exit

if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)

this.Exit();

// get elapsed frame time in seconds

frame_time = gameTime.ElapsedGameTime.Milliseconds / 1000.0;

// update mouse variables

MouseState mouse_state = Mouse.GetState();

mx = mouse_state.X;

my = mouse_state.Y;

prev_mpressed = mpressed;

mpressed = mouse_state.LeftButton == ButtonState.Pressed;

update_buttons();

handle_keyboard();

// TODO: Add your update logic here

base.Update(gameTime);

}

/// <summary>

/// This is called when the game should draw itself.

/// </summary>

/// <param name="gameTime">Provides a snapshot of timing values.</param>

protected override void Draw(GameTime gameTime)

{

GraphicsDevice.Clear(background_color);

spriteBatch.Begin();

for (int i = 0; i < NUMBER_OF_BUTTONS; i++)

spriteBatch.Draw(button_texture, button_rectangle, button_color);

DrawScenery();

spriteBatch.End();

base.Draw(gameTime);

}

private void DrawScenery()

{

Rectangle screenRectangle = new Rectangle(0, 0, screenWidth, screenHeight);

spriteBatch.Draw(backgroundTexture, screenRectangle, Color.White);

}

}

}

Share this post


Link to post
Share on other sites
HappyHippyHippo

onde está a inicialização ou a atribuição da variável "device" ??

PS : as tuas teclas CTRL, C e V devem estar gastas não ?


IRC : sim, é algo que ainda existe >> #p@p

Share this post


Link to post
Share on other sites
TouringRacer

PS : as tuas teclas CTRL, C e V devem estar gastas não ?

Por acaso só usei duas vezes cada para colocar aqui ;)

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.