Jump to content

  • Log In with Google      Sign In   
  • Create Account


Returning an element from an array


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
24 replies to this topic

#21 ApochPiQ   Moderators   -  Reputation: 14295

Like
0Likes
Like

Posted 17 July 2012 - 05:27 PM

Show us the code that is having problems.

If it's too large to post, consider taking out pieces one by one until you identify the smallest program subset that actually demonstrates the problem you're having, and post that instead.

Sponsor:

#22 Toshio   Members   -  Reputation: 135

Like
0Likes
Like

Posted 17 July 2012 - 05:47 PM

void Game::OnInit()
{	
    Map map;
    Player p(map);
    p.Init();
}

void Game::OnLoop()
{
    Map map;
    Player p(map);
    p.Move();
}

Nothing compiles as it should.
Here are those two functions:

void Player::Init()
{
    CSurface c;
    x = 100;
    y = 100;
    texture = c.loadTexture("player.png");
}
void Player::Move()
{
    x += xVel * (delta / 1000.0f);
    y += yVel * (delta / 1000.0f);
 
    //...
}


#23 Servant of the Lord   Crossbones+   -  Reputation: 17247

Like
1Likes
Like

Posted 17 July 2012 - 06:01 PM

If you create a Map or Player within a function, it creates a new Map or Player, not the one you used previously.

You have to create them once, by placing them in the Game declaration.
class Game
{
	 public:
	 Game();

	 //...other stuff.

	 private:
	 Map map;
	 Player player;

};

And in the constructor, you need to initialize Player with the Map, in the Game's constructor.
Game::Game() : player(map) //Pass 'map' into the Player constructor.
{

}

...And then you can use 'map' and 'player' normally, without re-creating them.
void Game::OnInit()
{	  
    p.Init();
}

void Game::OnLoop()
{
    p.Move();
}

Edited by Servant of the Lord, 17 July 2012 - 06:02 PM.

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.

[Fly with me on Twitter] [Google+] [My broken website]

All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.                                                                                                                                                       [Need free cloud storage? I personally like DropBox]

Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal


#24 Cornstalks   Crossbones+   -  Reputation: 6966

Like
1Likes
Like

Posted 17 July 2012 - 06:27 PM

#include <vector>

class Map
{
	 public:
     [...]

	 private:
	 int width, height;
	 std::vector<int> tiles;

	 //Converts from a two-dimensional x,y to a one-dimensional index into our vector.
	 int _GetIndex(int x, int y)
     [...]

	 //Makes sure that we aren't going out of bounds, which will crash the program.
	 bool _IsValid(int index)
     [...]
};

Just a note here so people don't get into any bad habits: identifiers starting with an underscore followed by an upper case letter should never be used, as they're reserved for the implementation.
[ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

#25 Toshio   Members   -  Reputation: 135

Like
0Likes
Like

Posted 18 July 2012 - 03:31 AM

Wow, thank you Servant of the Lord. Everything works now :D
Thanks ApochPiQ and everyone else who helped :)




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS