Jump to content
  • Advertisement
Sign in to follow this  
xuele91

how to declare a timer in the gameplay class when i have a timer class already?

This topic is 2992 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

HI!
i have a timer class and a gameplay screen class.
I want to show the timer in the gameplay screen.
How do i declare the timer in the gameplay screen and how to use the method from the timer class in the gameplay screen class?

Below are my timer class and gameplay screen class:

This is my timer class:


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 drag_and_lock_grid
{
class Timer
{
float timeElapsed;
float startingTimeInSeconds;
int currentMinutes;
int currentSeconds;
int currentTime;
float currentFloatTime;
string properSeconds;

public Timer(float startingTime)
{
startingTimeInSeconds = startingTime;
}

public void Update(GameTime gameTime)
{
timeElapsed += (float)gameTime.ElapsedGameTime.TotalSeconds;
currentFloatTime = startingTimeInSeconds - timeElapsed;

currentTime = Convert.ToInt32(currentFloatTime);
currentMinutes = currentTime / 60;
currentSeconds = currentTime % 60;

properSeconds = currentSeconds.ToString("00");

}

public void Draw(SpriteBatch spriteBatch, SpriteFont font)
{
// Draw my level timer stuff:
Vector2 timerPosition = new Vector2(10, 10);

string timerLabel;
if (currentSeconds >= 10)
{ timerLabel = currentMinutes + ":" + currentSeconds; }
else if (currentSeconds >= 0)
{ timerLabel = currentMinutes + ":" + properSeconds; }
else
{ timerLabel = "0:00"; }

spriteBatch.DrawString(font,
timerLabel, timerPosition, Color.White);
}

public int getTime()
{
return currentTime;
}
}
}




This is my Gameplay screen class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;
using Windows7.Multitouch;

namespace drag_and_lock_grid
{
class ScreenGameplay1 : Screen
{
SpriteFont font;
private float time;
SoundEffect soundEngine;
SoundEffectInstance soundInstance;

SoundEffect soundGameOver;
Boolean isPaused;

Vector2 basePosition;
Texture2D baseTexture;


Texture2D GamePausedTexture;
Vector2 GamePausedPosition;

Texture2D fadeTexture;
Texture2D pausedBaseTexture;
Vector2 pausedBasePosition;
Button buttonOptions;
Button buttonResumeGame;
Button buttonRestartGame;
Button buttonMainMenu;
Button buttonSoundOn;
Button buttonSoundOff;
Button buttonCurrentSound;

public static int noc = 0;
PuzzlePiece[] pieces;
public static Random random = new Random();

//AI
Boolean hasGenericAI = false;
Boolean hasRandomAI = true;
int interval = 10;

public ScreenGameplay1()
{
}

/// <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>
public override void Initialize()
{
time = 0;



Game1.menuSound.Pause();

basePosition = new Vector2(0, 0);

isPaused = false;

pieces = new PuzzlePiece[12];

int imageNo = 1;
int puzzleWidth = 3;
int puzzleHeight = 4;

Vector2 startPosition = new Vector2(569, 32);

Random random = new Random();
int tempNumberX = 0;
int tempNumberY = 0;

Texture2D tempTexture;

for (int i = 0; i < puzzleHeight; i++)
{
for (int j = 0; j < puzzleWidth; j++)
{

tempTexture = Game1.content.Load<Texture2D>("images/gameplay/game1/" + imageNo);
tempNumberX = random.Next(10, 500 - tempTexture.Width);
tempNumberY = random.Next(10, 780 - tempTexture.Height);
pieces[imageNo - 1] = new PuzzlePiece(0,
new Vector2(tempNumberX, tempNumberY), tempTexture, imageNo,
new Vector2(startPosition.X + (tempTexture.Width * j), startPosition.Y +
(tempTexture.Height * i)));
imageNo++;
}
}

soundEngine = Game1.content.Load<SoundEffect>("audio/game_play");
soundInstance = soundEngine.CreateInstance();
soundGameOver = Game1.content.Load<SoundEffect>("audio/Game_over");


baseTexture = Game1.content.Load<Texture2D>("images/gameplay/background");

fadeTexture = Game1.content.Load<Texture2D>("images/gameplay/fade");
pausedBaseTexture = Game1.content.Load<Texture2D>("images/base/darkened");
pausedBasePosition = new Vector2((Game1.graphicsViewport.Width - pausedBaseTexture.Width) / 2,
(Game1.graphicsViewport.Height - pausedBaseTexture.Height) / 2);
GamePausedTexture = Game1.content.Load<Texture2D>("images/gameplay/game options");
GamePausedPosition = new Vector2(25, 30);

buttonOptions = new Button(new Vector2(50, 630),
Game1.content.Load<Texture2D>("images/button/game_play/Options_button"));

buttonResumeGame = new Button(new Vector2(500, 160),
Game1.content.Load<Texture2D>("images/button/game_play/Resume_Game_button"));

buttonRestartGame = new Button(new Vector2(500, 310),
Game1.content.Load<Texture2D>("images/button/game_play/retry_button"));

buttonMainMenu = new Button(new Vector2(500, 460),
Game1.content.Load<Texture2D>("images/button/game_play/main_menu_button"));

buttonSoundOn = new Button(new Vector2(500, 610),
Game1.content.Load<Texture2D>("images/button/game_play/on"));

buttonSoundOff = new Button(new Vector2(500, 760),
Game1.content.Load<Texture2D>("images/button/game_play/off"));


//font = Game1.content.Load<SpriteFont>("font/Kootenay");
soundInstance.Volume = 1.0f;
soundInstance.IsLooped = true;

if (Game1.sound == true)
{
buttonCurrentSound = buttonSoundOn;
soundInstance.Play();
}
else
{
buttonCurrentSound = buttonSoundOff;
}

}

public override void touchDown(float x, float y)
{
if (!isPaused)
{
foreach (PuzzlePiece puzzle in pieces)
{
puzzle.touchDown(x,y);
}


if (buttonOptions.isWithin(x, y))
{
buttonOptions.touchDown();
buttonOptions.touchUp();
isPaused = true;
}
}
else
{
if (buttonResumeGame.isWithin(x, y))
{
buttonResumeGame.touchDown();
buttonResumeGame.touchUp();
isPaused = false;
}

if (buttonRestartGame.isWithin(x, y))
{
buttonRestartGame.touchDown();
soundInstance.Dispose();
ScreenManager.screenManager.addScreen(new ScreenGameplay1(), 0);
}

if (buttonMainMenu.isWithin(x, y))
{
buttonMainMenu.touchDown();
Game1.menuSound.Play();
soundInstance.Dispose();
ScreenManager.screenManager.addScreen(new ScreenMainMenu(), 0);
}

if (buttonCurrentSound.isWithin(x, y))
{
toggleSound();
}
}
}

public override void touchUp(float x, float y)
{
if (!isPaused)
{
foreach (PuzzlePiece puzzle in pieces)
{
puzzle.touchUp(x,y);
}

}
}
public override void touchMove(float x, float y)
{
if (!isPaused)
{
foreach (PuzzlePiece puzzle in pieces)
{
puzzle.touchMove(x, y);

}
}
}


/// <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>
public override void Update(GameTime gameTime)
{

// TODO: Add your update logic here
foreach (PuzzlePiece puzzle in pieces)
{
puzzle.Update();
}

base.Update(gameTime);
}


/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
public override void Draw(GameTime gameTime)
{
// TODO: Add your drawing code here
Game1.graphics.GraphicsDevice.Clear(Microsoft.Xna.Framework.Graphics.Color.Black);
Game1.spriteBatch.Begin();
Game1.spriteBatch.Draw(baseTexture, basePosition, Microsoft.Xna.Framework.Graphics.Color.White);

buttonOptions.Draw();

foreach (PuzzlePiece puzzle in pieces)
{
puzzle.Draw();
}


if (isPaused)
{
Game1.spriteBatch.Draw(fadeTexture, basePosition, Microsoft.Xna.Framework.Graphics.Color.White);
Game1.spriteBatch.Draw(GamePausedTexture, GamePausedPosition, Microsoft.Xna.Framework.Graphics.Color.White);
//Game1.spriteBatch.Draw(pausedBaseTexture, pausedBasePosition, Microsoft.Xna.Framework.Graphics.Color.White);

buttonResumeGame.Draw();
buttonRestartGame.Draw();
buttonMainMenu.Draw();
buttonCurrentSound.Draw();
}

Game1.spriteBatch.End();

base.Draw(gameTime);
}

public void toggleSound()
{
if (soundInstance.State == SoundState.Playing)
{
soundInstance.Pause();
Game1.sound = false;
buttonCurrentSound = buttonSoundOff;
}
else
{
soundInstance.Play();
Game1.sound = true;
buttonCurrentSound = buttonSoundOn;
}
}
}
}




Edit by Evil Steve: Added source tags.

[Edited by - Evil Steve on July 12, 2010 3:22:35 AM]

Share this post


Link to post
Share on other sites
Advertisement
Ummm, you'd do it just like any other class member. Can you be more specific about what you don't understand?

Share this post


Link to post
Share on other sites
Xuele I think if you don't understand something so simple you need to go back and start from the start with some basic programming tutorials.

I don't mean any offence, we all have to start at some stage but you seem to be diving into an example or a tutorial far too advanced for your current level of knowledge.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!