0
101 Jan 24, 2012 at 15:43 c++

can someone help me with the C++ tutorial about conditions?
I am new to C++ and just can’t get the code right to let the tank move up and down, left and right, without doing both at the same time (so it moves diagonally)
the post itself:

#### 5 Replies

0
101 Jan 24, 2012 at 20:28

If you don’t want your tank to move diagonally, so only one move direction at a time then check your movements in one condition (simple way):

if(left)
{
tank.x -= 1;
}
else if(right)
{
tank.x += 1;
}
else if(up)
{
tank.y -= 1;
}
else if(down)
{
tank.y += 1;
}

0
101 Jan 25, 2012 at 17:10

*Facepalm, indeed I forgot the code, here it is:

// Template, major revision 3
// IGAD/NHTV - Jacco Bikker - 2006-2009

#include "string.h"
#include "surface.h"
#include "stdlib.h"
#include "template.h"
#include "game.h"

using namespace Tmpl8;

void Game::Init()
{
// put your initialization code here; will be executed once
}

Sprite theSprite( new Surface("assets/ctankbase.tga"), 16 );

int SpriteX = 0;
int SpriteY = 100;
bool Visible = true;
bool Direction = true;
bool Direction2 = true;

void Game::Tick( float a_DT )
{
// render a single frame here
m_Screen->Clear( 0 );
if (Visible == true) theSprite.Draw( SpriteX, SpriteY, m_Screen );
// alter our coords

if (Direction2 == true)
{// he is moving up so sub tract from his Y
SpriteY -= 2;
if (SpriteY < 100) Direction2 = false;
}
else
{ // he is moving down so add to his Y
SpriteY += 2;
if (SpriteY > 300) Visible = false;
}
if (Direction == true, Direction2 == false)
{ // he is moving right so add to his x
SpriteX += 1;
if (SpriteX > 500) Direction = false;
}
else
{ // he is moving left so sub tract from his x
SpriteX -= 1;
if (SpriteX < 100) Direction = true;
}
}

0
101 Jan 25, 2012 at 18:04
if (Direction == true, Direction2 == false)


should be:

if (Direction == true && Direction2 == false)


This means IF Direction equals TRUE AND Direction2 equals FALSE the do this and this.
I never saw a comma in if statements like this, in msvc++ it compiles with no error or warning, but totally ignores things before the comma (as writing if(Direction2 == false)). It surprised me. :D

0
165 Jan 25, 2012 at 18:05

Don’t forget to use [ code ]…[ /code ] on the forum to post code. :)

0
101 Feb 10, 2012 at 14:58

unfortunately, the tips didn’t work out, I’ll just do another tutorial to get more experience and examples, maybe then I can find a way to do this.
still thnx for the help jari ;)