Logic question.....

This is a discussion on Logic question..... within the C++ Programming forums, part of the General Programming Boards category; I am initalizing the "Location" of an object in my init function but when I go and try to use ...

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    131

    Logic question.....

    I am initalizing the "Location" of an object in my init function but when I go and try to use it in my "tick" function it seems as if the orginal initaliaztion of the object i thrown out the window. Its kind of hard to explain....take a look....

    Code:
    MyStarFighterAlien::MyStarFighterAlien(System::Resources::ResourceManager * Resources, MyStarFighter *ship, MyStarFighterAlien *alien, MyStarFighterBullet *bullet, MyStarFighterBomb *bomb):Animation(Resources)
    	{	this->resources = resources;
    		this->alien = alien;
    		this->bullet = bullet;
    		this->ship = ship;
    		this->bomb = bomb;
    		
    		init(resources);
    	}
    	void MyStarFighterAlien::init(System::Resources::ResourceManager * Resources)
    	{	
    		myDirection = 1;//Direction of alien flight
    		Point alienLoc = Point(newAx,newAy);
    
    		this->Image = Image::FromFile(String::Concat(Directory::GetCurrentDirectory(),S"\\alien.bmp"));
    		this->Location = System::Drawing::Point(0, 10);
    		this->Name = S"Alien";
    		this->Size = System::Drawing::Size(49, 32);
    		this->TabIndex = 1;
    		this->TabStop = false;
    	}
    	void MyStarFighterAlien::handleAlienTick(MyStarFighterAlien *alien, MyStarFighterBullet *bullet, MyStarFighterBomb *bomb)
    	{
    		int newAx = alienLoc.X + myDirection; 
    		int newAy = alienLoc.Y; 
    				
    		Point newlocA = Point(newAx,newAy); 
    		alienLoc = newlocA; 
    
    		if (newAx > 505)//If alien goes off screen right change direction to left
    		{
    			myDirection = -1;
    		}
    		else if (newAx < -55)//If alien goes off screen left change directon to right
    		{
    			myDirection = 1;
    		}	
    	}
    As you can see the constructor calls the "initialaztion" function which initalizes the Location of the object to (0,10). But when I try to move the object in the "handleAlienTick" function it start at (0,0) not (0,10). So I am guessing there is something wrong with my logic in the "handleAlienTick" function. Any suggestions would be great.

    Thanks

    Chad

  2. #2
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    well, in your contructor, you have:

    this->resources = resources;
    this->alien = alien;
    this->bullet = bullet;
    this->ship = ship;
    this->bomb = bomb;

    init(resources);

    but your passed in variable is Resources, not resources.
    That's why I generally try not to use the same name for member variables and function parameters.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    131
    I was wondering about that. Are you saying it should be

    this->Resources = resources;
    this->alien = alien;
    this->bullet = bullet;
    this->ship = ship;
    this->bomb = bomb;

    init(resources);

    because this throws an error...



    error C2882: 'Resources' : illegal use of namespace identifier in expression

  4. #4
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    no, it should be:

    this->resources = Resources

    since Resources is local to the function it doesn't have scope to be used with this->

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    131
    Quote Originally Posted by rockytriton
    no, it should be:

    this->resources = Resources

    since Resources is local to the function it doesn't have scope to be used with this->

    Ahhh that makes sense.....

    Now I have the object actually flashing onto the screen in the correct place when the first "tick" happens, but when the second tick hits the object goes back to (0,0) before moving.....There is still something wrong in the logic of my handleAlienTick function...


    Thanks

    Chad

  6. #6
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    it looks ok, maybe the error is in the paint code or how you are using these variables. also, in your init function you are doing:

    Point alienLoc = Point(newAx,newAy);

    that's just creating a local variable, not assigning the member alienLoc variable.

  7. #7
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    You seem to have too many membervariables for the location.
    in init you initialize a member this->Location.
    in handleAlienTick you work with local variables newAx and newAy that are initialized from member variable alienLoc and stored as alienLoc.
    and finally in init you are working with a local variable alienLoc that is initialized from some membervariables ?? newAx and newAy.
    Kurt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. platform game logic, problem
    By Akkernight in forum Game Programming
    Replies: 7
    Last Post: 02-23-2009, 09:49 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  3. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 08:22 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 11:01 AM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 02:57 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21