Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!
Werbeanzeige
C#-Quelltext |
|
1 2 3 4 5 |
private void VerifyIsReady() { if (!IsReady) throw new InvalidOperationException(Strings.StorageDevice_is_not_valid); //hier bekomme ich die Fehlermeldung } |
C#-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; SaveGame savenow; SpriteFont textanzeige; GamePadState gps, gpsPrev; KeyboardState ks, ksPrev; public int highscore; public Game1() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; graphics.PreferredBackBufferWidth = 1280; graphics.PreferredBackBufferHeight = 720; } protected override void Initialize() { savenow = new SaveGame(this); savenow.Initialize(); savenow.Laden(); highscore = savenow.elf; base.Initialize(); } protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); savenow.Load(Content); textanzeige = Content.Load<SpriteFont>("Arial"); // set up touch gesture support: make vertical drag and flick the // gestures that we're interested in. TouchPanel.EnabledGestures = GestureType.VerticalDrag | GestureType.Flick; } protected override void Update(GameTime gameTime) { gpsPrev = gps; ksPrev = ks; gps = GamePad.GetState(PlayerIndex.One); ks = Keyboard.GetState(); bool tapped = false; while (TouchPanel.IsGestureAvailable) { GestureSample gesture = TouchPanel.ReadGesture(); if (gesture.GestureType == GestureType.Tap) tapped = true; } if ((gps.IsButtonDown(Buttons.A) && gpsPrev.IsButtonUp(Buttons.A)) || (ks.IsKeyDown(Keys.Space) && ksPrev.IsKeyUp(Keys.Space)) || tapped) { savenow.Speichern(); } highscore += 1; base.Update(gameTime); } protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); spriteBatch.Begin(); spriteBatch.DrawString(textanzeige, highscore.ToString(), new Vector2(200, 200), Color.White); savenow.Draw(spriteBatch); spriteBatch.End(); base.Draw(gameTime); } } |
C#-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
public class SaveGame { SpriteFont font; IAsyncSaveDevice saveDevice; Vector2 textPos = new Vector2(100, 100); private Game1 game1; public int elf; public SaveGame(Game1 game) { game1 = game; } public void Laden() { if (saveDevice.FileExists("TestContainer", "SaveFile.txt")) { saveDevice.Load( "TestContainer", "SaveFile.txt", stream => { using (StreamReader reader = new StreamReader(stream)) { elf = int.Parse(reader.ReadLine()); } }); } } public void Speichern() { // make sure the device is ready if (saveDevice.IsReady) { // save a file asynchronously. this will trigger IsBusy to return true // for the duration of the save process. saveDevice.SaveAsync( "TestContainer", "SaveFile.txt", stream => { using (StreamWriter writer = new StreamWriter(stream)) writer.WriteLine(game1.highscore); }); } } public void Initialize() { // we can set our supported languages explicitly or we can allow the // game to support all the languages. the first language given will // be the default if the current language is not one of the supported // languages. this only affects the text found in message boxes shown // by EasyStorage and does not have any affect on the rest of the game. EasyStorageSettings.SetSupportedLanguages(Language.English); // on Windows Phone we use a save device that uses IsolatedStorage // on Windows and Xbox 360, we use a save device that gets a shared StorageDevice to handle our file IO. #if WINDOWS_PHONE saveDevice = new IsolatedStorageSaveDevice(); #else // create and add our SaveDevice SharedSaveDevice sharedSaveDevice = new SharedSaveDevice(); game1.Components.Add(sharedSaveDevice); // make sure we hold on to the device saveDevice = sharedSaveDevice; // hook two event handlers to force the user to choose a new device if they cancel the // device selector or if they disconnect the storage device after selecting it sharedSaveDevice.DeviceSelectorCanceled += (s, e) => e.Response = SaveDeviceEventResponse.Force; sharedSaveDevice.DeviceDisconnected += (s, e) => e.Response = SaveDeviceEventResponse.Force; // prompt for a device on the first Update we can sharedSaveDevice.PromptForDevice(); #endif // we use the tap gesture for input on the phone TouchPanel.EnabledGestures = GestureType.Tap; #if XBOX // add the GamerServicesComponent game1.Components.Add(new Microsoft.Xna.Framework.GamerServices.GamerServicesComponent(game1)); #endif // hook an event so we can see that it does fire saveDevice.SaveCompleted += new SaveCompletedEventHandler(saveDevice_SaveCompleted); } void saveDevice_SaveCompleted(object sender, FileActionCompletedEventArgs args) { // just write some debug output for our verification Debug.WriteLine("SaveCompleted!"); } public void Load(ContentManager content) { font = content.Load<SpriteFont>("Font"); } public void Draw(SpriteBatch spriteBatch) { game1.GraphicsDevice.Clear(Color.CornflowerBlue); Vector2 textPos = new Vector2(game1.GraphicsDevice.Viewport.TitleSafeArea.X + 50, game1.GraphicsDevice.Viewport.TitleSafeArea.Y + 10); spriteBatch.DrawString(font, string.Format("Save device {0} ready.", saveDevice.IsReady ? "is" : "is not"), textPos, Color.White); textPos.Y += font.LineSpacing; spriteBatch.DrawString(font, string.Format("Save device {0} busy.", saveDevice.IsBusy ? "is" : "is not"), textPos, Color.White); textPos.Y += font.LineSpacing; if (saveDevice.IsReady) { #if WINDOWS_PHONE string instructions = "Tap the screen to save a file."; #else string instructions = "Press the A button or space key to save a file."; #endif spriteBatch.DrawString(font, instructions, textPos, Color.White); } } } |
Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von »Big_Santa« (01.01.2013, 01:02)
Werbeanzeige