Jump to content
  • Advertisement
Sign in to follow this  
xuele91

Help! Why can't the puzzle pieces move ? *XNA touch move handler* thankyou

This topic is 2899 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

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;

namespace drag_and_lock_grid
{
class ScreenGameplay1 : Screen
{
SpriteFont font;

SoundEffect soundEngine;
SoundEffectInstance soundInstance;

SoundEffect soundGameOver;
Boolean isPaused;

Vector2 basePosition;
Texture2D baseTexture;

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

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()
{

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);

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

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

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

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

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

buttonSoundOff = new Button(new Vector2(525, 558),
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)
{
if (!isPaused)
{
// Allows the game to exit
//if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
// this.Exit();

//if (noc == 12)
// this.Exit();

// 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(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;
}
}
}
}

Share this post


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

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!