Ciao. Ho fatto un esperimento con due oggetti, uno che si muove con la tastiera e uno no.
Ho voluto provare la programmazione OOP con l'ereditarietà e tutto va bene. Lavoro sempre in VB, ma per abbracciare un pubblico più vasto (ed aumentare la probabilità di ricevere aiuti) posto il codice in C#![]()
L'oggetto 'y' che NON si muove con la tastiera l'ho creato con una classe "generica" GameObject (allego il file GameObject.cs)
L'oggetto 'x' che si muove con la tastiera lo creo con una classe GameMoveableObject che faccio ereditare da GameObject e che in più contiene l'input da tastiera. Questo è il file:codice:using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; namespace oop { class GameObject { public Vector2 pos { get; set; } public Texture2D spr { get; set; } public GameObject() { this.pos = Vector2.Zero; } public GameObject(Vector2 val ) { this.pos = val; } public void Draw(SpriteBatch s ) { s.Draw (this.spr, this.pos, Color.White); } } }
e questa è la classe Game1:codice:using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; namespace oop { class GameMoveableObject : GameObject { public void KeybInput() { KeyboardState k = Keyboard.GetState(); if (k.IsKeyDown(Keys.Escape)) { /* VOGLIO USCIRE! this.Exit? base.Exit? Game1.Exit? Game.Exit? Program.Exit? */ } else if (k.IsKeyDown(Keys.Right)) this.pos += new Vector2(1, 0); else if (k.IsKeyDown(Keys.Left)) this.pos += new Vector2(-1, 0); else if (k.IsKeyDown(Keys.Up)) this.pos += new Vector2(0, -1); else if (k.IsKeyDown(Keys.Down)) this.pos += new Vector2(0, 1); } public GameMoveableObject() { base.pos = Vector2.Zero; } public GameMoveableObject(Vector2 val ) { base.pos = val; } } }
Il problema è che premendo ESC vorrei uscire dal gioco, e questo lo vorrei scrivere dentro la classe GameMoveableObject.codice:using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; namespace oop { public class Game1 : Microsoft.Xna.Framework.Game { private GraphicsDeviceManager g; private SpriteBatch s; private GameMoveableObject x ; private GameObject y ; public Game1() { g = new GraphicsDeviceManager(this); g.PreferredBackBufferWidth = 1000; g.PreferredBackBufferHeight = 500; Content.RootDirectory = "Content"; } protected override void Initialize() { x = new GameMoveableObject(new Vector2(450, 450)); y = new GameObject(new Vector2(450, 350)); base.Initialize(); } protected override void LoadContent() { s = new SpriteBatch(GraphicsDevice); x.spr = Content.Load<Texture2D>("x"); y.spr = Content.Load<Texture2D>("y"); } protected override void UnloadContent() { } protected override void Update(GameTime t) { x.KeybInput(); base.Update(t); } protected override void Draw(GameTime t) { GraphicsDevice.Clear(Color.CornflowerBlue); s.Begin(); x.Draw(s); y.Draw(s); s.End(); base.Draw(t); } } }
Come si fa? Ma si può fa?![]()
Grazie.

Rispondi quotando
più di VB!)
