Go Back   Zune Boards > Zune Discussions > Zune Games and Applications > Development Discussions

Development Discussions All developers who are coding games may stop by here for any help, suggestions, and everything development related.

Reply
 
Thread Tools
Old 03-22-2011, 03:08 PM   #1
crystalgolem420
Zewbie
 
Join Date: Mar 2011
Posts: 6
crystalgolem420 is on a distinguished road
Question char movement help

ok so im new to the whole XNA3.1 ZuneHD development, however not new with C#. im currently attempting to learn a few thing in an attempt to eventually make a game for the HD. here where im at: ive got sprites for buttons ( up,down,left,right,a,b,menu) and a place holder for char(for now till i get to animating the sprite) i can get all to draw in the position i want them(in portrait mode for now). i figured out how to get the character to move by touch(touch here he 'teleports' there, as well as 'click-n-drag') but i would to use the buttons to move the character i.e pressing the upBtn (i also have the touch locations for the buttons coded) makes character move up.

heres code sofar:
Code:

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 holycowsdfa
{
///<summary>
/// This is the main type for your game
///</summary>
publicclassGame1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D linkTex;
Texture2D uBtnTex;
Texture2D rBtnTex;
Texture2D dBtnTex;
Texture2D lBtnTex;
Texture2D aBtnTex;
Texture2D bBtnTex;
Texture2D menuBtnTex;
SpriteFont aldoFont;
 
float linkX = 0;
float linkY = 0;
Vector2 linkPos = newVector2();
 
Vector2 uBtnPos = newVector2(32, 384);
Vector2 rBtnPos = newVector2(64, 416);
Vector2 dBtnPos = newVector2(32, 448);
Vector2 lBtnPos = newVector2(0, 416);
Vector2 aBtnPos = newVector2(192, 384);
Vector2 bBtnPos = newVector2(192, 448);
Vector2 menuBtnPos = newVector2(128, 416);
 
Vector2 linkTexVector;
Rectangle upRegion, downRegion, leftRegion, rightRegion, aRegion, bRegion, menuRegion;
bool upTouched, downTouched, leftTouched, rightTouched, aTouched, bTouched, menuTouched;
float touchPressure;
public Game1()
{
graphics = newGraphicsDeviceManager(this);
Content.RootDirectory = "Content";
 
// Frame rate is 30 fps by default for Zune.
TargetElapsedTime = TimeSpan.FromSeconds(1 / 30.0);
}
///<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>
protectedoverridevoid Initialize()
{
// TODO: Add your initialization logic here
upRegion = newRectangle(32, 384, 32, 32);
downRegion = newRectangle(32, 448, 32, 32);
leftRegion = newRectangle(0, 416, 32, 32);
rightRegion = newRectangle(64, 416, 32, 32);
aRegion = newRectangle(192, 384, 32, 32);
bRegion = newRectangle(192, 448, 32, 32);
menuRegion = newRectangle(128, 416, 32, 32);
 
 
 
base.Initialize();
}
///<summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
///</summary>
protectedoverridevoid LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = newSpriteBatch(GraphicsDevice);
// TODO: use this.Content to load your game content here
linkTex = Content .Load <Texture2D > ("linky");
 
uBtnTex = Content.Load<Texture2D>("uBtn");
rBtnTex = Content.Load<Texture2D>("rBtn");
dBtnTex = Content.Load<Texture2D>("dBtn");
lBtnTex = Content.Load<Texture2D>("lBtn");
aBtnTex = Content.Load<Texture2D>("aBtn");
bBtnTex = Content.Load<Texture2D>("bBtn");
menuBtnTex = Content.Load<Texture2D>("menuBtn");
 
aldoFont = Content.Load<SpriteFont > ("aldo");
 
}
///<summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
///</summary>
protectedoverridevoid UnloadContent()
{
// TODO: Unload any non ContentManager content here

 
}
///<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>
protectedoverridevoid Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
// TODO: Add your update logic here
//IEnumerator <TouchLocation> touches = TouchPanel.GetState().GetEnumerator();
TouchCollection touchStates = TouchPanel .GetState ();
upTouched = downTouched = leftTouched = rightTouched = false;
aTouched = bTouched = menuTouched = false;
 
 
for (int i = 0; i < touchStates.Count; ++i)
{
if (touchStates[i].Pressure > 0.1f)
{
int x = (int)touchStates[i].Position.X;
int y = (int)touchStates[i].Position.Y;
 
if (upRegion.Contains(x, y)) upTouched = true;
if (downRegion.Contains(x, y)) downTouched = true;
if (leftRegion.Contains(x, y)) leftTouched = true;
if (rightRegion.Contains(x, y)) rightTouched = true;
 
}
 
}
 
/*while (touches.MoveNext())
{
linkTexVector = touches.Current.Position;
touchPressure =touches.Current.Pressure;
}*/
base.Update(gameTime);
}
///<summary>
/// This is called when the game should draw itself.
///</summary>
///<param name="gameTime">Provides a snapshot of timing values.</param>
protectedoverridevoid Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue );
spriteBatch.Begin(SpriteBlendMode .AlphaBlend ); // <---this enables the alphablending, set color key in the texture properties
//string currentVector = string.Format("{0}.{1}:{2}", linkTexVector.X.ToString(), linkTexVector.Y.ToString(), touchPressure.ToString());
//spriteBatch.DrawString(aldoFont, currentVector, new Vector2(50, 50), Color.Black);

spriteBatch.Draw(linkTex, linkTexVector, Color.White);
spriteBatch.Draw(uBtnTex, uBtnPos, Color.White);
spriteBatch.Draw(rBtnTex, rBtnPos, Color.White);
spriteBatch.Draw(dBtnTex, dBtnPos, Color.White);
spriteBatch.Draw(lBtnTex, lBtnPos, Color.White);
spriteBatch.Draw(aBtnTex, aBtnPos, Color.White);
spriteBatch.Draw(bBtnTex, bBtnPos, Color.White);
spriteBatch.Draw(menuBtnTex, menuBtnPos, Color.White);
 
 
spriteBatch.End();
base.Draw(gameTime);
}
}
}
as you may notice there are lines of code that are commented out....thats cause i was experimenting....anyways like i said
"how do i code 'link' to move when i press on the dir buttons?"



p.s. please and thank you for your help




crystalgolem420 is offline   Reply With Quote

Advertisement [Remove Advertisement]
Old 03-22-2011, 06:14 PM   #2
Nanaki
Holy Zuner
Development Front
Retired Staff
Holy Zuner
 
Nanaki's Avatar
 
Join Date: Jan 2007
Posts: 8,658
Nanaki has a brilliant futureNanaki has a brilliant futureNanaki has a brilliant futureNanaki has a brilliant futureNanaki has a brilliant futureNanaki has a brilliant futureNanaki has a brilliant futureNanaki has a brilliant futureNanaki has a brilliant futureNanaki has a brilliant future
Default

if(rightTouched)
linkX++;

You mean that sort of thing? Maybe I'm not sure what you're asking?



Nanaki is offline   Reply With Quote
Old 03-22-2011, 06:24 PM   #3
crystalgolem420
Zewbie
 
Join Date: Mar 2011
Posts: 6
crystalgolem420 is on a distinguished road
Default

i tried that but when i run it, the character wont move, and the debug doesnt throw any errors or warnings.....




crystalgolem420 is offline   Reply With Quote
Old 03-22-2011, 06:31 PM   #4
Nanaki
Holy Zuner
Development Front
Retired Staff
Holy Zuner
 
Nanaki's Avatar
 
Join Date: Jan 2007
Posts: 8,658
Nanaki has a brilliant futureNanaki has a brilliant futureNanaki has a brilliant futureNanaki has a brilliant futureNanaki has a brilliant futureNanaki has a brilliant futureNanaki has a brilliant futureNanaki has a brilliant futureNanaki has a brilliant futureNanaki has a brilliant future
Default

That would be because you never update linkTexVector to reflect linkX and linkY. You should only have 1 place where you're storing its position, either the vector or the dual ints.



Nanaki is offline   Reply With Quote
Old 04-04-2011, 12:03 PM   #5
crystalgolem420
Zewbie
 
Join Date: Mar 2011
Posts: 6
crystalgolem420 is on a distinguished road
Default

thanks nurta, i got it figured out.....now for scrollling backgrounds....any decent/good examples?




crystalgolem420 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
no new posts