Lesson 5 thing

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
Psychoman
Posts: 36
Joined: June 13th, 2013, 2:12 pm

Lesson 5 thing

Post by Psychoman » June 14th, 2013, 1:58 pm

Hello fellow game creators.I've got not a problem but a how to say "bug".I created a function which changes speed,but whenever I press space it gets me a feeling that standard speed and space one is adding and creating a space speed,but if it's enter works just fine.

Code: Select all

	//Code in ComposeFrame
           MoveAllDirect(4);
	if(kbd.SpaceIsPressed())
	{
		MoveAllDirect(1);	
	}
	if(kbd.EnterIsPressed())
	{
		MoveAllDirect(8);
	}
//This is the body of function
void Game::MoveAllDirect(int incr)
{
	if(kbd.RightIsPressed())
	{
		x+=incr;
	}
	if(kbd.LeftIsPressed())
	{
		x-=incr;
	}
	if(kbd.DownIsPressed())
	{
		y+=incr;
	}
	if(kbd.UpIsPressed())
	{
		y-=incr;
	}
}

User avatar
LuX
Posts: 1492
Joined: April 22nd, 2012, 12:33 pm
Location: Finland

Re: Lesson 5 thing

Post by LuX » June 14th, 2013, 5:58 pm

Code: Select all

//Code in ComposeFrame
    MoveAllDirect(4);                       // [1]

   if(kbd.SpaceIsPressed())
   {
      MoveAllDirect(1);                    // [2]
   }
   if(kbd.EnterIsPressed())
   {
      MoveAllDirect(8);                    // [3]
   }
I labeled the function points with numbers:
[1] in all cases the position is moved by 4
[2] in addition to [1], if space is pressed it will move another 1 unit
[3] if enter is pressed it will move [1] and [3] = 12 units, if space and enter are pressed it will move [1] + [2] + [3] times = 13 units

Its probably not what you wanted, so use "else if" instead, where if one case is true it will ignore the rest:

Code: Select all

if (enter is pressed)
{
    move ( 8 );
}
else if ( space is pressed )
{
    move ( 1 );
}
else // default if none above is true
{
    move ( 4 );
}
ʕ •ᴥ•ʔ

Psychoman
Posts: 36
Joined: June 13th, 2013, 2:12 pm

Re: Lesson 5 thing

Post by Psychoman » June 14th, 2013, 6:25 pm

Thanks, I just thought that it be 3 different ways,not standard thing and if you press something it adds standard to pressed =(.

Post Reply