Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

[C# XNA] Mit Sekunden arbeiten

Empfohlene Antworten

Veröffentlicht

Moin,

Ich will in meinen Spiel einen Regen (Lebensregenaration, Ausdauerregenaration usw)

Ich will das der alle 5 sekunden +1 bei z.B. Leben dazuzählt.

Wenn ich das mit ner variable mache(also in der Update methode immer variable++ dann lagt das zimlich) Also kann man da irgendwie ne "Uhr" programmiern die alle 5 sekunden +1 macht?

mfg

Naja ich hab aber keine Form Anwendung. Sondern eine Game Class mit XNA.

Heißt er kennt Timer net.

Kann man da die using sachen von einem Form auch nehmen?

Grob gesagt kannst Du zwei Klassen verwenden:

System.Threading.Timer

System.Timers.Timer

Problematisch ist die Threadsicherheit.

Ich umgeh das immer in dem ich ein delegate definiere und dann im Handler für den Timer eintrage dass "this" ein delegate mit der Methode in der was passiert "invoken" soll.

Ich hab Dir nen Samplecode geschustert:

Klasse Programm


delegate void EnshureThreadSafty();

static class Program

{       

[INDENT]/// <summary>

/// Der Haupteinstiegspunkt für die Anwendung.

/// </summary>

[STAThread]

static void Main()

{

[INDENT]Application.EnableVisualStyles();

Application.SetCompatibleTextRenderingDefault(false);

Application.Run(new Form1());[/INDENT]


}[/INDENT]

}

Klasse MyGame

    class MyGame

    {

        public int AValue;

        public MyHero theHero;

        public System.Timers.Timer GlobalCoolDown = new System.Timers.Timer(5000);

        public MyGame()

        {

            GlobalCoolDown.Elapsed += new System.Timers.ElapsedEventHandler(GLOBAL_COOLDOWN_Elapsed);

            AValue = 1;

            theHero = new MyHero();

            GlobalCoolDown.Start();

        }


        void GLOBAL_COOLDOWN_Elapsed(object sender, System.Timers.ElapsedEventArgs e)

        {

            AValue += AValue == Int32.MaxValue

                        ? 0

                        : 1;

        }

    }

Klasse MyHero

    class MyHero

    {

        public System.Threading.Timer ActionCD;

        public bool Ready4Action;


        public MyHero()

        {

            Ready4Action = true;

            ActionCD = new System.Threading.Timer(

                new System.Threading.TimerCallback(actionCD_Elapsed), null,0, 10000);    

        }


        /// <summary>

        /// Bereit den Helden auf eine neue Aktion vor.

        /// </summary>

        /// <param name="sender">nicht relevant</param>

        /// <param name="e">nicht relevant</param>

        void actionCD_Elapsed(object state)

        {

            this.Ready4Action = true;

        }


        /// <summary>

        /// Führt eine Action aus wenn der Held bereit ist.

        /// </summary>

        public bool PerformAction()

        {

            bool actionPerformed = false;

            if (Ready4Action)

            {

                Ready4Action = false;

                //DO Action

                actionPerformed = true;

            }

            return actionPerformed;

        }

    }

Klasse Form1

    public partial class Form1 : Form

    {


        private MyGame theGame;

        public Form1()

        {

            InitializeComponent();

            theGame = new MyGame();

            theGame.GlobalCoolDown.Elapsed += new System.Timers.ElapsedEventHandler(GLOBAL_COOLDOWN_Elapsed);             

        }


        void GLOBAL_COOLDOWN_Elapsed(object sender, System.Timers.ElapsedEventArgs e)

        {

            this.Invoke(new EnshureThreadSafty(writeOutput));


        }


        private void but_action_Click(object sender, EventArgs e)

        {

            if (!theGame.theHero.PerformAction())

            {

                lb_output.Items.Add("Held nicht bereit");

            }

        }



        private void writeOutput()

        {

            lb_output.Items.Add("Global CD abgelaufen. Wert von theGame.AValue ist nun: " + theGame.AValue);

            lb_output.Items.Add("Global CD abgelaufen. theGame.theHero.Ready4Action = " + theGame.theHero.Ready4Action);

        }


#region Vom Windows Form-Designer generierter Code


        /// <summary>

        /// Erforderliche Methode für die Designerunterstützung.

        /// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden.

        /// </summary>

        private void InitializeComponent()

        {

            this.lb_output = new System.Windows.Forms.ListBox();

            this.but_action = new System.Windows.Forms.Button();

            this.SuspendLayout();

            // 

            // lb_output

            // 

            this.lb_output.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)

                        | System.Windows.Forms.AnchorStyles.Left)

                        | System.Windows.Forms.AnchorStyles.Right)));

            this.lb_output.FormattingEnabled = true;

            this.lb_output.Location = new System.Drawing.Point(12, 12);

            this.lb_output.Name = "lb_output";

            this.lb_output.Size = new System.Drawing.Size(552, 368);

            this.lb_output.TabIndex = 0;

            // 

            // but_action

            // 

            this.but_action.Dock = System.Windows.Forms.DockStyle.Bottom;

            this.but_action.Location = new System.Drawing.Point(0, 399);

            this.but_action.Name = "but_action";

            this.but_action.Size = new System.Drawing.Size(576, 23);

            this.but_action.TabIndex = 1;

            this.but_action.Text = "Action";

            this.but_action.UseVisualStyleBackColor = true;

            this.but_action.Click += new System.EventHandler(this.but_action_Click);

            // 

            // Form1

            // 

            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);

            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

            this.ClientSize = new System.Drawing.Size(576, 422);

            this.Controls.Add(this.but_action);

            this.Controls.Add(this.lb_output);

            this.Name = "Form1";

            this.Text = "Form1";

            this.ResumeLayout(false);


        }


        #endregion


        private System.Windows.Forms.ListBox lb_output;

        private System.Windows.Forms.Button but_action;

    }



Bearbeitet von Mcolli

wow schaut kompliziert aus.

hab das so gelöst.

   

LebenTimer.Interval = 3000;

LebenTimer.Tick += new EventHandler(LebenRegen);

LebenTimer.Start();


 public void LebenRegen(object sender, EventArgs e)

        {

            if (SollPlayergehen == true && RedenPlayer == false)

            {

                if (LebenPlayer < LebenPlayerMax)

                {

                    LebenPlayer++;

                }

                if (AusdauerPlayer < AusdauerPlayerMax)

                {

                    AusdauerPlayer++;

                }

            }

        }

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.