所以我正在製作太空入侵者遊戲。我想要從different/random locations on the entire 0 coordinate of X
產生流星。我應該怎麼辦?我見過使用Lists
和Random()
s的人,但我需要我的meteorGenerator
類的代碼。然後我將其稱爲Game1中的方法C#XNA - 隨機在頂部X軸上繪製紋理
當流星被繪製時,它們應該落在屏幕的底部並消失。 我在這裏看到了SO答案,它實現我的課:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Storage;
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;
namespace SpaceInvaders
{
class meteorGenerator
{
public static Vector2 m_Pos;
public Vector2 m_Position
{
get { return m_Pos; }
set { m_Pos = value; }
}
Texture2D m_Texture { get; set; }
Texture2D m_MeteorsTex;
public meteorGenerator(Texture2D m_Tex, Vector2 m_Pos)
{
m_Position = m_Pos;
m_Texture = m_Tex;
}
List<meteorGenerator> m_MeteorsList = new List<meteorGenerator>();
static readonly Random rnd = new Random();
public void LoadContent(ContentManager Content)
{
m_MeteorsTex = Content.Load<Texture2D>(".\\gameGraphics\\gameSprites\\thePlan\\meteorSpawn");
}
public void Update(GameTime gameTime)
{
if (m_MeteorsList.Count() < 4)
{
m_MeteorsList.Add(new meteorGenerator(m_MeteorsTex, new Vector2(rnd.Next(30, 610), rnd.Next(30, 450))));
}
}
public void Draw(SpriteBatch spriteBatch)
{
foreach (meteorGenerator m_Meteor in m_MeteorsList)
{
spriteBatch.Draw(m_Meteor.m_Texture, m_Meteor.m_Position, Color.White);
}
}
}
}
但是,當我嘗試實例中的Game1類的構造函數,我得到一個錯誤:
meteorGenerator m_MeteorGenerator;
protected override void Initialize()
{
// TODO: Add your initialization logic here.
m_MeteorGenerator = new meteorGenerator(meteorGenerator.m_Tex, meteorGenerator.m_Pos);
}
Error 1 'SpaceInvaders.meteorGenerator' does not contain a definition for 'm_Tex'
這是SO過於寬泛。如果您有關於該技術的*特定*問題或者您擁有的某些代碼;請縮小這個問題的範圍。 – BradleyDotNET 2014-11-14 18:42:51
我需要的是一種方法/指南,以遵循並貫徹到我的遊戲中。我的meteorGenerator類中沒有任何代碼,這就是爲什麼我問。 – PowerUser 2014-11-14 18:49:13
好的;但你說你已經看過使用'List'和'Random'的代碼。這確實是正確的做法。什麼讓你感到困惑? *這個問題是你應該在這裏問的。 – BradleyDotNET 2014-11-14 18:50:49