Thread: Need Help: Variable transfer to other class

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    1

    Need Help: Variable transfer to other class

    I'm new to c++ and I'm writing a 2D game with SDL right now and i want to make a game where you're being followed by a tornado. So you can move ans the tornado move towards you. But when I use the x coordinates of the boy in the move section of the tornado it sais that te xboy is not declared so I can i do that

    Here's the code so far

    Code:
    ///The boy that will move around on the screen
    class Boy
    {
        private:
        //The X and Y offsets of the boy
        int xboy, yboy;
        
        //The velocity of the boy
        int xVelboy, yVelboy;
        
        public:
        //Initializes the variables
        Boy();
        
        //Takes key presses and adjusts the boy's velocity
        void handle_input();
        
        //Moves the boy
        void move();
        
        //Shows the boy on the screen
        void show();
    };
    
    Boy::Boy()
    {
        //Initialize the offsets
        xboy = 40;
        yboy = 40;
        
        //Initialize the velocity
        xVelboy = 0;
        yVelboy = 0;
    }
    after this, I have the other functions and then I create the Tornado class

    Code:
    //The Tornado that will move around on the screen
    class Tornado
    {
        private:
        //The X and Y offsets of the Tornado
        int xtor, ytor;
        
        //The velocity of the Tornado
        int xVeltor, yVeltor;
        
        public:
        //Initializes the variables
        Tornado();
        
        //Moves the Tornado
        void move();
        
        //Shows the Tornado on the screen
        void show();
    };
    
    Tornado::Tornado()
    {
        //Initialize the offsets
        xtor = 800;
        ytor = 400;
        
        //Initialize the velocity
        xVeltor = 0;
        yVeltor = 0;
    }
    
    
    void Tornado::move()
    {
        
        //Move the Tornado left or right
        xtor += xVeltor;
        
        //If the Tornado is at the right side of the boy
        if(  xtor > xboy  )
        {
            //move to the left
            xVeltor = -5;    
        }
          
        //If the Tornado is at the left side of the boy
        if(  xtor < xboy  )
        {
            //move to the left
            xVeltor = 5;    
        } 
        
        //Move the Tornado up or down
        ytor += yVeltor;
        
        //If the Tornado is above the boy
        if(  ytor < yboy  )
        {
            //move to the left
            yVeltor= 5;    
        } 
       
       //If the Tornado is under the boy
        if(  ytor > yboy  )
        {
            //move to the left
            yVeltor = -5;    
        } 
    
    }
    The game works when I place a number in stead of xboy and yboy but then the tornado moves towards that point and then stays right there.

    Please help me

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Well you could make Tornado have a pointer to the Boy object. Then the Tornado could call code like boy->get_xboy() for example. Food for thought:
    Code:
    class Boy {
    private:
        int xboy, yboy;
    public:
        int get_xboy() { return xboy; }
        int get_yboy() { return yboy; }
    };
    
    class Tornado {
    private:
        Boy *boy;
    public:
        void setBoy(Boy *b) { boy = b; }
        void move();
    };
    
    void Tornado::move() {
        int xboy = boy->get_xboy();
        int yboy = boy->get_yboy();
        // ...
    }
    
    // somewhere else, maybe main()
    Boy b;
    Tornado t;
    t.setBoy(&b);
    
    t.move();  // now knows about xboy
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-19-2011, 10:03 PM
  2. Need help with a class variable.
    By RealityFusion in forum C++ Programming
    Replies: 11
    Last Post: 10-20-2005, 10:26 AM
  3. static class variable vs. global variable
    By nadamson6 in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2005, 03:31 PM
  4. Replies: 7
    Last Post: 05-19-2004, 01:55 AM
  5. Variable "transfer"
    By gcn_zelda in forum Windows Programming
    Replies: 1
    Last Post: 12-14-2003, 11:52 AM