Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am trying to move the object around the screen using cocos2d. I have this code:

-(void) move
{
  CGFloat upperLimit = mWinSize.height - (mGunda.contentSize.height / 2.0);
  CGFloat upperLimit1 = mWinSize.height;
  CGFloat lowerLimit = (mGunda.contentSize.height / 2.0);
  CGFloat RightLimit = mWinSize.width - (mGunda.contentSize.width/2.0);
  CGFloat Right = (mGunda.contentSize.width/2.0);

  if(mImageGoingUpward ) {
    mGunda.position = ccp( mGunda.position.x, mGunda.position.y + 5);

    if(mGunda.position.y >= upperLimit) {
      mImageGoingUpward = NO;
      mHori = NO;
    }
  } else {
    mGunda.position = ccp( mGunda.position.x, mGunda.position.y - 5);
    if(mGunda.position.y <= lowerLimit) {
      mGunda.position = ccp(mGunda.position.x +5, lowerLimit);
    }

    if(mGunda.position.x >= RightLimit) {
      mGunda.position = ccp(mGunda.position.x, mGunda.position.y+10);
      mHori = YES;   
    }

    if(mHori) {
      if(mGunda.position.y >= upperLimit) {
        mGunda.position = ccp(mGunda.position.x - 5,mGunda.position.y);
      }
    }  
  }
}

It is supposed to move the object around the screen. The code mostly works, but it doesn't move the object from the top-right of the screen to the left of the screen.

share|improve this question

migrated from gaming.stackexchange.com Jan 10 '11 at 13:38

This question came from our site for passionate videogamers on all platforms.

2  
you will propably have to explain what the ccp function does. And it would be nice if you format your code in some better way –  Notabene Jan 10 '11 at 13:45
    
ccp is just a wrap for CGPointMake –  David McGraw Feb 9 '11 at 19:37
    
It's probably important to note that this code is using cocos2d. Also, could someone with edit powers please clean up the sample code? It's very hard to read! –  Olie Sep 26 '11 at 15:04

2 Answers 2

it's basically because when you mark mHury as true, this statement if(mGunda.position.y >= upperLimit) is still not true.

share|improve this answer

Check through your logic - it is broken. To sum it up in English, assuming I got it right (your code is horribly indented and not C++ :P)

if going upwards then adjust position upwards and if we exceed some limit in y stop further upwards movement. else: lower the image (wtf???) if y is below some lower limit move it to the right (wtf???) if x is above some upper limit then move it upwards at double speed (again, wtf???) and set a flag to say we are moving horizontally if the horizontal flag is set and we have y exceeding some upper limit then move it to the left. (again, wtf???)

Maybe I am missing something, but this makes no sense to me. What are you actually trying to achieve and how is the requirement to move input to this code (e.g. who sets mImageGoingUpward - can mHori be set from outside, etc.)? It is not very obvious, which precludes a detailed and complete answer to the question.

share|improve this answer
1  
It's Objective-C code, not C++. –  Josh Petrie Oct 10 '11 at 17:35
1  
Also this doesn't really answer the question. –  Josh Petrie Oct 10 '11 at 17:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.