Jump to content


Help with the devmaster tutorials???

c++

5 replies to this topic

#1 darkness7479

    New Member

  • Members
  • Pip
  • 3 posts

Posted 24 January 2012 - 03:43 PM

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:
http://devmaster.net...rt-5-conditions

#2 jari

    New Member

  • Members
  • PipPip
  • 18 posts

Posted 24 January 2012 - 08:28 PM

Show your condition code, otherwise we can't help you.
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;
}


#3 darkness7479

    New Member

  • Members
  • Pip
  • 3 posts

Posted 25 January 2012 - 05:10 PM

*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;
}
}


#4 jari

    New Member

  • Members
  • PipPip
  • 18 posts

Posted 25 January 2012 - 06:04 PM

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


#5 Reedbeta

    DevMaster Staff

  • Administrators
  • 5305 posts
  • LocationBellevue, WA

Posted 25 January 2012 - 06:05 PM

Don't forget to use [ code ]...[ /code ] on the forum to post code. :)
reedbeta.com - developer blog, OpenGL demos, and other projects

#6 darkness7479

    New Member

  • Members
  • Pip
  • 3 posts

Posted 10 February 2012 - 02:58 PM

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





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users