|
  
|
|
|||||||
| Development Discussions All developers who are coding games may stop by here for any help, suggestions, and everything development related. |
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 (permalink) |
|
Experienced Zuner
|
I am programing a game called Xmonster. Right now I have all the basics programed. Everything is working fine except a few things. It is runable on the zune. But, I ran into a little wall.
I have sprites to imatate walking, however it only displays left and right animation because they are the last of the program...even when you are pressing up and down. If I comment out the left and right animations, the up and down work. Please help me find out how to code it so the character faces the direction he is walking. ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Movement Code: Code:
//Touch Control for the person
GamePadState gamepadstatus = GamePad.GetState(PlayerIndex.One);
//Vertical Movement
personPos.Y += (int)((gamepadstatus.ThumbSticks.Left.Y * 5) * -1);
if ((int)((gamepadstatus.ThumbSticks.Left.Y * 5) * -1) < 0)
vert = -1;
else
vert = 1;
//Horizontal movement
personPos.X += (int)((gamepadstatus.ThumbSticks.Left.X * 5) * 1);
if ((int)((gamepadstatus.ThumbSticks.Left.X * 5) * 1) < 0)
horiz = -1;
else
horiz = 1;
Drawing code: Code:
if (vert == 1)
{
if (pose == 1)
spriteBatch.Draw(walkDown1, personPos, Color.White);
else
spriteBatch.Draw(walkDown2, personPos, Color.White);
}
else
{
if (pose == 1)
spriteBatch.Draw(walkUp1, personPos, Color.White);
else
spriteBatch.Draw(walkUp2, personPos, Color.White);
}
if (horiz == -1)
{
if (pose == 1)
spriteBatch.Draw(walkLeft1, personPos, Color.White);
else
spriteBatch.Draw(walkLeft2, personPos, Color.White);
}
else
{
if (pose == 1)
spriteBatch.Draw(walkRight1, personPos, Color.White);
else
spriteBatch.Draw(walkRight2, personPos, Color.White);
}
spriteBatch.Draw(monster, monsterPos, Color.White);
pose = pose * -1;
Thanks the monster sprite, is a sprite from the zune game caveIn...I needed an extra sprite for the testing of this program...It will so be replaced once the code is correct.
__________________
To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts. ![]() |
|
|
|
| Remove Advertisements Sponsored Links | |
Advertisement |
|
|
|
#2 (permalink) |
|
zB Programmer
Experienced Member Join Date: Mar 2007
Posts: 966
Reputation: 114
Donate |
I dont know if this will work but you can try doing this in your button press code when you press right(or left) set the vertical variable to zero, and when up or down is pressed set the vertical variable to 0
I believe what is happening is that when you press(say right) the horizontal is assigned 1, but if you press up then vert is assigned 1 too, therefore both variables are assigned directions and only the very last if statement(the way you have it) carries out(because first it checks for side directions(and even though it finds the right one, it goes to the next if statement and finds that thats true too so it does that one), then it checks up/down)
__________________
To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts. Last edited by LedZepp : 07-03-2008 at 09:17 PM. ![]() |
|
|
|
|
|
#3 (permalink) |
|
Experienced Zuner
|
Hey thanks... that helped alot. It did not fix the problem... but I had to use what you said along with another addition. I created a lastgamepadstate. Then before it set new values it check to see if the lastgamepadstate == the current gamepadstate. If not then it changed thte value and painted it to the screen. I had to use your recomendation to tell it what to paint...so it wouldn't paint all of them.
Thanks
__________________
To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts. ![]() |
|
|
|
|
|
#4 (permalink) | |
|
zB Programmer
Jr. Member Join Date: May 2008
Location: Bremen, germany
Posts: 347
Reputation: 101
Donate |
Quote:
Why? Because you check if horiz is -1 and what if it zero (as supposed when you do not walk horizontally)? Well, instead of doing nothing you say: ELSE So no matter which value is stored in horiz (including 0), when its unequal to -1 it goes through the following code: Code:
if (pose == 1)
spriteBatch.Draw(walkRight1, personPos, Color.White);
else
spriteBatch.Draw(walkRight2, personPos, Color.White);
__________________
To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts. ![]() ![]() ![]() |
|
|
|
|
|
|
#5 (permalink) |
|
Experienced Zuner
|
I rewrote my whole program using a classfile to handle all the movement and positions of the characters. Everything is working right now...thanks for your help. See if this code looks good. This is my first ever C# program.
Movement Calculation: Code:
//Calculates the next position for the character
public Vector2 updatePerson()
{
//Touch Control for the person
gamepadStatus = GamePad.GetState(PlayerIndex.One);
//Vertical Movement
//Checks to see if last gampad status is the same as current
if (lastGamepadStatus.ThumbSticks.Left.Y != gamepadStatus.ThumbSticks.Left.Y)
{
personPos.Y += (int)((gamepadStatus.ThumbSticks.Left.Y * 5) * -1);
if ((int)((gamepadStatus.ThumbSticks.Left.Y * 5) * -1) < 0)
{
vert = -1;
}
else
{
vert = 1;
}
horiz = 0;
}
//Horizontal movement
//Checks to see if last gampad status is the same as current
if (lastGamepadStatus.ThumbSticks.Left.X != gamepadStatus.ThumbSticks.Left.X)
{
personPos.X += (int)((gamepadStatus.ThumbSticks.Left.X * 5) * 1);
if ((int)((gamepadStatus.ThumbSticks.Left.X * 5) * 1) < 0)
{
horiz = -1;
}
else
{
horiz = 1;
}
vert = 0;
}
//Checks for the Person being off the screen
if (personPos.Y >= 293)
personPos.Y = 293;
if (personPos.Y <= 0)
personPos.Y = 0;
if (personPos.X >= 226)
personPos.X = 226;
if (personPos.X <= 0)
personPos.X = 0;
return personPos;
}
public void poseCalculation()
{
//Character Animations
poseCalc++;
if (poseCalc == 10)
{
pose = pose * -1;
poseCalc = 1;
}
}
//Sends values for Drawing IF statements to correctly draw sprites
public int[] directionUpdate()
{
directionCalc[0] = horiz;
directionCalc[1] = vert;
directionCalc[2] = pose;
return directionCalc;
}
Code:
spriteBatch.Draw(bg, new Rectangle(0, 0, 240, 320), Color.White);
if (directionInfo[1] != 0)
{
if (directionInfo[1] == 1)
{
if (directionInfo[2] == 1)
spriteBatch.Draw(walkDown1, personPos, Color.White);
else
spriteBatch.Draw(walkDown2, personPos, Color.White);
}
else
{
if (directionInfo[2] == 1)
spriteBatch.Draw(walkUp1, personPos, Color.White);
else
spriteBatch.Draw(walkUp2, personPos, Color.White);
}
}
if (directionInfo[0] != 0)
{
if (directionInfo[0] == -1)
{
if (directionInfo[2] == 1)
spriteBatch.Draw(walkLeft1, personPos, Color.White);
else
spriteBatch.Draw(walkLeft2, personPos, Color.White);
}
else
{
if (directionInfo[2] == 1)
spriteBatch.Draw(walkRight1, personPos, Color.White);
else
spriteBatch.Draw(walkRight2, personPos, Color.White);
}
}
if (directionInfo[2] == 1)
spriteBatch.Draw(monster, monsterPos, Color.White);
else
spriteBatch.Draw(monsterWalk, monsterPos, Color.White);
if (charsLocation.gameOver())
spriteBatch.DrawString(Font, "Game Over", gameOver, Color.Red);
base.Draw(gameTime);
spriteBatch.End();
__________________
To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts. ![]() |
|
|
|
|
|
#6 (permalink) |
|
zB Programmer
Jr. Member Join Date: May 2008
Location: Bremen, germany
Posts: 347
Reputation: 101
Donate |
[quote=Rockman87;257171]
Code:
personPos.Y += (int)((gamepadStatus.ThumbSticks.Left.Y * 5) * -1);
if ((int)((gamepadStatus.ThumbSticks.Left.Y * 5) * -1) < 0)
{
.
.
.
Store the value instead. In addition, why do you add a value to personPos.Y and negate it by multiplying with -1? You could better subtract it from the beginning. It would be the same... Applying these things, the code should rather look like: Code:
int TmpValue = (int)(gamepadStatus.ThumbSticks.Left.Y * 5);
personPos.Y -= TmpValue;
if (TmpValue < 0)
{
.
.
.
__________________
To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts. ![]() ![]() ![]() |
|
|
|
|
|
#7 (permalink) |
|
Experienced Zuner
|
Ya, your correct... that does make more sense. That will keep the game running faster.
You probably noticed that the previous methods above are methods I call from a class file into my main program. Is this more efficient than just having all the code in your main program. Is this faster?
__________________
To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts. ![]() |
|
|
|
|
|
#8 (permalink) |
|
zB Programmer
Jr. Member Join Date: May 2008
Location: Bremen, germany
Posts: 347
Reputation: 101
Donate |
Classes are the way to go... I don't think that its a good idea to put everything in the main program. Make use of OOP as much as you can.
__________________
To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts. ![]() ![]() ![]() |
|
|
|
|
|
#9 (permalink) |
|
Experienced Zuner
|
Why wont this code work.... It deploys error free but does not update the speed of the monster.
Main game code: This piece of code scores and then sends the score to the Level Control class( if multiple of 10) to see if person reached the next level Code:
//Check to see if scores
if (anyCollisions[0] == true)
{
score++;
scoreStr = score.ToString();
personPos2 = charsLocation.updatePerson2();
if (score % 10 == 0)
level.advanceLevel(score);
charsLocation.collisionReset();
}
Code:
public void advanceLevel(int score)
{
//Level 2
if (score == 10)
{
monsterControl.changeMonSpeed(2.0f);
}
Code:
public void changeMonSpeed(float chngSpeed)
{
monsterSpeed = 2f;
}
this is how the monster moves in the movement class: Code:
public Vector2 updateMonster()
{
//Position for the Monster
if (monsterPos.Y < personPos.Y)
monsterPos.Y += monsterSpeed;
else
monsterPos.Y -= monsterSpeed;
if (monsterPos.X < personPos.X)
monsterPos.X += monsterSpeed;
else
monsterPos.X -= monsterSpeed;
return monsterPos;
}
__________________
To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts. ![]() |
|
|
|
|
|
#10 (permalink) |
|
zB Programmer
Experienced Member Join Date: Mar 2007
Posts: 966
Reputation: 114
Donate |
Upload your whole movement class to PasteBin, it will help us help you
__________________
To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts. ![]() |
|
|
|
|
|
#11 (permalink) |
|
Experienced Zuner
|
ok... my movement class(Locations) is on paste bin
[edit]If you checked paste bin before you see this message check again... l didn't change something in my code. Because it wasn't working used a solid value instead of a var for debugging purposes... now it is back a variable http://pastebin.com/m66dd0130
__________________
To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts. Last edited by Rockman87 : 07-05-2008 at 12:50 PM. ![]() |
|
|
|
|
|
#12 (permalink) |
|
zB Programmer
Experienced Member Join Date: Mar 2007
Posts: 966
Reputation: 114
Donate |
Well I cant see anything wrong, The only thing that could be happening is that this line:
Code:
level.advanceLevel(score);
__________________
To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts. ![]() |
|
|
|
|
|
#13 (permalink) |
|
Experienced Zuner
|
I put the break point where you said... it reaches the break point... so then I added breakpoints to all the other places, even the one that changes monster speed. It reaches them all.
If it hits the line that changes the speed...why doesn't the speed change?
__________________
To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts. ![]() |
|
|
|
|
|
#15 (permalink) | |
|
zB Programmer
Experienced Member Join Date: Mar 2007
Posts: 966
Reputation: 114
Donate |
Could it be that the score is increasing too fast, and before you know it the moster is moving at 2f instead of .25f? try increasing the 2f to like 20f and see if you see a difference
Quote:
__________________
To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts. Last edited by LedZepp : 07-05-2008 at 01:04 PM. ![]() |
|
|
|
|
|
|
#16 (permalink) |
|
zB Programmer
Jr. Member Join Date: May 2008
Location: Bremen, germany
Posts: 347
Reputation: 101
Donate |
public void changeMonSpeed(float chngSpeed)
{ monsterSpeed = 2f; } You send the new speed to the function but you do not set it... you always set it to 2f. monsterSpeed += chngSpeed; would be the way to go.....
__________________
To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts. ![]() ![]() ![]() |
|
|
|